/** * Construct LongsDiffReader * * @param compressedData Input byte array * @param useUnsafeReader use unsafe reader */ public LongsDiffReader(byte[] compressedData, boolean useUnsafeReader) { if (useUnsafeReader) { input = new UnsafeByteArrayInputStream(compressedData); } else { input = new ExtendedByteArrayDataInput(compressedData); } }
new UnsafeByteArrayInputStream(out.getByteArray(), 0, out.getPos()); to.readFields(in); if (in.available() != (checkOverRead ? 1 : 0)) { throw new RuntimeException( "Serialization encountered issues with " + from.getClass() + ", " + (in.available() - (checkOverRead ? 1 : 0)) + " fewer bytes read");
@Override public void write(DataOutput output) throws IOException { super.write(output); output.writeInt(vertexMap.size()); for (Map.Entry<I, byte[]> entry : vertexMap.entrySet()) { progress(); entry.getKey().write(output); // Note here that we are writing the size of the vertex data first // as it is encoded in the first four bytes of the byte[] int vertexDataSize; if (useUnsafeSerialization) { vertexDataSize = UnsafeByteArrayInputStream.getInt(entry.getValue(), 0); } else { vertexDataSize = Ints.fromByteArray(entry.getValue()); } output.writeInt(vertexDataSize); output.write(entry.getValue(), 0, vertexDataSize); } }
/** * Create an extended data input (can be subclassed) * * @param buf Buffer to use for the input * @return ExtendedDataInput object */ public ExtendedDataInput createExtendedDataInput(byte[] buf) { if (useUnsafeSerialization) { return new UnsafeByteArrayInputStream(buf); } else { return new ExtendedByteArrayDataInput(buf); } }
/** * Create an extended data input (can be subclassed) * * @param buf Buffer to use for the input * @param off Where to start reading in the buffer * @param length Maximum length of the buffer * @return ExtendedDataInput object */ public ExtendedDataInput createExtendedDataInput( byte[] buf, int off, int length) { if (useUnsafeSerialization) { return new UnsafeByteArrayInputStream(buf, off, length); } else { return new ExtendedByteArrayDataInput(buf, off, length); } }
@Override public V getValue() { if (cachedValue != null) { return cachedValue; // Return always same instance } DataInput dis = new UnsafeByteArrayInputStream(valueBytes); cachedValue = getConf().createVertexValue(); try { cachedValue.readFields(dis); } catch (IOException ioe) { throw new RuntimeException("Could not deserialize vertex value", ioe); } // Forget the serialized data, because we have cached the object valueBytes = null; return cachedValue; }
/** * Read fields from byteArray to a Writeable object, skipping the size. * Serialization method is choosable * * @param byteArray Byte array to find the fields in. * @param writableObject Object to fill in the fields. * @param unsafe Use unsafe deserialization */ public static void readFieldsFromByteArrayWithSize( byte[] byteArray, Writable writableObject, boolean unsafe) { ExtendedDataInput extendedDataInput; if (unsafe) { extendedDataInput = new UnsafeByteArrayInputStream(byteArray); } else { extendedDataInput = new ExtendedByteArrayDataInput(byteArray); } try { extendedDataInput.readInt(); writableObject.readFields(extendedDataInput); } catch (IOException e) { throw new IllegalStateException( "readFieldsFromByteArrayWithSize: IOException", e); } }
ExtendedDataInput extendedDataInput; if (unsafe) { extendedDataInput = new UnsafeByteArrayInputStream(byteArray); } else { extendedDataInput = new ExtendedByteArrayDataInput(byteArray);