List of usage examples for com.google.common.io ByteStreams readFully
public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException
From source file:org.bimserver.client.CountingLittleEndianDataInputStream.java
@Override public void readFully(byte[] b, int off, int len) throws IOException { ByteStreams.readFully(this, b, off, len); pos += len;//w w w.ja v a2 s . c om }
From source file:com.bennavetta.appsite2.sync.Rsync.java
/** * Given the up-to-date file and the list of blocks from the old file, calculate the differences * between the two./*from w ww. j av a 2s. co m*/ * @param oldBlocks the blocks from the old file * @param chunkSize the chunk size used to generate the blocks * @param input the new file * @param listener an event handler that will receive the calculated differences * @throws IOException if there is an exception calculating differences */ public static void calculateDifferences(final ObjectArrayList<Block> oldBlocks, final int chunkSize, final InputStream input, final DifferenceListener listener) throws IOException { final ByteArrayDeque buf = new ByteArrayDeque(chunkSize); final ByteArrayList newData = new ByteArrayList(chunkSize); // build up new data final byte[] matchBuf = new byte[chunkSize]; //the buffer to pass into findMatch, etc. final byte[] single = new byte[1]; boolean inMatch = false; int matchRemaining = 0; listener.onStart(); ByteStreams.readFully(input, matchBuf, 0, chunkSize); // if we read directly into the ByteArrayDeque, it doesn't know that data was added buf.addLast(matchBuf); long checksum = RollingChecksum.checksum(buf.buffer, buf.head, chunkSize); int match = findMatch(oldBlocks, checksum, matchBuf); //only this time, for performance if (match != -1) { listener.onMatch(0); inMatch = true; matchRemaining = chunkSize - 1; // already one byte into match } while (true) { final byte old = buf.removeFirst(); final int read = input.read(single); if (read == -1) { break; } buf.addLast(single[0]); checksum = RollingChecksum.update(checksum, old, buf.getLast(), chunkSize); if (inMatch) { if (matchRemaining == 0) { inMatch = false; } matchRemaining--; } if (!inMatch) // separate test because we could have just left being in a match { match = findMatch(oldBlocks, checksum, buf.toArray(matchBuf)); //TODO: figure out how to pass the buffer without copying if (match == -1) { newData.add(buf.getLast()); //System.out.println("No match at " + i); } else { if (newData.size() > 0) { System.out.println("New data (" + newData.size() + " bytes):"); System.out.println(Charsets.UTF_8.decode(ByteBuffer.wrap(newData.toArray()))); System.out.println("Base64 => " + BaseEncoding.base16().encode(newData.toArray())); System.out.println("Decimal => " + newData.toString()); listener.onDifferent(newData.toArray(), 0, newData.size()); newData.elementsCount = 0; // could call clear(), but this keeps the buffer assert newData.size() == 0; } listener.onMatch(match); inMatch = true; matchRemaining = chunkSize - 1; // already one byte into match } } } listener.onFinish(); }
From source file:org.apache.spark.util.collection.unsafe.sort.UnsafeSorterSpillReader.java
@Override public void loadNext() throws IOException { recordLength = din.readInt();/*from w ww. jav a2 s .c om*/ keyPrefix = din.readLong(); if (recordLength > arr.length) { arr = new byte[recordLength]; baseObject = arr; } ByteStreams.readFully(in, arr, 0, recordLength); numRecordsRemaining--; if (numRecordsRemaining == 0) { close(); } }
From source file:org.bimserver.cache.FileCacheReadingWriter.java
@Override public boolean writeMessage(OutputStream outputStream, ProgressReporter progressReporter) throws IOException, SerializerException { if (outputStream instanceof ReusableLittleEndianDataOutputStream) { ReusableLittleEndianDataOutputStream reusableLittleEndianDataOutputStream = (ReusableLittleEndianDataOutputStream) outputStream; reusableLittleEndianDataOutputStream.ensureExtraCapacity(nextSize); GrowingByteBuffer growingByteBuffer = reusableLittleEndianDataOutputStream.getGrowingByteBuffer(); ByteBuffer targetBuffer = growingByteBuffer.getByteBuffer(); inputStream.readFully(targetBuffer.array(), targetBuffer.position(), nextSize); targetBuffer.position(targetBuffer.position() + nextSize); nextSize = inputStream.readInt(); return nextSize != -1; } else {//from www . ja va 2 s . co m if (buffer == null || nextSize > buffer.length) { buffer = new byte[nextSize]; } ByteStreams.readFully(inputStream, buffer, 0, nextSize); outputStream.write(buffer, 0, nextSize); nextSize = inputStream.readInt(); return nextSize != -1; } }
From source file:hihex.cs.LogEntry.java
/** * Replace the current entry with the content of the input stream. *///from w w w . j a v a2s . c om public void read(final InputStream stream) throws IOException { ByteStreams.readFully(stream, mSharedArray, 0, 4); mPayloadLength = mSharedBuffer.getShort(0); int headerLength = mSharedBuffer.getShort(2); if (headerLength != 24) { // FIXME In logger_entry(_v1) the __pad can be filled with garbage. We don't know if we are targeting v1 or // not. Maybe do an actual ioctl() check in the future. headerLength = 20; } ByteStreams.readFully(stream, mSharedArray, 0, headerLength - 4); mPid = mSharedBuffer.getInt(0); mTid = mSharedBuffer.getInt(4); mSec = mSharedBuffer.getInt(8); mNSec = mSharedBuffer.getInt(12); ByteStreams.readFully(stream, mSharedArray, 0, mPayloadLength); mTagSeparator = Bytes.indexOf(mSharedArray, (byte) 0); mTag = Optional.absent(); mMessage = Optional.absent(); }
From source file:com.codeminders.socketio.protocol.EngineIOProtocol.java
public static List<EngineIOPacket> binaryDecodePayload(InputStream is) throws IOException { final ArrayList<EngineIOPacket> packets = new ArrayList<>(); while (true) { final int packetFormat = is.read(); if (packetFormat == -1) break; // end of payload stream, done if (packetFormat != BINARY_FORMAT && packetFormat != TEXT_FORMAT) throw new SocketIOProtocolException("Unknown packet format (should be 0 or 1) :" + packetFormat); final int len = decodePacketLength(is); if (len < 0) // end of payload stream, done break; if (packetFormat == BINARY_FORMAT && len == 0) throw new SocketIOProtocolException("Empty binary attachment"); final EngineIOPacket.Type packetType = decodePacketType(packetFormat, is); byte[] data = new byte[len - 1]; ByteStreams.readFully(is, data, 0, data.length); switch (packetType) { case CLOSE: packets.add(createClosePacket()); break; case PING: if (packetFormat == TEXT_FORMAT) packets.add(createPingPacket(new String(data, "UTF-8"))); else// ww w .j a va2s.c om throw new SocketIOProtocolException("No implementation for binary PING"); break; case MESSAGE: if (packetFormat == TEXT_FORMAT) packets.add(createMessagePacket(new String(data, "UTF-8"))); else packets.add(createMessagePacket(new ByteArrayInputStream(data))); break; case UPGRADE: packets.add(createUpgradePacket()); break; case NOOP: packets.add(createNoopPacket()); break; default: throw new SocketIOProtocolException("Unexpected EIO packet type: " + packetType); } } return packets; }
From source file:org.apache.beam.runners.dataflow.util.RandomAccessData.java
/** * Reads {@code length} bytes from the specified input stream writing them into the backing * data store starting at {@code offset}. * * <p>Note that the in memory stream will be grown to ensure there is enough capacity. */// w w w. ja v a 2 s. c o m public void readFrom(InputStream inStream, int offset, int length) throws IOException { ensureCapacity(offset + length); ByteStreams.readFully(inStream, buffer, offset, length); size = offset + length; }
From source file:com.addthis.basis.util.LessBytes.java
/** * Read len bytes from is into b, starting at off. same as * InputStream.read(byte[], int, int) except that it keeps trying until len * bytes have been read.//from www. j a v a2s.c om * * @throws IOException if the stream threw an exception or if the end of * stream was reached before len bytes could be read * @deprecated Use * {@link ByteStreams#readFully(InputStream, byte[], int, int)} * from Guava. */ @Deprecated public static void readBytes(InputStream is, byte[] b, int off, int len) throws IOException { ByteStreams.readFully(is, b, off, len); }