/** * Serializes the value to bytes, stored in field valueBytes * @param value new vertex value */ private void setSerializedValue(V value) { UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(); try { value.write(bos); bos.close(); } catch (IOException ioe) { throw new RuntimeException("Could not serialize vertex value", ioe); } this.valueBytes = bos.toByteArray(); cachedValue = null; }
@Override public void write(int b) throws IOException { ensureSize(SIZE_OF_BYTE); buf[pos] = (byte) b; pos += SIZE_OF_BYTE; }
@Override public void skipBytes(int bytesToSkip) { ensureWritable(bytesToSkip); pos += bytesToSkip; }
@Override public void writeBytes(String s) throws IOException { // Note that this code is mostly copied from DataOutputStream int len = s.length(); ensureSize(len); for (int i = 0; i < len; i++) { int v = s.charAt(i); writeByte(v); } }
@Override public void writeChars(String s) throws IOException { // Note that this code is mostly copied from DataOutputStream int len = s.length(); ensureSize(len * SIZE_OF_CHAR); for (int i = 0; i < len; i++) { int v = s.charAt(i); writeChar(v); } }
/** * Create an extended data output (can be subclassed) * * @return ExtendedDataOutput object */ public ExtendedDataOutput createExtendedDataOutput() { if (useUnsafeSerialization) { return new UnsafeByteArrayOutputStream(); } else { return new ExtendedByteArrayDataOutput(); } }
UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); from.write(out); if (checkOverRead) { out.writeByte(0); new UnsafeByteArrayInputStream(out.getByteArray(), 0, out.getPos()); to.readFields(in);
/** * Serialize given writable to byte array, * using new instance of UnsafeByteArrayOutputStream. * * @param w Writable object * @return array of bytes * @param <T> Type of the object */ public static <T extends Writable> byte[] toByteArrayUnsafe(T w) { try { UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); w.write(out); return out.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
reusableOut.reset(); original.write(reusableOut); reusableIn.initialize( reusableOut.getByteArray(), 0, reusableOut.getPos()); copy.readFields(reusableIn);
ensureSize(utflen + SIZE_OF_SHORT); writeShort(utflen);
/** * Construct LongsDiffWriter * @param useUnsafeWriter use unsafe writer */ public LongsDiffWriter(boolean useUnsafeWriter) { if (useUnsafeWriter) { out = new UnsafeByteArrayOutputStream(); } else { out = new ExtendedByteArrayDataOutput(); } }
/** * Create an extended data output (can be subclassed) * * @param expectedSize Expected size * @return ExtendedDataOutput object */ public ExtendedDataOutput createExtendedDataOutput(int expectedSize) { if (useUnsafeSerialization) { return new UnsafeByteArrayOutputStream(expectedSize); } else { return new ExtendedByteArrayDataOutput(expectedSize); } }
@Override public void writeLong(long v) throws IOException { ensureSize(SIZE_OF_LONG); UNSAFE.putLong(buf, BYTE_ARRAY_OFFSET + pos, v); pos += SIZE_OF_LONG; }
/** * Create an extended data output (can be subclassed) * * @param buf Buffer to use for the output (reuse perhaps) * @param pos How much of the buffer is already used * @return ExtendedDataOutput object */ public ExtendedDataOutput createExtendedDataOutput(byte[] buf, int pos) { if (useUnsafeSerialization) { return new UnsafeByteArrayOutputStream(buf, pos); } else { return new ExtendedByteArrayDataOutput(buf, pos); } }
@Override public void writeShort(int v) throws IOException { ensureSize(SIZE_OF_SHORT); UNSAFE.putShort(buf, BYTE_ARRAY_OFFSET + pos, (short) v); pos += SIZE_OF_SHORT; }
private <W extends Writable> W copyWritable(W writable) { if (reusedOut == null) { reusedOut = new UnsafeByteArrayOutputStream(); } if (reusedIn == null) { reusedIn = new UnsafeReusableByteArrayInput(); } return WritableUtils.createCopy(reusedOut, reusedIn, writable, null); }
@Override public void writeInt(int v) throws IOException { ensureSize(SIZE_OF_INT); UNSAFE.putInt(buf, BYTE_ARRAY_OFFSET + pos, v); pos += SIZE_OF_INT; }
/** * Constructor * * Creates new instances of all reducers from * {@link WorkerAggregatorHandler} */ public ThreadLocalWorkerGlobalCommUsage() { threadReducerMap = Maps.newHashMapWithExpectedSize( WorkerAggregatorHandler.this.reducerMap.size()); UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); UnsafeReusableByteArrayInput in = new UnsafeReusableByteArrayInput(); for (Entry<String, Reducer<Object, Writable>> entry : reducerMap.entrySet()) { ReduceOperation<Object, Writable> globalReduceOp = entry.getValue().getReduceOp(); ReduceOperation<Object, Writable> threadLocalCopy = WritableUtils.createCopy(out, in, globalReduceOp, conf); threadReducerMap.put(entry.getKey(), new Reducer<>(threadLocalCopy)); } }
@Override public void write(byte[] b) throws IOException { ensureSize(b.length); System.arraycopy(b, 0, buf, pos, b.length); pos += b.length; }
/** * Write object to a byte array with the first 4 bytes as the size of the * entire buffer (including the size). * * @param writableObject Object to write from. * @param buffer Use this buffer instead * @param unsafe Use unsafe serialization? * @return Byte array with serialized object. */ public static byte[] writeToByteArrayWithSize(Writable writableObject, byte[] buffer, boolean unsafe) { ExtendedDataOutput extendedDataOutput; if (unsafe) { extendedDataOutput = new UnsafeByteArrayOutputStream(buffer); } else { extendedDataOutput = new ExtendedByteArrayDataOutput(buffer); } try { extendedDataOutput.writeInt(-1); writableObject.write(extendedDataOutput); extendedDataOutput.writeInt(0, extendedDataOutput.getPos()); } catch (IOException e) { throw new IllegalStateException("writeToByteArrayWithSize: " + "IOException", e); } return extendedDataOutput.getByteArray(); }