io.protostuff.Schema类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(202)

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

Schema介绍

[英]Handles the serialization and deserialization of a message/object tied to this.

Basically, any object can be serialized via protobuf. As long as its schema is provided, it does not need to implement Message. This was designed with "unobtrusive" in mind. The goal was to be able to serialize/deserialize any existing object without having to touch its source. This will enable you to customize the serialization of objects from 3rd party libraries.
[中]处理与此关联的消息/对象的序列化和反序列化。
基本上,任何对象都可以通过protobuf序列化。只要提供了它的模式,它就不需要实现消息。这是为了“低调”而设计的。目标是能够序列化/反序列化任何现有对象,而不必接触其源。这将使您能够自定义第三方库中对象的序列化。

代码示例

代码示例来源:origin: protostuff/protostuff

private <T> T mergeObjectEncodedAsGroup(T value, final Schema<T> schema) throws IOException
{
  if (value == null)
    value = schema.newMessage();
  schema.mergeFrom(this, value);
  if (!schema.isInitialized(value))
    throw new UninitializedMessageException(value, schema);
  // handling is in #readFieldNumber
  checkLastTagWas(0);
  return value;
}

代码示例来源:origin: protostuff/protostuff

/**
 * Serializes the {@code message} into an {@link OutputStream} using the given schema.
 * 
 * @return the size of the message
 */
public static <T> int writeTo(OutputStream out, T message, Schema<T> schema,
    LinkedBuffer buffer) throws IOException
{
  if (buffer.start != buffer.offset)
    throw new IllegalArgumentException("Buffer previously used and had not been reset.");
  final ProtobufOutput output = new ProtobufOutput(buffer);
  schema.writeTo(output, message);
  return LinkedBuffer.writeTo(out, buffer);
}

代码示例来源:origin: protostuff/protostuff

@Override
public String getFieldName(int number)
{
  return schema.getFieldName(number);
}

代码示例来源:origin: protostuff/protostuff

@Override
public void mergeFrom(Input input, final Object message) throws IOException
{
  final Schema<Object> schema = lastSchema;
  // merge using this input.
  schema.mergeFrom(this, message);
  if (!schema.isInitialized(message))
    throw new UninitializedMessageException(message, schema);
  // restore
  lastSchema = schema;
}

代码示例来源:origin: protostuff/protostuff

/**
 * Serializes the {@code message} into an {@link XMLStreamWriter} using the given {@code schema}.
 */
public static <T> void writeTo(XMLStreamWriter writer, T message, Schema<T> schema)
    throws IOException, XMLStreamException, XmlOutputException
{
  writer.writeStartElement(schema.messageName());
  schema.writeTo(new XmlOutput(writer, schema), message);
  writer.writeEndElement();
}

代码示例来源:origin: protostuff/protostuff

public void testImmutablePojo() throws Exception
{
  Schema<ImmutablePojo> schema = RuntimeSchema
      .getSchema(ImmutablePojo.class);
  ImmutablePojo p = new ImmutablePojo(3, "ip");
  byte[] data = ProtostuffIOUtil.toByteArray(p, schema, buf());
  ImmutablePojo p2 = schema.newMessage();
  ProtostuffIOUtil.mergeFrom(data, 0, data.length, p2, schema);
  assertEquals(p, p2);
  List<ImmutablePojo> list = new ArrayList<ImmutablePojo>();
  list.add(p);
  list.add(p2);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  ProtostuffIOUtil.writeListTo(out, list, schema, buf());
  byte[] listData = out.toByteArray();
  ByteArrayInputStream in = new ByteArrayInputStream(listData);
  List<ImmutablePojo> parsedList = ProtostuffIOUtil.parseListFrom(in,
      schema);
  assertEquals(list, parsedList);
}

代码示例来源:origin: protostuff/protostuff

@Override
protected <T> void mergeFrom(byte[] data, int offset, int length,
    T message, Schema<T> schema) throws IOException
{
  ByteArrayInputStream in = new ByteArrayInputStream(data, offset, length);
  try
  {
    schema.mergeFrom(new KvpInput(in, numeric), message);
  }
  catch (ArrayIndexOutOfBoundsException e)
  {
    throw new ProtostuffException("Truncated message.", e);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public Object readObject() throws IOException, ClassNotFoundException {
  int classNameLength = dis.readInt();
  int bytesLength = dis.readInt();
  if (classNameLength < 0 || bytesLength < 0) {
    throw new IOException();
  }
  byte[] classNameBytes = new byte[classNameLength];
  dis.readFully(classNameBytes, 0, classNameLength);
  byte[] bytes = new byte[bytesLength];
  dis.readFully(bytes, 0, bytesLength);
  String className = new String(classNameBytes);
  Class clazz = Class.forName(className);
  Object result;
  if (WrapperUtils.needWrapper(clazz)) {
    Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
    Wrapper wrapper = schema.newMessage();
    GraphIOUtil.mergeFrom(bytes, wrapper, schema);
    result = wrapper.getData();
  } else {
    Schema schema = RuntimeSchema.getSchema(clazz);
    result = schema.newMessage();
    GraphIOUtil.mergeFrom(bytes, result, schema);
  }
  return result;
}

代码示例来源:origin: protostuff/protostuff

@Test
public void forceUseSunReflectionFactory() throws Exception {
  System.setProperty("protostuff.runtime.always_use_sun_reflection_factory", "true");
  Schema<MyClass> schema = RuntimeSchema.getSchema(MyClass.class);
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  MyClass myClass = new MyClass(); // constructor initializes list with one element
  ProtostuffIOUtil.writeTo(output, myClass, schema, LinkedBuffer.allocate());
  byte[] bytes = output.toByteArray();
  Assert.assertEquals(1, myClass.getList().size());
  MyClass myClassNew = schema.newMessage(); // default constructor should not be used
  ProtostuffIOUtil.mergeFrom(bytes, myClassNew, schema);
  Assert.assertEquals(1, myClassNew.getList().size());
}

代码示例来源:origin: protostuff/protostuff

private void deserTest(Message origMsg,
    Schema sch,
    ByteBuffer buf) throws IOException
{
  ByteBufferInput input = new ByteBufferInput(buf, false);
  Object newM = sch.newMessage();
  sch.mergeFrom(input, newM);
  assertEquals(origMsg, newM);
}

代码示例来源:origin: protostuff/protostuff

/**
 * Used by the code generated messages that implement {@link java.io.Externalizable}. Writes to the
 * {@link DataOutput} .
 * 
 * @return the size of the message.
 */
public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema)
    throws IOException
{
  final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.MIN_BUFFER_SIZE);
  final ProtostuffOutput output = new ProtostuffOutput(buffer);
  schema.writeTo(output, message);
  ProtobufOutput.writeRawVarInt32Bytes(out, output.size);
  LinkedBuffer.writeTo(out, buffer);
  return output.size;
}

代码示例来源:origin: protostuff/protostuff

@Override
public void writeTo(Output output, T message) throws IOException
{
  schema.writeTo(output, message);
}

代码示例来源:origin: protostuff/protostuff

@Override
public void mergeFrom(Input input, T message) throws IOException
{
  schema.mergeFrom(input, message);
}

代码示例来源:origin: protostuff/protostuff

static <T> byte[] toByteArrayBufferedProtostuff(T message, Schema<T> schema)
{
  final ProtostuffOutput output = new ProtostuffOutput(new LinkedBuffer(BUF_SIZE));
  try
  {
    schema.writeTo(output, message);
  }
  catch (IOException e)
  {
    throw new RuntimeException("Serializing to a byte array threw an IOException " +
        "(should never happen).", e);
  }
  return output.toByteArray();
}

代码示例来源:origin: protostuff/protostuff

@Override
public Class<? super T> typeClass()
{
  return schema.typeClass();
}

代码示例来源:origin: protostuff/protostuff

@Override
public boolean isInitialized(T message)
{
  return schema.isInitialized(message);
}

代码示例来源:origin: protostuff/protostuff

public void testFoo() throws Exception
{
  Schema<Foo> schema = Foo.getSchema();
  Foo message = SerializableObjects.foo;
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  int size = optWriteDelimitedTo(out, message, schema, buf());
  int delimSize = ProtobufOutput.computeRawVarint32Size(size);
  byte[] data = out.toByteArray();
  int expectedSize = size + delimSize;
  assertEquals(expectedSize, data.length);
  verifyOptData(data, message, schema, buf());
  ByteArrayInputStream in = new ByteArrayInputStream(data);
  Foo parsedMessage = schema.newMessage();
  boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf(512));
  assertTrue(merged);
  assertEquals(message, parsedMessage);
}

代码示例来源:origin: apache/incubator-dubbo

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public Object readObject() throws IOException, ClassNotFoundException {
  int classNameLength = dis.readInt();
  int bytesLength = dis.readInt();
  if (classNameLength < 0 || bytesLength < 0) {
    throw new IOException();
  }
  byte[] classNameBytes = new byte[classNameLength];
  dis.readFully(classNameBytes, 0, classNameLength);
  byte[] bytes = new byte[bytesLength];
  dis.readFully(bytes, 0, bytesLength);
  String className = new String(classNameBytes);
  Class clazz = Class.forName(className);
  Object result;
  if (WrapperUtils.needWrapper(clazz)) {
    Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
    Wrapper wrapper = schema.newMessage();
    GraphIOUtil.mergeFrom(bytes, wrapper, schema);
    result = wrapper.getData();
  } else {
    Schema schema = RuntimeSchema.getSchema(clazz);
    result = schema.newMessage();
    GraphIOUtil.mergeFrom(bytes, result, schema);
  }
  return result;
}

代码示例来源:origin: protostuff/protostuff

private void deserTest(Message origMsg,
    Schema sch,
    ByteBuffer buf) throws IOException
{
  ByteBufferInput input = new ByteBufferInput(buf, true);
  Object newM = sch.newMessage();
  sch.mergeFrom(input, newM);
  assertEquals(origMsg, newM);
}

代码示例来源:origin: protostuff/protostuff

/**
 * Used by the code generated messages that implement {@link java.io.Externalizable}. Writes to the
 * {@link DataOutput} .
 * 
 * @return the size of the message.
 */
public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema)
    throws IOException
{
  final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.MIN_BUFFER_SIZE);
  final ProtostuffOutput output = new ProtostuffOutput(buffer);
  final GraphProtostuffOutput graphOutput = new GraphProtostuffOutput(output);
  schema.writeTo(graphOutput, message);
  ProtobufOutput.writeRawVarInt32Bytes(out, output.size);
  LinkedBuffer.writeTo(out, buffer);
  return output.size;
}

相关文章