com.esotericsoftware.kryo.util.Util类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(424)

本文整理了Java中com.esotericsoftware.kryo.util.Util类的一些代码示例,展示了Util类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util类的具体详情如下:
包路径:com.esotericsoftware.kryo.util.Util
类名称:Util

Util介绍

[英]A few utility methods, mostly for private use.
[中]一些实用方法,主要用于私人用途。

代码示例

代码示例来源:origin: apache/metron

@Override
 public Object newInstance () {
  try {
   return constructor.newInstance();
  } catch (Exception ex) {
   throw new KryoException("Error constructing instance of class: " + className(type), ex);
  }
 }
};

代码示例来源:origin: com.esotericsoftware/kryo

/** Returns the class formatted as a string. The format varies depending on the type. */
static public String className (Class type) {
  if (type.isArray()) {
    Class elementClass = getElementClass(type);
    StringBuilder buffer = new StringBuilder(16);
    for (int i = 0, n = getDimensionCount(type); i < n; i++)
      buffer.append("[]");
    return className(elementClass) + buffer;
  }
  if (type.isPrimitive() || type == Object.class || type == Boolean.class || type == Byte.class || type == Character.class
    || type == Short.class || type == Integer.class || type == Long.class || type == Float.class || type == Double.class
    || type == String.class) {
    return type.getSimpleName();
  }
  return type.getName();
}

代码示例来源:origin: com.esotericsoftware.kryo/kryo

/** Returns false for all primitive wrappers. */
  public boolean useReferences (Class type) {
    return !Util.isWrapperClass(type);
  }
}

代码示例来源:origin: com.esotericsoftware/kryo

if (type.isPrimitive()) type = getWrapperClass(type);
boolean referencesSupported = referenceResolver.useReferences(type);
int id;
  id = input.readVarInt(true);
  if (id == Kryo.NULL) {
    if (TRACE || (DEBUG && depth == 1)) log("Read", null);
    readObject = null;
    return REF;
  if (TRACE) trace("kryo", "Read initial object reference " + id + ": " + className(type));
  readReferenceIds.add(id);
  return readReferenceIds.size;
if (DEBUG) debug("kryo", "Read object reference " + id + ": " + string(readObject));
return REF;

代码示例来源:origin: com.esotericsoftware/kryo

public Registration readClass (Input input) {
  int classID = input.readVarInt(true);
  switch (classID) {
  case Kryo.NULL:
    if (TRACE || (DEBUG && kryo.getDepth() == 1)) log("Read", null);
    return null;
  case NAME + 2: // Offset for NAME and NULL.
    return readName(input);
  }
  if (classID == memoizedClassId) return memoizedClassIdValue;
  Registration registration = idToRegistration.get(classID - 2);
  if (registration == null) throw new KryoException("Encountered unregistered class ID: " + (classID - 2));
  if (TRACE) trace("kryo", "Read class " + (classID - 2) + ": " + className(registration.getType()));
  memoizedClassId = classID;
  memoizedClassIdValue = registration;
  return registration;
}

代码示例来源:origin: com.esotericsoftware.kryo/kryo

public Registration register (Registration registration) {
  if (registration == null) throw new IllegalArgumentException("registration cannot be null.");
  if (registration.getId() != NAME) {
    if (TRACE) {
      trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType()) + " ("
        + registration.getSerializer().getClass().getName() + ")");
    }
    idToRegistration.put(registration.getId(), registration);
  } else if (TRACE) {
    trace("kryo", "Register class name: " + className(registration.getType()) + " ("
      + registration.getSerializer().getClass().getName() + ")");
  }
  classToRegistration.put(registration.getType(), registration);
  if (registration.getType().isPrimitive()) classToRegistration.put(getWrapperClass(registration.getType()), registration);
  return registration;
}

代码示例来源:origin: com.esotericsoftware/kryo-shaded

/** Writes an object using the specified serializer. The registered serializer is ignored. */
public void writeObject (Output output, Object object, Serializer serializer) {
  if (output == null) throw new IllegalArgumentException("output cannot be null.");
  if (object == null) throw new IllegalArgumentException("object cannot be null.");
  if (serializer == null) throw new IllegalArgumentException("serializer cannot be null.");
  beginObject();
  try {
    if (references && writeReferenceOrNull(output, object, false)) {
      serializer.setGenerics(this, null);
      return;
    }
    if (TRACE || (DEBUG && depth == 1)) log("Write", object);
    serializer.write(this, output, object);
  } finally {
    if (--depth == 0 && autoReset) reset();
  }
}

代码示例来源:origin: com.esotericsoftware/kryo

/** @param object May be null if mayBeNull is true.
 * @return true if no bytes need to be written for the object. */
boolean writeReferenceOrNull (Output output, Object object, boolean mayBeNull) {
  if (object == null) {
    if (TRACE || (DEBUG && depth == 1)) log("Write", null);
    output.writeVarInt(Kryo.NULL, true);
    return true;
  }
  if (!referenceResolver.useReferences(object.getClass())) {
    if (mayBeNull) output.writeVarInt(Kryo.NOT_NULL, true);
    return false;
  }
  // Determine if this object has already been seen in this object graph.
  int id = referenceResolver.getWrittenId(object);
  // If not the first time encountered, only write reference ID.
  if (id != -1) {
    if (DEBUG) debug("kryo", "Write object reference " + id + ": " + string(object));
    output.writeVarInt(id + 2, true); // + 2 because 0 and 1 are used for NULL and NOT_NULL.
    return true;
  }
  // Otherwise write NOT_NULL and then the object bytes.
  id = referenceResolver.addWrittenObject(object);
  output.writeVarInt(NOT_NULL, true);
  if (TRACE) trace("kryo", "Write initial object reference " + id + ": " + string(object));
  return false;
}

代码示例来源:origin: com.esotericsoftware/kryo

public Class read (Kryo kryo, Input input, Class<Class> type) {
    Registration registration = kryo.readClass(input);
    int isPrimitive = input.read();
    Class typ = registration != null ? registration.getType() : null;
    if (typ == null || !typ.isPrimitive()) return typ;
    return (isPrimitive == 1) ? typ : getWrapperClass(typ);
  }
}

代码示例来源:origin: com.esotericsoftware/kryo

final private void writeLittleEndianLong (long val) {
  if (isLittleEndian)
    writeLong(val);
  else
    writeLong(Util.swapLong(val));
}

代码示例来源:origin: com.esotericsoftware.kryo/kryo

final private void writeLittleEndianInt (int val) {
  if (isLittleEndian)
    writeInt(val);
  else
    writeInt(Util.swapInt(val));
}

代码示例来源:origin: com.esotericsoftware/kryo

/** Returns true if the specified type is final. Final types can be serialized more efficiently because they are
 * non-polymorphic.
 * <p>
 * This can be overridden to force non-final classes to be treated as final. Eg, if an application uses ArrayList extensively
 * but never uses an ArrayList subclass, treating ArrayList as final could allow FieldSerializer to save 1-2 bytes per
 * ArrayList field. */
public boolean isFinal (Class type) {
  if (type == null) throw new IllegalArgumentException("type cannot be null.");
  if (type.isArray()) return Modifier.isFinal(Util.getElementClass(type).getModifiers());
  return Modifier.isFinal(type.getModifiers());
}

代码示例来源:origin: com.esotericsoftware/kryo

/** Logs a message about an object. The log level and the string format of the object depend on the object type. */
static public void log (String message, Object object) {
  if (object == null) {
    if (TRACE) trace("kryo", message + ": null");
    return;
  }
  Class type = object.getClass();
  if (type.isPrimitive() || type == Boolean.class || type == Byte.class || type == Character.class || type == Short.class
    || type == Integer.class || type == Long.class || type == Float.class || type == Double.class || type == String.class) {
    if (TRACE) trace("kryo", message + ": " + string(object));
  } else {
    debug("kryo", message + ": " + string(object));
  }
}

代码示例来源:origin: com.esotericsoftware.kryo/kryo

if (type.isPrimitive()) type = getWrapperClass(type);
boolean referencesSupported = referenceResolver.useReferences(type);
int id;
  id = input.readVarInt(true);
  if (id == Kryo.NULL) {
    if (TRACE || (DEBUG && depth == 1)) log("Read", null);
    readObject = null;
    return REF;
  if (TRACE) trace("kryo", "Read initial object reference " + id + ": " + className(type));
  readReferenceIds.add(id);
  return readReferenceIds.size;
if (DEBUG) debug("kryo", "Read object reference " + id + ": " + string(readObject));
return REF;

代码示例来源:origin: com.esotericsoftware.kryo/kryo

public Registration readClass (Input input) {
  int classID = input.readVarInt(true);
  switch (classID) {
  case Kryo.NULL:
    if (TRACE || (DEBUG && kryo.getDepth() == 1)) log("Read", null);
    return null;
  case NAME + 2: // Offset for NAME and NULL.
    return readName(input);
  }
  if (classID == memoizedClassId) return memoizedClassIdValue;
  Registration registration = idToRegistration.get(classID - 2);
  if (registration == null) throw new KryoException("Encountered unregistered class ID: " + (classID - 2));
  if (TRACE) trace("kryo", "Read class " + (classID - 2) + ": " + className(registration.getType()));
  memoizedClassId = classID;
  memoizedClassIdValue = registration;
  return registration;
}

代码示例来源:origin: com.esotericsoftware/kryo

public Registration register (Registration registration) {
  if (registration == null) throw new IllegalArgumentException("registration cannot be null.");
  if (registration.getId() != NAME) {
    if (TRACE) {
      trace("kryo", "Register class ID " + registration.getId() + ": " + className(registration.getType()) + " ("
        + registration.getSerializer().getClass().getName() + ")");
    }
    idToRegistration.put(registration.getId(), registration);
  } else if (TRACE) {
    trace("kryo", "Register class name: " + className(registration.getType()) + " ("
      + registration.getSerializer().getClass().getName() + ")");
  }
  classToRegistration.put(registration.getType(), registration);
  if (registration.getType().isPrimitive()) classToRegistration.put(getWrapperClass(registration.getType()), registration);
  return registration;
}

代码示例来源:origin: com.esotericsoftware/kryo

/** Writes an object using the specified serializer. The registered serializer is ignored. */
public void writeObject (Output output, Object object, Serializer serializer) {
  if (output == null) throw new IllegalArgumentException("output cannot be null.");
  if (object == null) throw new IllegalArgumentException("object cannot be null.");
  if (serializer == null) throw new IllegalArgumentException("serializer cannot be null.");
  beginObject();
  try {
    if (references && writeReferenceOrNull(output, object, false)) {
      serializer.setGenerics(this, null);
      return;
    }
    if (TRACE || (DEBUG && depth == 1)) log("Write", object);
    serializer.write(this, output, object);
  } finally {
    if (--depth == 0 && autoReset) reset();
  }
}

代码示例来源:origin: com.esotericsoftware/kryo-shaded

/** @param object May be null if mayBeNull is true.
 * @return true if no bytes need to be written for the object. */
boolean writeReferenceOrNull (Output output, Object object, boolean mayBeNull) {
  if (object == null) {
    if (TRACE || (DEBUG && depth == 1)) log("Write", null);
    output.writeVarInt(Kryo.NULL, true);
    return true;
  }
  if (!referenceResolver.useReferences(object.getClass())) {
    if (mayBeNull) output.writeVarInt(Kryo.NOT_NULL, true);
    return false;
  }
  // Determine if this object has already been seen in this object graph.
  int id = referenceResolver.getWrittenId(object);
  // If not the first time encountered, only write reference ID.
  if (id != -1) {
    if (DEBUG) debug("kryo", "Write object reference " + id + ": " + string(object));
    output.writeVarInt(id + 2, true); // + 2 because 0 and 1 are used for NULL and NOT_NULL.
    return true;
  }
  // Otherwise write NOT_NULL and then the object bytes.
  id = referenceResolver.addWrittenObject(object);
  output.writeVarInt(NOT_NULL, true);
  if (TRACE) trace("kryo", "Write initial object reference " + id + ": " + string(object));
  return false;
}

代码示例来源:origin: com.esotericsoftware/kryo-shaded

public Class read (Kryo kryo, Input input, Class<Class> type) {
    Registration registration = kryo.readClass(input);
    int isPrimitive = input.read();
    Class typ = registration != null ? registration.getType() : null;
    if (typ == null || !typ.isPrimitive()) return typ;
    return (isPrimitive == 1) ? typ : getWrapperClass(typ);
  }
}

代码示例来源:origin: com.esotericsoftware/kryo

final private void writeLittleEndianLong (long val) {
  if (isLittleEndian)
    writeLong(val);
  else
    writeLong(Util.swapLong(val));
}

相关文章