@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; }
/** * Method that tries to append data in buffer if there is enough room; * if there is, data is appended and 'true' returned; otherwise no changes * are made and 'false' is returned. */ public abstract boolean tryAppend(byte[] data, int dataOffset, int dataLength);
public long getBytesInMemory() { return inputBuffer.getTotalPayloadLength(); }
count = input.read(readBuffer); } catch (IOException e) { // probably will fail to write response too but... throw new StoreException.IO(key, "Failed to read content to store (after "+offHeap.getTotalPayloadLength() +" bytes)", e); if (!offHeap.tryAppend(readBuffer, 0, count)) {
@Override public void close() { _buffer.close(); }
@Override public int available() { long l = _buffer.available(); if (l > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } return (int) l; }
/** * Method that tries to append data in buffer and returning; * if there is no room, a {@link IllegalStateException} is thrown. */ public final void append(byte[] data) { append(data, 0, data.length); }
@Override public void close() throws IOException { // Cleanup volatile data inputBuffer.clear(); // Cleanup persistent data if (deleteFilesOnClose) { for (final String path : createdFiles) { log.info("Discarding file: {}", path); if (!new File(path).delete()) { log.warn("Unable to discard file: {}", path); } } } }
count = input.read(readBuffer, 0, maxToRead); } catch (IOException e) { long offLength = (offHeap == null) ? 0 : offHeap.getTotalPayloadLength(); throw new StoreException.IO(key, "Failed to read content to store (after "+offLength +" bytes)", e); if ((offHeap == null) || !offHeap.tryAppend(readBuffer, 0, count)) { _verifyBufferLength(offHeap, total);
@Override public <T> T leaseOffHeapBuffer(ByteBufferCallback<T> cb) { StreamyBytesMemBuffer buffer = null; try { buffer = allocOffHeapBuffer(); return cb.withBuffer(buffer); } catch (IllegalStateException e) { return cb.withError(e); } finally { if (buffer != null) { buffer.close(); } } }
private void flushUnderlyingBufferAndReset() { synchronized (inputBuffer) { if (inputBuffer.available() == 0) { // Somebody beat you to it return; } final String pathname = getFileName(); createdFiles.add(pathname); log.debug("Flushing in-memory buffer to disk: {}", pathname); try { final File out = new File(pathname); flushToFile(out); } catch (IOException e) { log.warn("Error flushing data", e); } finally { reset(); } } }
/** * Method that tries to append data in buffer and returning; * if there is no room, a {@link IllegalStateException} is thrown. */ public final void append(byte[] data) { append(data, 0, data.length); }
public void reset() { inputBuffer.clear(); try { write(SmileConstants.TOKEN_LITERAL_START_ARRAY); } catch (IOException e) { // Not sure how to recover? } }
/** * Method that tries to append data in buffer if there is enough room; * if there is, data is appended and 'true' returned; otherwise no changes * are made and 'false' is returned. */ public abstract boolean tryAppend(byte[] data, int dataOffset, int dataLength);
public final int readIfAvailable(byte[] buffer) { return readIfAvailable(buffer, 0, buffer.length); }
private void _verifyBufferLength(StreamyBytesMemBuffer offHeap, long total) throws IOException { // sanity check to verify that buffer has everything it is supposed to... long bufd = (offHeap == null) ? 0L : offHeap.getTotalPayloadLength(); if (bufd != total) { throw new IOException("Internal buffering problem: read "+total +" bytes, buffer contains "+bufd); } }
} finally { if (offHeap != null) { offHeap.close();
/** * Method that tries to append data in buffer and returning; * if there is no room, a {@link IllegalStateException} is thrown. */ public final void append(byte[] data, int dataOffset, int dataLength) { if (!tryAppend(data, dataOffset, dataLength)) { throw new IllegalStateException("Not enough room in buffer to append entry of "+dataLength +" (can't allocate enough new segments)"); } }
public final int readIfAvailable(byte[] buffer) { return readIfAvailable(buffer, 0, buffer.length); }
/** * Method that tries to append value in buffer and returning; * if there is no room, a {@link IllegalStateException} is thrown. */ public final void append(byte value) { if (!tryAppend(value)) { throw new IllegalStateException("Not enough room in buffer to append a single value (can't allocate enough new segments)"); } }