Search in sources :

Example 1 with CloudStorageFileSystem

use of com.google.cloud.storage.contrib.nio.CloudStorageFileSystem 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 2 with CloudStorageFileSystem

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

the class ITGcsNio method testCreate.

@Test
public void testCreate() 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.createFile(path);
        // now it does, and it has size 0.
        assertThat(Files.exists(path)).isTrue();
        long size = Files.size(path);
        assertThat(size).isEqualTo(0);
    } finally {
        // let's not leave files around
        Files.deleteIfExists(path);
    }
}
Also used : Path(java.nio.file.Path) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) Test(org.junit.Test)

Example 3 with CloudStorageFileSystem

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

the class ITGcsNio method testCopy.

@Test
public void testCopy() throws IOException {
    CloudStorageFileSystem testBucket = getTestBucket();
    Path src = testBucket.getPath(SML_FILE);
    Path dst = testBucket.getPath(PREFIX + randomSuffix());
    // file shouldn't exist initially (see above).
    assertThat(Files.exists(dst)).isFalse();
    try {
        Files.copy(src, dst);
        assertThat(Files.exists(dst)).isTrue();
        assertThat(Files.size(dst)).isEqualTo(SML_SIZE);
        byte[] got = new byte[SML_SIZE];
        readFully(Files.newByteChannel(dst), got);
        assertThat(got).isEqualTo(randomContents(SML_SIZE));
    } finally {
        // let's not leave files around
        Files.deleteIfExists(dst);
    }
}
Also used : Path(java.nio.file.Path) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) Test(org.junit.Test)

Example 4 with CloudStorageFileSystem

use of com.google.cloud.storage.contrib.nio.CloudStorageFileSystem 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)

Example 5 with CloudStorageFileSystem

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

the class CreateCloudStorageFileSystem method main.

public static void main(String... args) throws IOException {
    // Create a file system for the bucket
    CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("bucket");
    byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
    Path path = fs.getPath("/object");
    // Write a file in the bucket
    Files.write(path, data);
    // Read a file from the bucket
    data = Files.readAllBytes(path);
}
Also used : Path(java.nio.file.Path) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)

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