preparations for
@@ -169,18 +169,26 @@ public boolean isOpen() {
return !closed.get();
}
- @Override
- public void close() throws IOException {
- if (closed.compareAndSet(false, true)) {
+ /**
+ * Potentialy pads and then writes the block to the underlying stream.
+ * @throws IOException if writing fails
+ */
+ public void flushBlock() throws IOException {
if (buffer.position() != 0) {
- padLastBlock();
+ padBlock();
writeBlock();
}
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (closed.compareAndSet(false, true)) {
+ flushBlock();
out.close();
}
}
- private void padLastBlock() {
+ private void padBlock() {
buffer.order(ByteOrder.nativeOrder());
int bytesToWrite = buffer.remaining();
if (bytesToWrite > 8) {
