ByteArrayOutputStream ba = new ByteArrayOutputStream(16); DataOutputStream da = new DataOutputStream(ba); da.writeLong(uuid.getMostSignificantBits()); da.writeLong(uuid.getLeastSignificantBits()); return ba.toByteArray();
public GFSnapshotExporter(File out, String region, InternalCache cache) throws IOException { this.cache = cache; FileOutputStream fos = new FileOutputStream(out); fc = fos.getChannel(); dos = new DataOutputStream(new BufferedOutputStream(fos)); // write snapshot version dos.writeByte(SNAP_VER_2); // write format type dos.write(SNAP_FMT); // write temporary pdx location in bytes 4-11 dos.writeLong(-1); // write region name dos.writeUTF(region); }
@Test public void testUpdateLong() throws Exception { long input = Long.MIN_VALUE + 123456789; ByteArrayOutputStream out = new ByteArrayOutputStream(); new DataOutputStream(out).writeLong(input); long expected = getHashAndReset(out); primitiveDataChecksum.updateLong(input); Assert.assertEquals(expected, primitiveDataChecksum.getValue()); }
/** * Writes this {@code BloomFilter} to an output stream, with a custom format (not Java * serialization). This has been measured to save at least 400 bytes compared to regular * serialization. * * <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter. */ public void writeTo(OutputStream out) throws IOException { // Serial form: // 1 signed byte for the strategy // 1 unsigned byte for the number of hash functions // 1 big endian int, the number of longs in our bitset // N big endian longs of our bitset DataOutputStream dout = new DataOutputStream(out); dout.writeByte(SignedBytes.checkedCast(strategy.ordinal())); dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor dout.writeInt(bits.data.length()); for (int i = 0; i < bits.data.length(); i++) { dout.writeLong(bits.data.get(i)); } }
@Override public void serialize(final Long value, final OutputStream out) throws SerializationException, IOException { final DataOutputStream dos = new DataOutputStream(out); dos.writeLong(value); }
private static void writeLdc(LdcInsnNode lin, DataOutputStream dos) throws IOException { Object o = lin.cst; if (o instanceof Integer) { dos.writeShort(1); dos.writeInt((Integer)o); } else if (o instanceof Float) { dos.writeShort(2); dos.writeFloat((Float)o); } else if (o instanceof Long) { dos.writeShort(3); dos.writeLong((Long)o); } else if (o instanceof Double) { dos.writeShort(4); dos.writeDouble((Double)o); } else if (o instanceof String) { dos.writeShort(5); dos.writeUTF((String)o); } else if (o instanceof Type) { dos.writeShort(6); dos.writeUTF(((Type)o).getDescriptor()); } else { dos.writeShort(0); } }
@Override public <K, V> boolean replace(AtomicCacheEntry<K, V, Long> entry, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException { return withCommsSession(session -> { validateProtocolVersion(session, 2); final DataOutputStream dos = new DataOutputStream(session.getOutputStream()); dos.writeUTF("replace"); serialize(entry.getKey(), keySerializer, dos); dos.writeLong(entry.getRevision().orElse(0L)); serialize(entry.getValue(), valueSerializer, dos); dos.flush(); // read response final DataInputStream dis = new DataInputStream(session.getInputStream()); return dis.readBoolean(); }); }
ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeLong(someLong); dos.close(); byte[] longBytes = baos.toByteArray();
/** * Serialize a bloom filter * * @param out output stream to write to * @param bloomFilter BloomKFilter that needs to be seralized */ public static void serialize(OutputStream out, BloomKFilter bloomFilter) throws IOException { /** * Serialized BloomKFilter format: * 1 byte for the number of hash functions. * 1 big endian int(That is how OutputStream works) for the number of longs in the bitset * big endina longs in the BloomKFilter bitset */ DataOutputStream dataOutputStream = new DataOutputStream(out); dataOutputStream.writeByte(bloomFilter.k); dataOutputStream.writeInt(bloomFilter.getBitSet().length); for (long value : bloomFilter.getBitSet()) { dataOutputStream.writeLong(value); } }
private static <T> T talkToServiceWithinPortForward(Communicator<T> communicator, int localPort) throws IOException { Socket socket = new Socket(LOCAL_HOST, localPort); DataInputStream input = new DataInputStream(socket.getInputStream()); DataOutputStream output = new DataOutputStream(socket.getOutputStream()); output.writeLong(ProtocolConstants.PROTOCOL_IDENTIFIER); output.writeInt(ProtocolConstants.PROTOCOL_VERSION); socket.setSoTimeout(2 * 1000); // Allow up to 2 seconds before timing out int version = input.readInt(); if (version != ProtocolConstants.PROTOCOL_VERSION) { String msg = String.format(Locale.US, "Client and server protocol versions don't match (%1$d != %2$d)", version, ProtocolConstants.PROTOCOL_VERSION); throw new IOException(msg); } socket.setSoTimeout(communicator.getTimeout()); T value = communicator.communicate(input, output); output.writeInt(ProtocolConstants.MESSAGE_EOF); return value; } }
public static void persistCreationMeta(File indexDir, long crc, long creationTime) throws IOException { File segmentDir = SegmentDirectoryPaths.findSegmentDirectory(indexDir); File creationMetaFile = new File(segmentDir, V1Constants.SEGMENT_CREATION_META); try (DataOutputStream output = new DataOutputStream(new FileOutputStream(creationMetaFile))) { output.writeLong(crc); output.writeLong(creationTime); } }
@Override public void serializeRecord(final StateMapUpdate record, final DataOutputStream out) throws IOException { out.writeUTF(record.getComponentId()); out.writeUTF(record.getUpdateType().name()); if (record.getUpdateType() == UpdateType.DELETE) { return; } final StateMap stateMap = record.getStateMap(); final long recordVersion = stateMap.getVersion(); out.writeLong(recordVersion); final Map<String, String> map = stateMap.toMap(); out.writeInt(map.size()); for (final Map.Entry<String, String> entry : map.entrySet()) { final boolean hasKey = entry.getKey() != null; final boolean hasValue = entry.getValue() != null; out.writeBoolean(hasKey); if (hasKey) { out.writeUTF(entry.getKey()); } out.writeBoolean(hasValue); if (hasValue) { out.writeUTF(entry.getValue()); } } }
private void writeAdditionalPayload(final DataOutputStream out) throws IOException { boolean isFileIdLong = fileKey instanceof Long, isFileIdWritable = fileKey instanceof Writable; int flags = (hasBase ? BASE_FLAG : 0) | (isOriginal ? ORIGINAL_FLAG : 0) | (hasFooter ? FOOTER_FLAG : 0) | (isFileIdLong ? HAS_LONG_FILEID_FLAG : 0) | (isFileIdWritable ? HAS_SYNTHETIC_FILEID_FLAG : 0); out.writeByte(flags); out.writeInt(deltas.size()); for(AcidInputFormat.DeltaMetaData delta: deltas) { delta.write(out); } if (hasFooter) { OrcProto.FileTail fileTail = orcTail.getMinimalFileTail(); byte[] tailBuffer = fileTail.toByteArray(); int tailLen = tailBuffer.length; WritableUtils.writeVInt(out, tailLen); out.write(tailBuffer); } if (isFileIdLong) { out.writeLong(((Long)fileKey).longValue()); } else if (isFileIdWritable) { ((Writable)fileKey).write(out); } out.writeLong(fileLen); }
public static byte[] serialize(CountMinSketch sketch) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream s = new DataOutputStream(bos); try { s.writeLong(sketch.size); s.writeInt(sketch.depth); s.writeInt(sketch.width); for (int i = 0; i < sketch.depth; ++i) { s.writeLong(sketch.hashA[i]); for (int j = 0; j < sketch.width; ++j) { s.writeLong(sketch.table[i][j]); } } return bos.toByteArray(); } catch (IOException e) { // Shouldn't happen throw new RuntimeException(e); } }