void () { if (myNextChunkBuffer != null && myDirty) { Path incompleteChunkFile = getIncompleteChunkFile(); try { saveNextChunkIfNeeded(); if (myBufferPosition != 0) { try (BufferedOutputStream stream = new BufferedOutputStream(Files.newOutputStream(incompleteChunkFile, StandardOpenOption.CREATE))) { stream.write(myNextChunkBuffer, 0, myBufferPosition); } } else if (Files.exists(incompleteChunkFile)) { Files.delete(incompleteChunkFile); } } catch (NoSuchFileException ex) { Path parentFile = incompleteChunkFile.getParent(); if (!Files.exists(parentFile)) { try { Files.createDirectories(parentFile); saveIncompleteChunk(); return; } catch (IOException e) { throw new RuntimeException("Failed to write: " + incompleteChunkFile, ex); } } throw new RuntimeException(ex); } catch (IOException ex) { throw new RuntimeException(ex); } myDirty = false; } }