Tabnine Logo
Schema
Code IndexAdd Tabnine to your IDE (free)

How to use
Schema
in
io.protostuff

Best Java code snippets using io.protostuff.Schema (Showing top 20 results out of 369)

Refine searchRefine arrow

  • LinkedBuffer
  • ByteArrayInputStream
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;
}
io.protostuffSchema

Javadoc

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.

Most used methods

  • newMessage
  • writeTo
  • mergeFrom
  • isInitialized
  • getFieldName
  • typeClass
  • getFieldNumber
  • messageFullName
  • messageName
    Returns the simple name of the message tied to this schema. Allows custom schemas to provide a custo

Popular in Java

  • Start an intent from android
  • onCreateOptionsMenu (Activity)
  • getSystemService (Context)
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • Thread (java.lang)
    A thread is a thread of execution in a program. The Java Virtual Machine allows an application to ha
  • SortedSet (java.util)
    SortedSet is a Set which iterates over its elements in a sorted order. The order is determined eithe
  • UUID (java.util)
    UUID is an immutable representation of a 128-bit universally unique identifier (UUID). There are mul
  • ExecutorService (java.util.concurrent)
    An Executor that provides methods to manage termination and methods that can produce a Future for tr
  • Reference (javax.naming)
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now