Java Code Examples for com.google.common.io.LittleEndianDataOutputStream#writeInt()
The following examples show how to use
com.google.common.io.LittleEndianDataOutputStream#writeInt() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringPoolChunk.java From android-arscblamer with Apache License 2.0 | 6 votes |
@Override public byte[] toByteArray(int options) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos); try { for (StringPoolSpan span : spans()) { byte[] encodedSpan = span.toByteArray(options); if (encodedSpan.length != StringPoolSpan.SPAN_LENGTH) { throw new IllegalStateException("Encountered a span of invalid length."); } payload.write(encodedSpan); } payload.writeInt(RES_STRING_POOL_SPAN_END); } finally { Closeables.close(payload, true); } return baos.toByteArray(); }
Example 2
Source File: FileSerializationBenchmark.java From imhotep with Apache License 2.0 | 5 votes |
@Override public void serialize(int[] a, File file) throws IOException { LittleEndianDataOutputStream os = new LittleEndianDataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); for (int val : a) { os.writeInt(val); } os.close(); }
Example 3
Source File: TestWeirdStuff.java From util with Apache License 2.0 | 5 votes |
private void writeFile() throws IOException { mmapFile = new File(tmpDir, "mmap"); LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(new BufferedOutputStream(new FileOutputStream(mmapFile), 65536)); //write 8 mb of crap to ensure multiple pages have been written for (int i = 0; i < 2 * 1024 * 1024; i++) { out.writeInt(i); } out.close(); }
Example 4
Source File: GuavaIOUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenWriteReadLittleEndian_thenCorrect() throws IOException { final int value = 100; final LittleEndianDataOutputStream writer = new LittleEndianDataOutputStream(new FileOutputStream("src/test/resources/test_le.txt")); writer.writeInt(value); writer.close(); final LittleEndianDataInputStream reader = new LittleEndianDataInputStream(new DataInputStream(new FileInputStream("src/test/resources/test_le.txt"))); final int result = reader.readInt(); reader.close(); assertEquals(value, result); }
Example 5
Source File: KdbxSerializer.java From KeePassJava2 with Apache License 2.0 | 4 votes |
/** * Write a KdbxHeader to the output stream supplied. The header is updated with the * message digest of the written stream. * @param kdbxHeader the header to write and update * @param outputStream the output stream * @throws IOException on error */ public static void writeKdbxHeader(KdbxHeader kdbxHeader, OutputStream outputStream) throws IOException { MessageDigest messageDigest = Encryption.getMessageDigestInstance(); DigestOutputStream digestOutputStream = new DigestOutputStream(outputStream, messageDigest); LittleEndianDataOutputStream ledos = new LittleEndianDataOutputStream(digestOutputStream); // write the magic number ledos.writeInt(SIG1); ledos.writeInt(SIG2); // write a file version ledos.writeInt(FILE_VERSION_32); ledos.writeByte(HeaderType.CIPHER_ID); ledos.writeShort(16); byte[] b = new byte[16]; ByteBuffer bb = ByteBuffer.wrap(b); bb.putLong(kdbxHeader.getCipherUuid().getMostSignificantBits()); bb.putLong(8, kdbxHeader.getCipherUuid().getLeastSignificantBits()); ledos.write(b); ledos.writeByte(HeaderType.COMPRESSION_FLAGS); ledos.writeShort(4); ledos.writeInt(kdbxHeader.getCompressionFlags().ordinal()); ledos.writeByte(HeaderType.MASTER_SEED); ledos.writeShort(kdbxHeader.getMasterSeed().length); ledos.write(kdbxHeader.getMasterSeed()); ledos.writeByte(HeaderType.TRANSFORM_SEED); ledos.writeShort(kdbxHeader.getTransformSeed().length); ledos.write(kdbxHeader.getTransformSeed()); ledos.writeByte(HeaderType.TRANSFORM_ROUNDS); ledos.writeShort(8); ledos.writeLong(kdbxHeader.getTransformRounds()); ledos.writeByte(HeaderType.ENCRYPTION_IV); ledos.writeShort(kdbxHeader.getEncryptionIv().length); ledos.write(kdbxHeader.getEncryptionIv()); ledos.writeByte(HeaderType.PROTECTED_STREAM_KEY); ledos.writeShort(kdbxHeader.getProtectedStreamKey().length); ledos.write(kdbxHeader.getProtectedStreamKey()); ledos.writeByte(HeaderType.STREAM_START_BYTES); ledos.writeShort(kdbxHeader.getStreamStartBytes().length); ledos.write(kdbxHeader.getStreamStartBytes()); ledos.writeByte(HeaderType.INNER_RANDOM_STREAM_ID); ledos.writeShort(4); ledos.writeInt(kdbxHeader.getProtectedStreamAlgorithm().ordinal()); ledos.writeByte(HeaderType.END); ledos.writeShort(0); MessageDigest digest = digestOutputStream.getMessageDigest(); kdbxHeader.setHeaderHash(digest.digest()); }
Example 6
Source File: ImmutableBTreeIndex.java From lsmtree with Apache License 2.0 | 4 votes |
private static <K,V> WriteLevelResult writeLevel( final CountingOutputStream counter, final Path tempPath, final Iterator<Generation.Entry<K,V>> iterator, final Serializer<K> keySerializer, final Serializer<V> valueSerializer, final int blocksize, final boolean keepDeletions ) throws IOException { Generation.Entry<K,V> next; if (!iterator.hasNext()) { return new WriteLevelResult(0, 0); } next = iterator.next(); final LittleEndianDataOutputStream tmpOut = new LittleEndianDataOutputStream(new BufferedOutputStream(Files.newOutputStream(tempPath), 131072)); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final LittleEndianDataOutputStream bufferDataOutput = new LittleEndianDataOutputStream(buffer); final ByteArrayOutputStream currentBlock = new ByteArrayOutputStream(blocksize); final CharArrayList keyOffsets = new CharArrayList(); int tmpCount = 0; boolean done = false; final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(counter); long count = 0; outer: while (!done) { currentBlock.reset(); keyOffsets.clear(); if (!keepDeletions) { while (next.isDeleted()) { if (!iterator.hasNext()) break outer; next = iterator.next(); } } keySerializer.write(next.getKey(), tmpOut); tmpOut.writeLong(counter.getCount()); tmpCount++; while (true) { buffer.reset(); final boolean skipDeleted = updateBuffer(next, keySerializer, valueSerializer, keepDeletions, bufferDataOutput); if (4+2*keyOffsets.size()+2+currentBlock.size()+buffer.size() > blocksize) { if (currentBlock.size() == 0) { throw new IllegalArgumentException("key value pair is greater than block size"); } break; } if (!skipDeleted) { keyOffsets.add((char)currentBlock.size()); buffer.writeTo(currentBlock); count++; } if (!iterator.hasNext()) { done = true; break; } next = iterator.next(); } if (keyOffsets.size() > 0) { final long start = counter.getCount(); out.writeInt(keyOffsets.size()); for (int i = 0; i < keyOffsets.size(); i++) { out.writeChar(keyOffsets.getChar(i)); } currentBlock.writeTo(out); if (counter.getCount()-start > blocksize) { log.error("too big"); } } } tmpOut.close(); return new WriteLevelResult(tmpCount, count); }