Search in sources :

Example 11 with SeekableByteChannel

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");
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 12 with SeekableByteChannel

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);
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 13 with SeekableByteChannel

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");
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 14 with SeekableByteChannel

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);
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 15 with SeekableByteChannel

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);
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.Test)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)130 ByteBuffer (java.nio.ByteBuffer)58 Path (java.nio.file.Path)48 IOException (java.io.IOException)42 Test (org.junit.Test)33 InputStream (java.io.InputStream)14 NoSuchFileException (java.nio.file.NoSuchFileException)12 Test (org.testng.annotations.Test)9 ReadableByteChannel (java.nio.channels.ReadableByteChannel)8 OpenOption (java.nio.file.OpenOption)7 StandardOpenOption (java.nio.file.StandardOpenOption)7 HashSet (java.util.HashSet)7 File (java.io.File)6 FileSystem (java.nio.file.FileSystem)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 URI (java.net.URI)5 RandomAccessData (org.apache.beam.runners.dataflow.util.RandomAccessData)5 OutputStream (java.io.OutputStream)4 FileChannel (java.nio.channels.FileChannel)4 WritableByteChannel (java.nio.channels.WritableByteChannel)4