List of usage examples for java.io DataOutput writeInt
void writeInt(int v) throws IOException;
int
value, which is comprised of four bytes, to the output stream. From source file:com.glaf.core.util.ByteBufferUtils.java
public static void writeWithLength(ByteBuffer bytes, DataOutput out) throws IOException { out.writeInt(bytes.remaining()); write(bytes, out); // writing data bytes to output source }
From source file:org.cloudata.core.common.io.CWritableUtils.java
public static int writeString(DataOutput out, String s) throws IOException { if (s != null) { byte[] buffer = s.getBytes("UTF-8"); int len = buffer.length; out.writeInt(len); out.write(buffer, 0, len);/*w w w . j a v a 2 s . c o m*/ return CWritableUtils.getIntByteSize() + len; } else { out.writeInt(-1); return CWritableUtils.getIntByteSize(); } }
From source file:com.willetinc.hadoop.mapreduce.dynamodb.AttributeValueIOUtils.java
public static void write(Types type, AttributeValue value, DataOutput out) throws IOException { switch (type) { case STRING:/*from w w w . j ava 2 s . c o m*/ Text.writeString(out, value.getS()); break; case NUMBER: Text.writeString(out, value.getN()); break; case BINARY: { WritableUtils.writeCompressedByteArray(out, value.getB().array()); break; } case STRING_SET: { List<String> values = value.getSS(); out.writeInt(values.size()); for (String s : values) { Text.writeString(out, s); } break; } case NUMBER_SET: { List<String> values = value.getNS(); out.writeInt(values.size()); for (String s : values) { Text.writeString(out, s); } break; } case BINARY_SET: { List<ByteBuffer> values = value.getBS(); out.writeInt(values.size()); for (ByteBuffer buf : values) { WritableUtils.writeCompressedByteArray(out, buf.array()); } } } }
From source file:hivemall.fm.FFMPredictionModel.java
@Nonnull static void writeStates(@Nonnull final byte[] status, @Nonnull final DataOutput out) throws IOException { // write empty states's indexes differentially final int size = status.length; int cardinarity = 0; for (int i = 0; i < size; i++) { if (status[i] != IntOpenHashTable.FULL) { cardinarity++;/*from w w w . java2s . c om*/ } } out.writeInt(cardinarity); if (cardinarity == 0) { return; } int prev = 0; for (int i = 0; i < size; i++) { if (status[i] != IntOpenHashTable.FULL) { int diff = i - prev; assert (diff >= 0); VariableByteCodec.encodeUnsignedInt(diff, out); prev = i; } } }
From source file:com.aliyun.odps.io.TupleReaderWriter.java
private static void writeDatum(DataOutput out, Writable val) throws IOException { // Read the data type byte type = findType(val); switch (type) { case TUPLE://from w w w.j a v a2s .c o m Tuple t = (Tuple) val; out.writeByte(TUPLE); int sz = t.size(); out.writeInt(sz); for (int i = 0; i < sz; i++) { writeDatum(out, t.get(i)); } break; case NULL: out.writeByte(NULL); break; case INTWRITABLE: out.writeByte(INTWRITABLE); ((IntWritable) val).write(out); break; case LONGWRITABLE: out.writeByte(LONGWRITABLE); ((LongWritable) val).write(out); break; case DATETIMEWRITABLE: out.writeByte(DATETIMEWRITABLE); ((DatetimeWritable) val).write(out); break; case DOUBLEWRITABLE: out.writeByte(DOUBLEWRITABLE); ((DoubleWritable) val).write(out); break; case BOOLEANWRITABLE: out.writeByte(BOOLEANWRITABLE); ((BooleanWritable) val).write(out); break; case BYTESWRITABLE: out.writeByte(BYTESWRITABLE); ((BytesWritable) val).write(out); break; case TEXT: out.writeByte(TEXT); ((Text) val).write(out); break; case NULLWRITABLE: out.writeByte(NULLWRITABLE); ((NullWritable) val).write(out); break; case UNKNOWN: out.writeByte(UNKNOWN); out.writeUTF(val.getClass().getName()); val.write(out); break; default: throw new RuntimeException("Unexpected data type " + type + " found in stream."); } }
From source file:com.linkedin.cubert.io.rubix.RubixFile.java
private static void extract(List<RubixFile<Tuple, Object>> rfiles, long blockId, int numBlocks, String output) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { Configuration conf = new JobConf(); File outFile = new File(output); if (outFile.exists()) { outFile.delete();/* ww w. j a va 2s.com*/ } outFile.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile)); ByteArrayOutputStream keySectionStream = new ByteArrayOutputStream(); DataOutput keySectionOut = new DataOutputStream(keySectionStream); SerializationFactory serializationFactory = new SerializationFactory(conf); RubixFile<Tuple, Object> lastrFile = null; JsonNode json; long totalLength = 0; final int BUF_SIZE = 32 * 1024; long blockIds[] = new long[numBlocks]; int foundBlocks = 0; for (int i = 0; i < numBlocks; i++) blockIds[i] = blockId + i; for (int i = 0; i < numBlocks; i++) { boolean found = false; for (RubixFile<Tuple, Object> rfile : rfiles) { print.f("Checking %s", rfile.path.toString()); List<KeyData<Tuple>> keyDataList = rfile.getKeyData(); for (KeyData<Tuple> keyData : keyDataList) { if (keyData.getBlockId() == blockIds[i]) { long offset = keyData.getOffset(); long length = keyData.getLength(); Tuple key = keyData.getKey(); print.f("Extracting block %d (off=%d len=%d) from %s", keyData.getBlockId(), offset, length, rfile.path.toString()); // copy the data if (length > 0) { FileSystem fs = FileSystem.get(conf); FSDataInputStream in = fs.open(rfile.path); in.seek(offset); byte[] data = new byte[BUF_SIZE]; long toRead = length; while (toRead > 0) { int thisRead = toRead > BUF_SIZE ? BUF_SIZE : (int) toRead; in.readFully(data, 0, thisRead); bos.write(data, 0, thisRead); toRead -= thisRead; System.out.print("."); } System.out.println(); } // copy the key section Serializer<Tuple> keySerializer = serializationFactory.getSerializer(rfile.getKeyClass()); keySerializer.open(keySectionStream); keySerializer.serialize(key); keySectionOut.writeLong(totalLength); // position keySectionOut.writeLong(keyData.getBlockId()); keySectionOut.writeLong(keyData.getNumRecords()); foundBlocks++; totalLength += length; lastrFile = rfile; found = true; break; } } if (found) { break; } } if (!found) System.err.println("Cannot locate block with id " + blockIds[i]); } byte[] trailerBytes = keySectionStream.toByteArray(); json = JsonUtils.cloneNode(lastrFile.metadataJson); ((ObjectNode) json).put("numberOfBlocks", foundBlocks); DataOutput out = new DataOutputStream(bos); out.writeUTF(json.toString()); out.writeInt(trailerBytes.length); out.write(trailerBytes); out.writeLong(totalLength); // trailer start offset bos.close(); }
From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java
private static void writeBlocks(final Block[] blocks, final DataOutput out) throws IOException { if (blocks == null) { out.writeInt(0); } else {/*from www .ja v a2 s. c om*/ out.writeInt(blocks.length); for (Block blk : blocks) { blk.write(out); } } }
From source file:com.bigdata.dastor.utils.FBUtilities.java
public static void serialize(TSerializer serializer, TBase struct, DataOutput out) throws IOException { assert serializer != null; assert struct != null; assert out != null; byte[] bytes; try {//from w ww. j a v a 2 s . c o m bytes = serializer.serialize(struct); } catch (TException e) { throw new RuntimeException(e); } out.writeInt(bytes.length); out.write(bytes); }
From source file:org.cloudata.core.common.io.CWritableUtils.java
public static int writeCompressedByteArray(DataOutput out, byte[] bytes) throws IOException { if (bytes != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzout = new GZIPOutputStream(bos); gzout.write(bytes, 0, bytes.length); gzout.close();/*from www. j av a2s . co m*/ byte[] buffer = bos.toByteArray(); int len = buffer.length; out.writeInt(len); out.write(buffer, 0, len); /* debug only! Once we have confidence, can lose this. */ return ((bytes.length != 0) ? (100 * buffer.length) / bytes.length : 0); } else { out.writeInt(-1); return -1; } }
From source file:com.krawler.common.util.ByteUtil.java
public static void writeUTF8(DataOutput out, String str) throws IOException { // Special case: Null string is serialized as length of -1. if (str == null) { out.writeInt(-1); return;/*from www . j a va 2 s .c o m*/ } int len = str.length(); if (len > MAX_STRING_LEN) throw new IOException( "String length " + len + " is too long in ByteUtil.writeUTF8(); max=" + MAX_STRING_LEN); if (len > 0) { byte[] buf = str.getBytes("UTF-8"); out.writeInt(buf.length); out.write(buf); } else out.writeInt(0); }