Refine search
try { Path path = Paths.get(new URI(fname)); long size = Files.size(path); System.out.println(fname + ": " + size + " bytes."); ByteBuffer buf = ByteBuffer.allocate(bufSize); System.out.println("Reading the whole file..."); Stopwatch sw = Stopwatch.createStarted(); try (SeekableByteChannel chan = Files.newByteChannel(path)) { long total = 0; int readCalls = 0; MessageDigest md = MessageDigest.getInstance("MD5"); while (chan.read(buf) > 0) { readCalls++; md.update(buf.array(), 0, buf.position());
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
channel.position(pos); head.position(0); int c = channel.read(head); head.position(0); if (c == buffSize) { try (SeekableByteChannel channel2 = Files.newByteChannel(nFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE);) { odexHead.putInt(checksum); odexHead.position(0); channel2.write(odexHead); channel.position(pos + dexOffset); copy(channel, channel2, copyBuff, fileSize); channel2.write(ByteBuffer.allocate(nDepsPadding)); channel.position(pos + depsOffset); copy(channel, channel2, copyBuff, depsLength); channel2.write(ByteBuffer.allocate(nOptPadding)); channel.position(pos + optOffset); copy(channel, channel2, copyBuff, optLength);
/** * Skips the given number of bytes or throws an EOFException if * skipping failed. */ private void skipBytes(final int count) throws IOException { long currentPosition = archive.position(); long newPosition = currentPosition + count; if (newPosition > archive.size()) { throw new EOFException(); } archive.position(newPosition); }
@Test public void testCreateAndWrite() throws IOException { CloudStorageFileSystem testBucket = getTestBucket(); Path path = testBucket.getPath(PREFIX + randomSuffix()); // file shouldn't exist initially (see above). assertThat(Files.exists(path)).isFalse(); try { Files.createFile(path); Files.write(path, FILE_CONTENTS, UTF_8); // now it does. assertThat(Files.exists(path)).isTrue(); // let's check that the contents is OK. ByteArrayOutputStream wantBytes = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(wantBytes, UTF_8)); for (String content : FILE_CONTENTS) { writer.println(content); } writer.close(); SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ); byte[] gotBytes = new byte[(int) chan.size()]; readFully(chan, gotBytes); assertThat(gotBytes).isEqualTo(wantBytes.toByteArray()); } finally { // let's not leave files around Files.deleteIfExists(path); } }
@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 testNewByteChannelRead_seekBeyondSize_reportsEofOnNextRead() throws Exception { Path path = Paths.get(URI.create("gs://lol/cat")); Files.write(path, "hellocat".getBytes(UTF_8)); try (SeekableByteChannel input = Files.newByteChannel(path)) { ByteBuffer buffer = ByteBuffer.allocate(5); input.position(10); assertThat(input.read(buffer)).isEqualTo(-1); input.position(11); assertThat(input.read(buffer)).isEqualTo(-1); assertThat(input.size()).isEqualTo(8); } }
@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"); }
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); }
Path p = fillFile(fs, bytes, repeat); try (SeekableByteChannel chan = Files.newByteChannel(p, StandardOpenOption.READ)) { ByteBuffer buf = ByteBuffer.allocate(bytes.length); for (int i = 0; i < repeat; i++) { buf.clear(); for (int off = 0; off < bytes.length; ) { int read = chan.read(buf); if (read < 0) { int eof = chan.read(buf); assertWithMessage("EOF should return -1").that(eof).isEqualTo(-1); } finally { Files.delete(p);
@Test(timeout = 60_000) public void testReadByteChannel() throws IOException { CloudStorageFileSystem testBucket = getTestBucket(); Path path = testBucket.getPath(SML_FILE); long size = Files.size(path); SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ); assertThat(chan.size()).isEqualTo(size); ByteBuffer buf = ByteBuffer.allocate(SML_SIZE); int read = 0; while (chan.isOpen()) { int rc = chan.read(buf); assertThat(chan.size()).isEqualTo(size); if (rc < 0) { // EOF break; } assertThat(rc).isGreaterThan(0); read += rc; assertThat(chan.position()).isEqualTo(read); } assertThat(read).isEqualTo(size); byte[] expected = new byte[SML_SIZE]; new Random(SML_SIZE).nextBytes(expected); assertThat(Arrays.equals(buf.array(), expected)).isTrue(); }
@Test @Category( Writable.class ) public void testWriteBeyondFileSize() throws IOException { Path file = fileTA(); try( SeekableByteChannel out = FS.provider().newByteChannel( file, singleton( WRITE ) ) ) { out.position( 100 ); out.write( ByteBuffer.wrap( "ha".getBytes( "UTF-8" ) ) ); } assertThat( Files.size( file ) ).isEqualTo( 102L ); }
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(); } }
@Test public void testSeek() throws IOException { CloudStorageFileSystem testBucket = getTestBucket(); Path path = testBucket.getPath(BIG_FILE); int size = BIG_SIZE; byte[] contents = randomContents(size); byte[] sample = new byte[100]; byte[] wanted; byte[] wanted2; SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ); assertThat(chan.size()).isEqualTo(size); // check seek int dest = size / 2; chan.position(dest); readFully(chan, sample); wanted = Arrays.copyOfRange(contents, dest, dest + 100); assertThat(wanted).isEqualTo(sample); // now go back and check the beginning // (we do 2 locations because 0 is sometimes a special case). chan.position(0); readFully(chan, sample); wanted2 = Arrays.copyOf(contents, 100); assertThat(wanted2).isEqualTo(sample); // if the two spots in the file have the same contents, then this isn't a good file for this // test. assertThat(wanted).isNotEqualTo(wanted2); }
@ExpectWarning("OS_OPEN_STREAM") void test3(File f, byte b) throws IOException { SeekableByteChannel c = Files.newByteChannel(Paths.get("")); c.position(); }
@Test public void testReadFromClosedChannelThrows() throws IOException { try( SeekableByteChannel read = Files.newByteChannel( getFile() ) ) { read.close(); assertThatThrownBy( () -> read.read( ByteBuffer.allocate( 30 ) ) ). isInstanceOf( ClosedChannelException.class ); } }
/** * 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)); } }