@Override protected final void writeOut(final byte[] data, final int offset, final int length) throws IOException { channel.write(ByteBuffer.wrap(data, offset, length)); } }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { if (len > BUF_SIZE) { channel.write(ByteBuffer.wrap(b, off, len)); } else { buffer.clear(); buffer.put(b, off, len).flip(); channel.write(buffer); } compressedCrc32.update(b, off, len); fileBytesWritten += len; }
private static void copy(SeekableByteChannel channel, SeekableByteChannel channel2, ByteBuffer copyBuff, int fileSize) throws IOException { int remain = fileSize; while (remain > 0) { copyBuff.rewind(); copyBuff.limit(Math.min(remain, copyBuff.capacity())); int read = channel.read(copyBuff); copyBuff.position(0); channel2.write(copyBuff); remain -= read; } }
@Override public void write(final int b) throws IOException { buffer.clear(); buffer.put((byte) b).flip(); channel.write(buffer); compressedCrc32.update(b); fileBytesWritten++; }
@Test public void testWriteOnClose() throws Exception { Path path = Paths.get(URI.create("gs://greenbean/adipose")); try (SeekableByteChannel chan = Files.newByteChannel(path, WRITE)) { // writing lots of contents to defeat channel-internal buffering. for (int i = 0; i < 9999; i++) { for (String s : FILE_CONTENTS) { chan.write(ByteBuffer.wrap(s.getBytes(UTF_8))); } } try { Files.size(path); // we shouldn't make it to this line. Not using thrown.expect because // I still want to run a few lines after the exception. assertThat(false).isTrue(); } catch (NoSuchFileException nsf) { // that's what we wanted, we're good. } } // channel now closed, the file should be there and with the new contents. assertThat(Files.exists(path)).isTrue(); assertThat(Files.size(path)).isGreaterThan(100L); }
@Test public void testNewByteChannelWrite() throws Exception { Path path = Paths.get(URI.create("gs://bucket/tests")); try (SeekableByteChannel output = Files.newByteChannel(path, WRITE)) { assertThat(output.position()).isEqualTo(0); assertThat(output.size()).isEqualTo(0); ByteBuffer buffer = ByteBuffer.wrap("filec".getBytes(UTF_8)); assertThat(output.write(buffer)).isEqualTo(5); assertThat(output.position()).isEqualTo(5); assertThat(output.size()).isEqualTo(5); buffer = ByteBuffer.wrap("onten".getBytes(UTF_8)); assertThat(output.write(buffer)).isEqualTo(5); assertThat(output.position()).isEqualTo(10); assertThat(output.size()).isEqualTo(10); } assertThat(new String(Files.readAllBytes(path), UTF_8)).isEqualTo("fileconten"); }
expectedSize += sBytes.length * 9999; for (int i = 0; i < 9999; i++) { chan.write(ByteBuffer.wrap(sBytes));
private void saveMpegFrames(SeekableByteChannel saveFile) throws IOException { int filePos = xingOffset; if (filePos < 0) filePos = startOffset; if (filePos < 0) return; if (endOffset < filePos) return; SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, StandardOpenOption.READ); ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength); try { seekableByteChannel.position(filePos); while (true) { byteBuffer.clear(); int bytesRead = seekableByteChannel.read(byteBuffer); byteBuffer.rewind(); if (filePos + bytesRead <= endOffset) { byteBuffer.limit(bytesRead); saveFile.write(byteBuffer); filePos += bytesRead; } else { byteBuffer.limit(endOffset - filePos + 1); saveFile.write(byteBuffer); break; } } } finally { seekableByteChannel.close(); } } }
public void save(String newFilename) throws IOException, NotSupportedException { if (path.toAbsolutePath().compareTo(Paths.get(newFilename).toAbsolutePath()) == 0) { throw new IllegalArgumentException("Save filename same as source filename"); } try (SeekableByteChannel saveFile = Files.newByteChannel(Paths.get(newFilename), EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE))) { if (hasId3v2Tag()) { ByteBuffer byteBuffer = ByteBuffer.wrap(id3v2Tag.toBytes()); byteBuffer.rewind(); saveFile.write(byteBuffer); } saveMpegFrames(saveFile); if (hasCustomTag()) { ByteBuffer byteBuffer = ByteBuffer.wrap(customTag); byteBuffer.rewind(); saveFile.write(byteBuffer); } if (hasId3v1Tag()) { ByteBuffer byteBuffer = ByteBuffer.wrap(id3v1Tag.toBytes()); byteBuffer.rewind(); saveFile.write(byteBuffer); } saveFile.close(); } }
public void extract(EmbeddedArtifact embeddedArtifact, Path directory) { final Path extractTo = directory.resolve(embeddedArtifact.getFilename()); final Path parent = extractTo.getParent(); try { if (parent != null) { Files.createDirectories(parent); } } catch (IOException e) { throw new RuntimeException(String.format("Couldn't extract %s, unable to create directory %s", embeddedArtifact.getName(), parent), e); } log.info("Extracting {} bytes of {} to {}", embeddedArtifact.getContent().length, embeddedArtifact.getName(), extractTo); try (SeekableByteChannel byteChannel = Files.newByteChannel(extractTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))) { byteChannel.write(ByteBuffer.wrap(embeddedArtifact.getContent())); } catch (IOException e) { throw new RuntimeException(String.format("Couldn't extract %s", embeddedArtifact.getName()), e); } checkMd5(embeddedArtifact, extractTo); }
@Override protected final void writeOut(final byte[] data, final int offset, final int length) throws IOException { channel.write(ByteBuffer.wrap(data, offset, length)); } }
try (SeekableByteChannel sbc = Files.newByteChannel(p, StandardOpenOption.APPEND)) { sbc.write(ByteBuffer.wrap(s.getBytes())); } catch (IOException e) { System.err.println(e); }
/** {@inheritDoc} */ @Override public boolean writeStream(byte[] b) { try { dataChannel.write(ByteBuffer.wrap(b)); return true; } catch (IOException e) { log.error("", e); } return false; }
@Override public void write(final int b) throws IOException { buffer.clear(); buffer.put((byte) b).flip(); channel.write(buffer); compressedCrc32.update(b); fileBytesWritten++; }
synchronized void write(long base, byte[] data) { try { channel.position(base); channel.write(ByteBuffer.wrap(data)); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Increase the size of the file up to a size that is a multiple of the specified value. * * @param multiple */ public synchronized void pad(int multiple) throws IOException { long padding = (multiple - channel.size() % multiple) % multiple; channel.position(channel.size()); channel.write(ByteBuffer.allocate((int) padding)); } }
@Test public void testWriteToReadOnlyChannelThrows() throws IOException { Path file = getFile(); try( SeekableByteChannel channel = FS.provider().newByteChannel( file, asSet( READ ) ) ) { assertThatThrownBy( () -> channel.write( ByteBuffer.allocate( 2 ) ) ).isInstanceOf( NonWritableChannelException.class ); } }
@Override public int write(ByteBuffer src) throws IOException { checkAllExceptions(); return delegate.write(src); }