List of usage examples for org.apache.hadoop.io Writable write
void write(DataOutput out) throws IOException;
out
. From source file:org.apache.giraph.comm.aggregators.AggregatedValueOutputStream.java
License:Apache License
/** * Write aggregator to the stream and increment internal counter * * @param aggregatorName Name of the aggregator * @param aggregatedValue Value of aggregator * @return Number of bytes occupied by the stream * @throws IOException// w w w .j ava 2 s .c om */ public int addAggregator(String aggregatorName, Writable aggregatedValue) throws IOException { incrementCounter(); dataOutput.writeUTF(aggregatorName); aggregatedValue.write(dataOutput); return getSize(); }
From source file:org.apache.giraph.comm.aggregators.AggregatorOutputStream.java
License:Apache License
/** * Write aggregator to the stream and increment internal counter * * @param aggregatorName Name of the aggregator * @param aggregatorClass Class of aggregator * @param aggregatedValue Value of aggregator * @return Number of bytes occupied by the stream * @throws IOException/* w ww . j a va 2s .c o m*/ */ public int addAggregator(String aggregatorName, Class<? extends Aggregator> aggregatorClass, Writable aggregatedValue) throws IOException { incrementCounter(); dataOutput.writeUTF(aggregatorName); dataOutput.writeUTF(aggregatorClass.getName()); aggregatedValue.write(dataOutput); return getSize(); }
From source file:org.apache.giraph.comm.aggregators.GlobalCommValueOutputStream.java
License:Apache License
/** * Write global communication object to the stream * and increment internal counter/* w w w . j a va2 s .c o m*/ * * @param name Name * @param type Global communication type * @param value Object value * @return Number of bytes occupied by the stream * @throws IOException */ public int addValue(String name, GlobalCommType type, Writable value) throws IOException { incrementCounter(); dataOutput.writeUTF(name); dataOutput.writeByte(type.ordinal()); if (writeClass) { WritableUtils.writeWritableObject(value, dataOutput); } else { value.write(dataOutput); } return getSize(); }
From source file:org.apache.giraph.utils.WritableUtils.java
License:Apache License
/** * Write object to a byte array./*from www . j av a2s .c om*/ * * @param writableObjects Objects to write from. * @return Byte array with serialized object. */ public static byte[] writeToByteArray(Writable... writableObjects) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutput output = new DataOutputStream(outputStream); try { for (Writable writableObject : writableObjects) { writableObject.write(output); } } catch (IOException e) { throw new IllegalStateException("writeToByteArray: IOStateException", e); } return outputStream.toByteArray(); }
From source file:org.apache.giraph.utils.WritableUtils.java
License:Apache License
/** * Write object to a byte array with the first 4 bytes as the size of the * entire buffer (including the size).//from w w w.ja v a2 s. co m * * @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(); }
From source file:org.apache.giraph.utils.WritableUtils.java
License:Apache License
/** * Write list of object to a byte array. * * @param writableList List of object to write from. * @return Byte array with serialized objects. *//*from w ww.jav a 2s .co m*/ public static byte[] writeListToByteArray(List<? extends Writable> writableList) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutput output = new DataOutputStream(outputStream); try { output.writeInt(writableList.size()); for (Writable writable : writableList) { writable.write(output); } } catch (IOException e) { throw new IllegalStateException("writeListToByteArray: IOException", e); } return outputStream.toByteArray(); }
From source file:org.apache.giraph.utils.WritableUtils.java
License:Apache License
/** * Write object to output stream//from w w w .j av a 2 s . c om * @param object Object * @param output Output stream * @throws IOException */ public static void writeWritableObject(Writable object, DataOutput output) throws IOException { output.writeBoolean(object != null); if (object != null) { output.writeUTF(object.getClass().getName()); object.write(output); } }
From source file:org.apache.giraph.utils.WritableUtils.java
License:Apache License
/** * Writes a list of Writable objects into output stream. * This method is trying to optimize space occupied by class information only * storing class object if it is different from the previous one * as in most cases arrays tend to have objects of the same type inside. * @param list serialized object//from ww w .j a v a 2s .c om * @param output the output stream * @throws IOException */ public static void writeList(List<? extends Writable> list, DataOutput output) throws IOException { output.writeBoolean(list != null); if (list != null) { output.writeInt(list.size()); Class<? extends Writable> clazz = null; for (Writable element : list) { output.writeBoolean(element == null); if (element != null) { if (element.getClass() != clazz) { clazz = element.getClass(); output.writeBoolean(true); writeClass(clazz, output); } else { output.writeBoolean(false); } element.write(output); } } } }
From source file:org.apache.hama.bsp.message.io.CombineSpilledDataProcessor.java
License:Apache License
@Override public boolean handleSpilledBuffer(SpilledByteBuffer buffer) { if (combiner == null || writableObject == null) { return super.handleSpilledBuffer(buffer); }//from w ww . ja v a 2 s .co m try { iterator.set(buffer); } catch (IOException e1) { LOG.error("Error setting buffer for combining data", e1); return false; } Writable combinedMessage = combiner.combine(iterator); try { iterator.prepareForNext(); } catch (IOException e1) { LOG.error("Error preparing for next buffer.", e1); return false; } try { combinedMessage.write(this.combineOutputBuffer); } catch (IOException e) { LOG.error("Error writing the combiner output.", e); return false; } try { this.combineOutputBuffer.flush(); } catch (IOException e) { LOG.error("Error flushing the combiner output.", e); return false; } this.combineOutputBuffer.getBuffer().flip(); try { return super.handleSpilledBuffer(new SpilledByteBuffer(this.combineOutputBuffer.getBuffer(), this.combineOutputBuffer.getBuffer().remaining())); } finally { this.combineOutputBuffer.clear(); } }
From source file:org.apache.hama.bsp.sync.ZKSyncClient.java
License:Apache License
/** * Utility function to get byte array out of Writable * // w w w. java2 s .com * @param value The Writable object to be converted to byte array. * @return byte array from the Writable object. Returns null on given null * value or on error. */ protected byte[] getBytesForData(Writable value) { byte[] data = null; if (value != null) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream outputStream = new DataOutputStream(byteStream); try { value.write(outputStream); outputStream.flush(); data = byteStream.toByteArray(); } catch (IOException e) { LOG.error("Error writing data to write buffer.", e); } finally { try { byteStream.close(); outputStream.close(); } catch (IOException e) { LOG.error("Error closing byte stream.", e); } } } return data; }