use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProviderTest method testNewByteChannelRead_seeking.
@Test
public void testNewByteChannelRead_seeking() throws Exception {
Path path = Paths.get(URI.create("gs://lol/cat"));
Files.write(path, "helloworld".getBytes(UTF_8));
try (SeekableByteChannel input = Files.newByteChannel(path)) {
ByteBuffer buffer = ByteBuffer.allocate(5);
input.position(5);
assertThat(input.position()).isEqualTo(5);
assertThat(input.read(buffer)).isEqualTo(5);
assertThat(input.position()).isEqualTo(10);
assertThat(new String(buffer.array(), UTF_8)).isEqualTo("world");
buffer.rewind();
assertThat(input.read(buffer)).isEqualTo(-1);
input.position(0);
assertThat(input.position()).isEqualTo(0);
assertThat(input.read(buffer)).isEqualTo(5);
assertThat(input.position()).isEqualTo(5);
assertThat(new String(buffer.array(), UTF_8)).isEqualTo("hello");
}
}
use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProviderTest method testNewByteChannelRead_seekBeyondSize_reportsEofOnNextRead.
@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);
}
}
use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProviderTest method testNewByteChannelWrite.
@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");
}
use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITGcsNio method testCreateAndWrite.
@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);
}
}
use of java.nio.channels.SeekableByteChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITGcsNio method testWriteOnClose.
@Test
public void testWriteOnClose() throws Exception {
CloudStorageFileSystem testBucket = getTestBucket();
Path path = testBucket.getPath(PREFIX + randomSuffix());
// file shouldn't exist initially (see above)
assertThat(Files.exists(path)).isFalse();
try {
long expectedSize = 0;
try (SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.WRITE)) {
// writing lots of contents to defeat channel-internal buffering.
for (String s : FILE_CONTENTS) {
byte[] sBytes = s.getBytes(UTF_8);
expectedSize += sBytes.length * 9999;
for (int i = 0; i < 9999; i++) {
chan.write(ByteBuffer.wrap(sBytes));
}
}
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.
Assert.fail("Files.size should have thrown an exception");
} 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)).isEqualTo(expectedSize);
} finally {
Files.deleteIfExists(path);
}
}
Aggregations