Search in sources :

Example 6 with CloudStorageFileSystem

use of com.google.cloud.storage.contrib.nio.CloudStorageFileSystem in project google-cloud-java by GoogleCloudPlatform.

the class ITGcsNio method testWrite.

@Test
public void testWrite() throws IOException {
    CloudStorageFileSystem testBucket = getTestBucket();
    Path path = testBucket.getPath(PREFIX + randomSuffix());
    // file shouldn't exist initially. If it does it's either because it's a leftover
    // from a previous run (so we should delete the file)
    // or because we're misconfigured and pointing to an actually important file
    // (so we should absolutely not delete it).
    // So if the file's here, don't try to fix it automatically, let the user deal with it.
    assertThat(Files.exists(path)).isFalse();
    try {
        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 7 with CloudStorageFileSystem

use of com.google.cloud.storage.contrib.nio.CloudStorageFileSystem in project google-cloud-java by GoogleCloudPlatform.

the class ITGcsNio method testFileSize.

@Test
public void testFileSize() throws IOException {
    CloudStorageFileSystem testBucket = getTestBucket();
    Path path = testBucket.getPath(SML_FILE);
    assertThat(Files.size(path)).isEqualTo(SML_SIZE);
}
Also used : Path(java.nio.file.Path) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) Test(org.junit.Test)

Example 8 with CloudStorageFileSystem

use of com.google.cloud.storage.contrib.nio.CloudStorageFileSystem in project google-cloud-java by GoogleCloudPlatform.

the class ITGcsNio method testSeek.

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

Example 9 with CloudStorageFileSystem

use of com.google.cloud.storage.contrib.nio.CloudStorageFileSystem in project google-cloud-java by GoogleCloudPlatform.

the class ITGcsNio method testReadByteChannel.

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

Example 10 with CloudStorageFileSystem

use of com.google.cloud.storage.contrib.nio.CloudStorageFileSystem in project google-cloud-java by GoogleCloudPlatform.

the class ITGcsNio method testFileExists.

@Test
public void testFileExists() throws IOException {
    CloudStorageFileSystem testBucket = getTestBucket();
    Path path = testBucket.getPath(SML_FILE);
    assertThat(Files.exists(path)).isTrue();
}
Also used : Path(java.nio.file.Path) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) Test(org.junit.Test)

Aggregations

CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)10 Path (java.nio.file.Path)10 Test (org.junit.Test)9 SeekableByteChannel (java.nio.channels.SeekableByteChannel)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 ByteBuffer (java.nio.ByteBuffer)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Random (java.util.Random)1