/** * Create a fixed length block output stream with given destination stream and block size * @param os The stream to wrap. * @param blockSize The block size to use. */ public FixedLengthBlockOutputStream(OutputStream os, int blockSize) { if (os instanceof FileOutputStream) { FileOutputStream fileOutputStream = (FileOutputStream) os; out = fileOutputStream.getChannel(); buffer = ByteBuffer.allocateDirect(blockSize); } else { out = new BufferAtATimeOutputChannel(os); buffer = ByteBuffer.allocate(blockSize); } this.blockSize = blockSize; } /**
@Override public int write(ByteBuffer buffer) throws IOException { if (!isOpen()) { throw new ClosedChannelException(); } if (!buffer.hasArray()) { throw new IllegalArgumentException("direct buffer somehow written to BufferAtATimeOutputChannel"); } try { int pos = buffer.position(); int len = buffer.limit() - pos; out.write(buffer.array(), buffer.arrayOffset() + pos, len); buffer.position(buffer.limit()); return len; } catch (IOException e) { try { close(); } catch (IOException ignored) { //NOSONAR } throw e; } }
@Override public int write(ByteBuffer buffer) throws IOException { if (!isOpen()) { throw new ClosedChannelException(); } if (!buffer.hasArray()) { throw new IllegalArgumentException("direct buffer somehow written to BufferAtATimeOutputChannel"); } try { int pos = buffer.position(); int len = buffer.limit() - pos; out.write(buffer.array(), buffer.arrayOffset() + pos, len); buffer.position(buffer.limit()); return len; } catch (IOException e) { try { close(); } catch (IOException ignored) { //NOSONAR } throw e; } }
/** * Create a fixed length block output stream with given destination stream and block size * @param os The stream to wrap. * @param blockSize The block size to use. */ public FixedLengthBlockOutputStream(OutputStream os, int blockSize) { if (os instanceof FileOutputStream) { FileOutputStream fileOutputStream = (FileOutputStream) os; out = fileOutputStream.getChannel(); buffer = ByteBuffer.allocateDirect(blockSize); } else { out = new BufferAtATimeOutputChannel(os); buffer = ByteBuffer.allocate(blockSize); } this.blockSize = blockSize; } /**