@Override public int read(byte[] buffer, int offset, int length) { int count = _buffer.readIfAvailable(buffer, offset, length); // need to translate 0 to -1, to signify end-of-content return (count <= 0) ? -1 : count; }
public final int readIfAvailable(byte[] buffer) { return readIfAvailable(buffer, 0, buffer.length); }
public final int readIfAvailable(byte[] buffer) { return readIfAvailable(buffer, 0, buffer.length); }
private void flushToFile(final File out) throws IOException { final byte[] buf = new byte[BUF_SIZE]; FileOutputStream transfer = null; int bytesTransferred = 0; try { transfer = Files.newOutputStreamSupplier(out).getOutput(); while (true) { final int r = inputBuffer.readIfAvailable(buf); if (r == 0) { break; } transfer.write(buf, 0, r); bytesTransferred += r; } } finally { if (transfer != null) { try { transfer.write(SmileConstants.TOKEN_LITERAL_END_ARRAY); bytesTransferred++; bytesOnDisk += bytesTransferred; } finally { transfer.flush(); } } } log.debug("Saved {} bytes to disk", bytesTransferred); }
private long _writeBuffered(StreamyBytesMemBuffer offHeap, OutputStream out, byte[] copyBuffer, byte[] leftovers) throws IOException { final long nanoStart = (_diagnostics == null) ? 0L : _timeMaster.nanosForDiagnostics(); long total = 0L; int count; if (offHeap != null) { while ((count = offHeap.readIfAvailable(copyBuffer)) > 0) { out.write(copyBuffer, 0, count); total += count; } } if (leftovers != null) { out.write(leftovers); total += leftovers.length; } if (_diagnostics != null) { _diagnostics.addResponseWriteTime(nanoStart, _timeMaster); } return total; }
@Override public Long perform(long operationTime, StorableKey key, Storable value, File externalFile) throws IOException, StoreException { final long fsStart = (diag == null) ? 0L : _timeMaster.nanosForDiagnostics(); long total = 0L; try { int count; while ((count = offHeap.readIfAvailable(readBuffer)) > 0) { total += count; try { out.write(readBuffer, 0, count); } catch (IOException e) { throw new StoreException.IO(key, "Failed to write "+count+" bytes (after "+total +") to file '"+externalFile.getAbsolutePath()+"'", e); } hasher.update(readBuffer, 0, count); } } finally { try { out.close(); } catch (IOException e) { } if (diag != null) { // Note: due to compression, bytes written may be less than read: long writtenBytes = (compressedOut == null) ? total : compressedOut.count(); diag.addFileWriteAccess(nanoStart, fsStart, _timeMaster, writtenBytes); } } return total; } });
while ((count = offHeap.readIfAvailable(readBuffer)) > 0) { out.write(readBuffer, 0, count); total += count;