{"record":{"id":"8aeb294f03406d95","repo":"MuntashirAkon/AppManager","slug":"failed-to-write-d-bytes-atomically-only-wrote-d","errorCode":null,"errorMessage":"Failed to write %,d bytes atomically. Only wrote  %,d","messagePattern":"Failed to write %,d bytes atomically\\. Only wrote  %,d","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStream.java","lineNumber":83,"sourceCode":"    public FixedLengthBlockOutputStream(final WritableByteChannel out, final int blockSize) {\n        this.out = out;\n        this.blockSize = blockSize;\n        this.buffer = ByteBuffer.allocateDirect(blockSize);\n    }\n\n    private void maybeFlush() throws IOException {\n        if (!buffer.hasRemaining()) {\n            writeBlock();\n        }\n    }\n\n    private void writeBlock() throws IOException {\n        buffer.flip();\n        final int i = out.write(buffer);\n        final boolean hasRemaining = buffer.hasRemaining();\n        if (i != blockSize || hasRemaining) {\n            final String msg = String.format(Locale.ROOT, \"Failed to write %,d bytes atomically. Only wrote  %,d\", blockSize, i);\n            throw new IOException(msg);\n        }\n        buffer.clear();\n    }\n\n    @Override\n    public void write(final int b) throws IOException {\n        if (!isOpen()) {\n            throw new ClosedChannelException();\n        }\n        buffer.put((byte) b);\n        maybeFlush();\n    }\n\n    @Override\n    public void write(final byte[] b, final int offset, final int length) throws IOException {\n        if (!isOpen()) {\n            throw new ClosedChannelException();\n        }","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStream.java#L65-L101","documentation":"FixedLengthBlockOutputStream.writeBlock flushes a full fixed-size block to the underlying channel and requires the write to be atomic: the channel must accept exactly blockSize bytes with none left over. A partial write indicates the underlying channel cannot write atomically in the required block size, so the stream throws rather than risk a corrupt/unaligned block.","triggerScenarios":"The underlying channel's write(ByteBuffer) returns fewer bytes than blockSize, or leaves buffer.hasRemaining() true — e.g. writing to a non-blocking channel, a pipe with insufficient capacity, or a channel with a smaller internal buffer.","commonSituations":"Using a non-blocking SocketChannel as the sink; underlying output streams with small buffers; wrapping channels that do partial writes under load or backpressure.","solutions":["Use a blocking file/channel sink that supports full-size writes (e.g. FileOutputStream/FileChannel in blocking mode)","Increase the underlying channel's buffer/capacity or drain the consumer side","Catch the IOException and retry the whole block on a channel that supports atomic writes"],"exampleFix":"// before\nnew FixedLengthBlockOutputStream(socketChannel, 512); // partial writes on non-blocking channel\n// after\nsocketChannel.configureBlocking(true);\nnew FixedLengthBlockOutputStream(socketChannel, 512);","handlingStrategy":"try-catch","validationCode":"// verify sink supports blocking atomic writes: if (channel instanceof SocketChannel && !((SocketChannel) channel).isBlocking()) throw new IllegalStateException(\"need blocking channel\");","typeGuard":null,"tryCatchPattern":"try { blockStream.write(buffer); blockStream.flushBlock(); } catch (IOException e) { if (e.getMessage().startsWith(\"Failed to write\")) { /* switch to a blocking/direct sink or retry */ } else throw e; }","preventionTips":["Use blocking FileChannel/FileOutputStream sinks with this stream","Avoid non-blocking socket channels as the underlying sink","Size blocks to what the sink can accept in one write"],"tags":["io","outputstream","partial-write"],"backgroundTag":"file-write-failed","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}