List of usage examples for com.google.common.io ByteStreams newDataOutput
public static ByteArrayDataOutput newDataOutput(ByteArrayOutputStream byteArrayOutputSteam)
From source file:io.airlift.security.der.DerUtils.java
/** * Encodes a sequence of encoded values. *//*from w w w . j a v a2 s .co m*/ public static byte[] encodeSequence(byte[]... encodedValues) { int length = 0; for (byte[] encodedValue : encodedValues) { length += encodedValue.length; } byte[] lengthEncoded = encodeLength(length); ByteArrayDataOutput out = ByteStreams.newDataOutput(1 + lengthEncoded.length + length); out.write(SEQUENCE_TAG); out.write(lengthEncoded); for (byte[] entry : encodedValues) { out.write(entry); } return out.toByteArray(); }
From source file:org.elasticsoftware.elasticactors.cassandra.serialization.CompressingSerializer.java
@Override public byte[] serialize(I object) throws IOException { byte[] serializedObject = delegate.serialize(object); if (serializedObject.length > compressionThreshold) { byte[] compressedBytes = lz4Compressor.compress(serializedObject); ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput(compressedBytes.length + 8); dataOutput.write(MAGIC_HEADER);//from www . j a v a 2 s . c o m dataOutput.writeInt(serializedObject.length); dataOutput.write(compressedBytes); return dataOutput.toByteArray(); } else { return serializedObject; } }
From source file:org.elasticsoftware.elasticactors.cassandra2.serialization.CompressingSerializer.java
@Override public ByteBuffer serialize(I object) throws IOException { byte[] serializedObject = delegate.serialize(object); if (serializedObject.length > compressionThreshold) { byte[] compressedBytes = lz4Compressor.compress(serializedObject); ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput(compressedBytes.length + 8); dataOutput.write(MAGIC_HEADER);//from ww w . j a va 2s . c o m dataOutput.writeInt(serializedObject.length); dataOutput.write(compressedBytes); return ByteBuffer.wrap(dataOutput.toByteArray()); } else { return ByteBuffer.wrap(serializedObject); } }
From source file:org.locationtech.geogig.storage.datastream.FormatCommonV2_1.java
@Override public void writeFeature(RevFeature feature, DataOutput target) throws IOException { try (InternalByteArrayOutputStream out = new InternalByteArrayOutputStream()) { final DataOutput data = ByteStreams.newDataOutput(out); final int attrCount = feature.size(); final int[] dataOffsets = new int[attrCount]; int offset = 0; for (int i = 0; i < attrCount; i++) { Object value = feature.get(i).orNull(); FieldType type = FieldType.forValue(value); data.writeByte(type.getTag() & 0xFF); DataStreamValueSerializerV2.write(value, data); dataOffsets[i] = offset;/*from w w w . j ava 2s . co m*/ offset = out.size(); } // <HEADER> // - unsigned varint: number of attributes writeUnsignedVarInt(attrCount, target); // - unsigned varint: size of <DATA> final int dataSize = out.size(); writeUnsignedVarInt(dataSize, target); // - unsigned varint[number of attributes]: attribute offsets (starting form zero at // <DATA>, not including the header) for (int i = 0; i < attrCount; i++) { writeUnsignedVarInt(dataOffsets[i], target); } // <DATA> target.write(out.intenal(), 0, dataSize); } }
From source file:com.google.devrel.gmscore.tools.apk.arsc.ResourceString.java
/** * Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string. * Strings are prefixed by 2 values. The first is the number of characters in the string. * The second is the encoding length (number of bytes in the string). * * <p>Here's an example UTF-8-encoded string of ab: * <pre>03 04 61 62 C2 A9 00</pre> * * @param str The string to be encoded./* w w w. j av a2 s . c om*/ * @param type The encoding type that the {@link ResourceString} should be encoded in. * @return The encoded string. */ public static byte[] encodeString(String str, Type type) { byte[] bytes = str.getBytes(type.charset()); // The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator. ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5); encodeLength(output, str.length(), type); if (type == Type.UTF8) { // Only UTF-8 strings have the encoding length. encodeLength(output, bytes.length, type); } output.write(bytes); // NULL-terminate the string if (type == Type.UTF8) { output.write(0); } else { output.writeShort(0); } return output.toByteArray(); }
From source file:io.airlift.security.der.DerUtils.java
/** * Encodes a bit string padded with the specified number of bits. * The encoding is a byte containing the padBits followed by the value bytes. *///from w w w . ja v a 2s.c o m public static byte[] encodeBitString(int padBits, byte[] value) { checkArgument(padBits >= 0 && padBits < 8, "Invalid pad bits"); byte[] lengthEncoded = encodeLength(value.length + 1); ByteArrayDataOutput out = ByteStreams.newDataOutput(2 + lengthEncoded.length + value.length); out.write(BIT_STRING_TAG); out.write(lengthEncoded); out.write(padBits); out.write(value); return out.toByteArray(); }
From source file:io.airlift.security.der.DerUtils.java
/** * Encodes an octet string.//from ww w.j a v a 2 s . c o m */ public static byte[] encodeOctetString(byte[] value) { byte[] lengthEncoded = encodeLength(value.length); ByteArrayDataOutput out = ByteStreams.newDataOutput(2 + lengthEncoded.length + value.length); out.write(OCTET_STRING_TAG); out.write(lengthEncoded); out.write(value); return out.toByteArray(); }
From source file:org.locationtech.geogig.storage.datastream.v2_3.RevTreeFormat.java
private static void encode(final RevTree tree, ByteArrayOutputStream buff) throws IOException { DataOutput out = ByteStreams.newDataOutput(buff); final int offsetOfTreesNodeset; final int offsetOfFeaturesNodeset; final int offsetOfBuckets; final int offsetOfStringTable; final int offsetOfTail; StringTable stringTable = StringTable.unique(); Header.encode(out, tree);/* w w w . j ava 2s .c o m*/ offsetOfTreesNodeset = tree.treesSize() == 0 ? 0 : buff.size(); if (tree.treesSize() > 0) { NodeSet.encode(out, tree.trees(), stringTable); } offsetOfFeaturesNodeset = tree.featuresSize() == 0 ? 0 : buff.size(); if (tree.featuresSize() > 0) { NodeSet.encode(out, tree.features(), stringTable); } offsetOfBuckets = tree.bucketsSize() == 0 ? 0 : buff.size(); BucketSet.encode(out, tree, stringTable); offsetOfStringTable = buff.size(); stringTable.encode(out); offsetOfTail = buff.size(); Tail.encode(out, // offsetOfTreesNodeset, // offsetOfFeaturesNodeset, // offsetOfBuckets, // offsetOfStringTable, // offsetOfTail); }
From source file:com.netease.flume.taildirSource.TailFile.java
private String readLine() throws IOException { ByteArrayDataOutput out = ByteStreams.newDataOutput(300); int i = 0;//from www .ja va 2s. c o m int c; while ((c = raf.read()) != -1) { i++; out.write((byte) c); if (c == LINE_SEP.charAt(0)) { break; } } if (i == 0) { return null; } return new String(out.toByteArray(), Charsets.UTF_8); }
From source file:org.locationtech.geogig.storage.postgresql.PGIndexDatabase.java
@Override public IndexInfo createIndexInfo(String treeName, String attributeName, IndexType strategy, @Nullable Map<String, Object> metadata) { IndexInfo index = new IndexInfo(treeName, attributeName, strategy, metadata); final String sql = format( "INSERT INTO %s (repository, treeName, attributeName, strategy, metadata) VALUES(?, ?, ?, ?, ?)", config.getTables().index()); try (Connection cx = PGStorage.newConnection(dataSource)) { cx.setAutoCommit(false);//from w ww.j a v a 2 s . c o m try (PreparedStatement ps = cx .prepareStatement(log(sql, LOG, repositoryId, treeName, attributeName, strategy, metadata)); ByteArrayOutputStream outStream = new ByteArrayOutputStream()) { ps.setInt(1, repositoryId); ps.setString(2, treeName); ps.setString(3, attributeName); ps.setString(4, strategy.toString()); if (index.getMetadata() != null) { DataOutput out = ByteStreams.newDataOutput(outStream); DataStreamValueSerializerV2.write(index.getMetadata(), out); ps.setBytes(5, outStream.toByteArray()); } else { ps.setNull(5, java.sql.Types.OTHER, "bytea"); } ps.executeUpdate(); cx.commit(); } catch (SQLException | IOException e) { rollbackAndRethrow(cx, e); } finally { cx.setAutoCommit(true); } } catch (SQLException e) { throw propagate(e); } return index; }