switch (JsonType.values()[in.readIndex()]) { case LONG: return new LongNode(in.readLong()); case DOUBLE: return new DoubleNode(in.readDouble()); case STRING: return new TextNode(in.readString()); case BOOLEAN: return in.readBoolean() ? BooleanNode.TRUE : BooleanNode.FALSE; case NULL: in.readNull(); return NullNode.getInstance(); case ARRAY: ArrayNode array = JsonNodeFactory.instance.arrayNode(); for (long l = in.readArrayStart(); l > 0; l = in.arrayNext()) for (long i = 0; i < l; i++) array.add(read(in)); case OBJECT: ObjectNode object = JsonNodeFactory.instance.objectNode(); for (long l = in.readMapStart(); l > 0; l = in.mapNext()) for (long i = 0; i < l; i++) object.put(in.readString(), read(in)); return object; default:
break; case ENUM: in.readInt(); break; case ARRAY: Schema elementType = schema.getElementType(); for (long l = in.skipArray(); l > 0; l = in.skipArray()) { for (long i = 0; i < l; i++) { skip(elementType, in); case MAP: Schema value = schema.getValueType(); for (long l = in.skipMap(); l > 0; l = in.skipMap()) { for (long i = 0; i < l; i++) { in.skipString(); skip(value, in); skip(schema.getTypes().get((int)in.readIndex()), in); break; case FIXED: in.skipFixed(schema.getFixedSize()); break; case STRING: in.skipString(); break; case BYTES: in.skipBytes(); break; case INT: in.readInt(); break;
@Override public String readString() throws IOException { Symbol actual = parser.advance(Symbol.STRING); if (actual == Symbol.BYTES) { return new String(in.readBytes(null).array(), UTF8); } else { assert actual == Symbol.STRING; return in.readString(); } }
@Override public long readLong() throws IOException { Symbol actual = parser.advance(Symbol.LONG); if (actual == Symbol.INT) { return in.readInt(); } else if (actual == Symbol.DOUBLE) { return (long) in.readDouble(); } else { assert actual == Symbol.LONG; return in.readLong(); } }
@Override public double readDouble() throws IOException { Symbol actual = parser.advance(Symbol.DOUBLE); if (actual == Symbol.INT) { return (double) in.readInt(); } else if (actual == Symbol.LONG) { return (double) in.readLong(); } else if (actual == Symbol.FLOAT) { return (double) in.readFloat(); } else { assert actual == Symbol.DOUBLE; return in.readDouble(); } }
switch (c) { case 'N': vi.readNull(); break; case 'B': assertEquals(values[p++], vi.readBoolean()); break; case 'I': assertEquals(values[p++], vi.readInt()); break; case 'L': assertEquals(values[p++], vi.readLong()); break; case 'F': if (!(values[p] instanceof Float)) fail(); float f = (Float) values[p++]; assertEquals(f, vi.readFloat(), Math.abs(f / 1000)); break; case 'D': if (!(values[p] instanceof Double)) fail(); double d = (Double) values[p++]; assertEquals(d, vi.readDouble(), Math.abs(d / 1000)); break; case 'S': extractInt(cs); if (level == skipLevel) { vi.skipString(); p++; } else {
return decoder.readInt(); case FLOAT: return Float.floatToIntBits(decoder.readFloat()); case LONG: { long l = decoder.readLong(); return (int)(l^(l>>>32)); long l = Double.doubleToLongBits(decoder.readDouble()); return (int)(l^(l>>>32)); Schema elementType = schema.getElementType(); int hashCode = 1; for (long l = decoder.readArrayStart(); l != 0; l = decoder.arrayNext()) for (long i = 0; i < l; i++) hashCode = hashCode*31 + hashCode(data, elementType); throw new AvroRuntimeException("Can't hashCode maps!"); case UNION: return hashCode(data, schema.getTypes().get(decoder.readInt())); case FIXED: return hashBytes(1, data, schema.getFixedSize(), false); case STRING: return hashBytes(0, data, decoder.readInt(), false); case BYTES: return hashBytes(1, data, decoder.readInt(), true); case BOOLEAN: return decoder.readBoolean() ? 1231 : 1237; case NULL: return 0;
} else { countStack.push(new S(count, true)); count = input.readArrayStart(); continue; input.skipBytes(); } else { String s = parser.getText(); } else { countStack.push(new S(count, false)); count = input.readMapStart(); if (count < 0) { count = -count; input.readLong(); // byte count count = countStack.peek().isArray ? input.arrayNext() : input.mapNext();
@Override void readInternal(Decoder d) throws IOException { Utf8 key = new Utf8(); for (long i = d.readMapStart(); i != 0; i = d.mapNext()) { for (long j = 0; j < i; j++) { key = d.readString(key); d.readFloat(); d.readFloat(); d.readFloat(); d.readFloat(); } } }
@Override void readInternal(Decoder d) throws IOException { d.readArrayStart(); for (long i = d.readArrayStart(); i != 0; i = d.arrayNext()) { for (long j = 0; j < i; j++) { d.readFloat(); d.readFloat(); d.readFloat(); d.readFloat(); } } d.arrayNext(); }
/** Called to read strings. Subclasses may override to use a different * string representation. By default, this calls {@link * Decoder#readString(Utf8)}.*/ protected Object readString(Object old, Decoder in) throws IOException { return in.readString(old instanceof Utf8 ? (Utf8)old : null); }
/** Called to read integers. Subclasses may override to use a different * integer representation. By default, this calls {@link * Decoder#readInt()}.*/ protected Object readInt(Object old, Schema expected, Decoder in) throws IOException { return in.readInt(); }
@Override @SuppressWarnings("unchecked") protected Map<String, Serializable> read(Object o, Decoder decoder) throws IOException { int index = decoder.readIndex(); if (index == NULL_SCHEMA_INDEX) { decoder.readNull(); return emptyMap(); } else if (index == STRING_SCHEMA_INDEX) { String mapAsJson = decoder.readString(); return new ObjectMapper().readValue(mapAsJson, Map.class); } else { throw new IOException("Unable to read Map as Json as String, index=" + index + " is unknown"); } } }
@Override protected Instant read(Object o, Decoder decoder) throws IOException { int index = decoder.readIndex(); if (index == NULL_SCHEMA_INDEX) { decoder.readNull(); return null; } else if (index == LONG_SCHEMA_INDEX) { return Instant.ofEpochMilli(decoder.readLong()); } else { throw new IOException("Unable to read Instant as long, index=" + index + " is unknown"); } } }
@Override public int readIndex() throws IOException { parser.advance(Symbol.UNION); Symbol.Alternative top = (Symbol.Alternative) parser.popSymbol(); int result = in.readIndex(); parser.pushSymbol(top.getSymbol(result)); return result; }
@Override void readInternal(Decoder d) throws IOException { for (int i = 0; i < count; i++) { d.readDouble(); d.readDouble(); d.readDouble(); d.readInt(); d.readInt(); d.readInt(); } } @Override
public Object fromBytes(Schema schema, byte[] data) throws IOException { Schema fromSchema = null; if (schema.getType() == Type.UNION) { try { Decoder decoder = DecoderFactory.get().binaryDecoder(data, null); int unionIndex = decoder.readIndex(); List<Schema> possibleTypes = schema.getTypes(); fromSchema = possibleTypes.get(unionIndex); Schema effectiveSchema = possibleTypes.get(unionIndex); if (effectiveSchema.getType() == Type.NULL) { decoder.readNull(); return null; } else { data = decoder.readBytes(null).array(); } } catch (IOException e) { LOG.error(e.getMessage()); throw new GoraException("Error decoding union type: ", e); } } else { fromSchema = schema; } return fromBytes(encoder, fromSchema, data); }
@Test(expected = EOFException.class) public void testEOF() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Encoder e = EncoderFactory.get().binaryEncoder(baos, null); e.writeLong(0x10000000000000L); e.flush(); Decoder d = newDecoder(new ByteArrayInputStream(baos.toByteArray())); Assert.assertEquals(0x10000000000000L, d.readLong()); d.readInt(); }