return readUTF(nonNullValue, offset, len);
public void testInt() throws Exception { for (int i : int_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeInt(i, session, lb); LinkedBuffer.writeTo(out, lb); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); LinkedBuffer lb2 = new LinkedBuffer(NUM_BUF_SIZE); WriteSession session2 = new WriteSession(lb2, out2); StreamedStringSerializer.writeInt(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Integer.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
public void testReallyLongString() throws Exception { LinkedBuffer lb = new LinkedBuffer(256); WriteSession session = new WriteSession(lb); // The motivation of this test is to make sure // that the serializer/deserializer can handle very large Strings. // DataInputStream only supports Strings up to Short.MAX_VALUE, // so we should make sure our implementation can support more than that. // // Ideally, we'd like to test all the way up to Integer.MAX_VALUE, but that // may be unfeasable in many test environments, so we will just do 3 * Short.MAX_VALUE, // which would overflow an unsigned short. StringBuilder sb = new StringBuilder(3 * Short.MAX_VALUE); for (int i = 0; i < 3 * Short.MAX_VALUE; i++) { sb.append(i % 10); } String bigString = sb.toString(); StringSerializer.writeUTF8(bigString, session, lb); byte[] buffered = session.toByteArray(); // We want to make sure it's our implementation // that can handle the large string assertEquals(bigString, STRING.deserCustomOnly(buffered)); }
byteBuf.readBytes(bytes, 0, length); return STRING.deser(bytes, 0, length);
@Override public String readString() throws IOException { if (SerializationConstants.INCOMPATIBLE_FAST_STRING_FORMAT) { return readStringFast(); } final int length = readRawVarint32(); if (length < 0) { throw negativeSize(); } if (byteBuf.readableBytes() < length) { throw misreportedSize(); } byte[] bytes = ThreadLocalBytes.current(); byteBuf.readBytes(bytes, 0, length); return STRING.deser(bytes, 0, length); }
public String readString() throws IOException { int length = this.readRawVarint32(); if (length < 0) { throw new ProtobufException("CodedInput encountered an embedded string or message which claimed to have negative size."); } else { byte[] tmp = new byte[length]; this.buffer.readBytes(tmp, 0, tmp.length); return StringSerializer.STRING.deser(tmp); } }
@Override public String readString() throws IOException { final int length = readRawVarint32(); if (length < 0) { throw ProtobufException.negativeSize(); } if (offset + length > limit) { throw ProtobufException.misreportedSize(); } final int offset = this.offset; this.offset += length; return STRING.deser(buffer, offset, length); }
assertEquals(surrogatePairs, STRING.deserCustomOnly(fastPathBuffered)); assertEquals(surrogatePairs, STRING.deserCustomOnly(legacySurrogatePairSerialized)); assertEquals(surrogatePairs, STRING.deserCustomOnly(nativeSurrogatePairsSerialized)); assertEquals(surrogatePairs, STRING.deser(nativeSurrogatePairsSerialized));
if (digit == -1) throw new NumberFormatException(STRING.deser(buffer, start, length)); throw new NumberFormatException(STRING.deser(buffer, start, length)); throw new NumberFormatException(STRING.deser(buffer, start, length)); if (result < 0) throw new NumberFormatException(STRING.deser(buffer, start, length));
if (digit == -1) throw new NumberFormatException(STRING.deser(buffer, start, length)); throw new NumberFormatException(STRING.deser(buffer, start, length)); throw new NumberFormatException(STRING.deser(buffer, start, length)); if (result < 0) throw new NumberFormatException(STRING.deser(buffer, start, length));
assertEquals(surrogatePairs, STRING.deserCustomOnly(buffered)); assertEquals(surrogatePairs, STRING.deserCustomOnly(legacySurrogatePairSerialized)); assertEquals(surrogatePairs, STRING.deserCustomOnly(nativeSurrogatePairsSerialized)); assertEquals(surrogatePairs, STRING.deser(nativeSurrogatePairsSerialized));
@Override public String readString() throws IOException { if (offset + 2 > limit && !readable(2)) throw new ProtostuffException("Truncated message."); final int size = buffer[offset++] | (buffer[offset++] << 8); if (size == 0) return ByteString.EMPTY_STRING; if (size > MAX_VALUE_SIZE) throw new ProtostuffException("Exceeded kvp max value size."); if (offset + size > limit) { if (size > buffer.length) { // need to create a copy. return STRING.deser(fill(new byte[size], 0, size)); } // it can fit in the buffer. if (!readable(size)) throw new ProtostuffException("Truncated Message."); // final String str = STRING.deser(buffer, offset, size); // offset += size; // return str; } final String str = STRING.deser(buffer, offset, size); offset += size; return str; }
@Override protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema) throws IOException { byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf()); ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf); byte[] protostuff = ProtostuffIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, buf()); byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf()); assertTrue(protostuff.length == protostuffFromStream.length); assertEquals(STRING.deser(protostuff), STRING.deser(protostuffFromStream)); ByteArrayInputStream protostuffStream = new ByteArrayInputStream( protostuff); byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, buf()); byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf()); assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length); String strProtobufRoundTrip = STRING.deser(protobufRoundTrip); assertEquals(strProtobufRoundTrip, STRING.deser(protobufRoundTripFromStream)); assertTrue(protobufRoundTrip.length == protobuf.length); assertEquals(strProtobufRoundTrip, STRING.deser(protobuf)); }
@Override protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema) throws IOException { byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf()); ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf); byte[] protostuff = ProtostuffIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, buf()); byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf()); assertTrue(protostuff.length == protostuffFromStream.length); assertEquals(STRING.deser(protostuff), STRING.deser(protostuffFromStream)); ByteArrayInputStream protostuffStream = new ByteArrayInputStream( protostuff); byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, buf()); byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf()); assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length); String strProtobufRoundTrip = STRING.deser(protobufRoundTrip); assertEquals(strProtobufRoundTrip, STRING.deser(protobufRoundTripFromStream)); assertTrue(protobufRoundTrip.length == protobuf.length); assertEquals(strProtobufRoundTrip, STRING.deser(protobuf)); }
public void testFoo() throws Exception { Bar bar = new Bar(); bar.setSomeInt(1); bar.setSomeBytes(ByteString.copyFromUtf8(ESCAPE_TARGET)); bar.setSomeString(ESCAPE_TARGET); ArrayList<Bar> bars = new ArrayList<Bar>(); bars.add(bar); bars.add(bar); ArrayList<String> strings = new ArrayList<String>(); strings.add(ESCAPE_TARGET); strings.add(""); strings.add(ESCAPE_TARGET); Foo foo = new Foo(); foo.setSomeBar(bars); foo.setSomeString(strings); byte[] protostuff = ProtostuffIOUtil.toByteArray(foo, foo.cachedSchema(), buf()); byte[] json = JsonIOUtil.toByteArray(ProtostuffIOUtil.newPipe(protostuff), Foo.getPipeSchema(), false); byte[] json2 = JsonXIOUtil.toByteArray(ProtostuffIOUtil.newPipe(protostuff), Foo.getPipeSchema(), false, buf()); assertTrue(json.length == json2.length); String strJson = STRING.deser(json); String strJson2 = STRING.deser(json2); assertEquals(strJson, strJson2); System.err.println(strJson); System.err.println(strJson2); }
@Override protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema) throws Exception { byte[] json = JsonXIOUtil.toByteArray(message, schema, isNumeric(), buf()); ByteArrayInputStream jsonStream = new ByteArrayInputStream(json); byte[] protostuff = ProtostuffIOUtil.toByteArray( JsonIOUtil.newPipe(json, 0, json.length, isNumeric()), pipeSchema, buf()); byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray( JsonIOUtil.newPipe(jsonStream, isNumeric()), pipeSchema, buf()); assertTrue(Arrays.equals(protostuff, protostuffFromStream)); T parsedMessage = schema.newMessage(); ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema); SerializableObjects.assertEquals(message, parsedMessage); ByteArrayInputStream protostuffStream = new ByteArrayInputStream(protostuff); byte[] jsonRoundTrip = JsonXIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, isNumeric(), buf()); byte[] jsonRoundTripFromStream = JsonXIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, isNumeric(), buf()); assertTrue(jsonRoundTrip.length == jsonRoundTripFromStream.length); String strJsonRoundTrip = STRING.deser(jsonRoundTrip); assertEquals(strJsonRoundTrip, STRING.deser(jsonRoundTripFromStream)); assertTrue(jsonRoundTrip.length == json.length); assertEquals(strJsonRoundTrip, STRING.deser(json)); }