@Override public synchronized void setLength(long newLength) throws IOException { // Flush before changing the file's length, otherwise the behavior of setLength() would be modified, especially // when truncating the file flush(); raos.setLength(newLength); }
/** * Writes the specified byte to this buffered output stream. * * @param b the byte to be written * @throws IOException if an I/O error occurs */ @Override public synchronized void write(int b) throws IOException { if (count >= buffer.length) flushBuffer(); buffer[count++] = (byte)b; }
/** * Writes the specified byte array to this buffered output stream. * * @param b the bytes to be written * @throws IOException if an I/O error occurs */ @Override public synchronized void write(byte b[]) throws IOException { write(b, 0, b.length); }
/** * Opens the zip file for random write access. * * @throws IOException if an error occured while opening the zip file for random read access. * @throws UnsupportedFileOperationException if a required operation is not supported by the underlying filesystem. */ private void openWrite() throws IOException, UnsupportedFileOperationException { if(raos!=null) { LOGGER.info("Warning: an existing RandomAccessOutputStream was found, closing it now"); raos.close(); } // Create a buffered output stream to improve write performance, as headers are written by small chunks raos = new BufferedRandomOutputStream(file.getRandomAccessOutputStream(), WRITE_BUFFER_SIZE); }
public synchronized long getLength() throws IOException { // Anticipate if the file is to be expanded by the bytes awaiting in the buffer return Math.max(raos.getLength(), getOffset()); }
file.delete(); out = new BufferedRandomOutputStream(file.getRandomAccessOutputStream());
public synchronized void seek(long offset) throws IOException { // Flush any buffered bytes before seeking, otherwise buffered bytes would be written at the wrong offset flush(); raos.seek(offset); }
/** * Flushes this buffered output stream. This forces any buffered * output bytes to be written out to the underlying output stream. * * @throws IOException if an I/O error occurs. */ @Override public synchronized void flush() throws IOException { flushBuffer(); raos.flush(); }
/** * This method is overridden to release the internal buffer when this stream is closed. */ @Override public synchronized void close() throws IOException { if(buffer!=null) { // buffer is null if close() was already called try { flush(); } catch(IOException e) { // Continue anyway } // Release the buffer BufferPool.releaseByteArray(buffer); buffer = null; } raos.close(); }
flushBuffer(); raos.write(b, off, len); return; flushBuffer();