Example usage for java.nio.channels SeekableByteChannel close

List of usage examples for java.nio.channels SeekableByteChannel close

Introduction

In this page you can find the example usage for java.nio.channels SeekableByteChannel close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this channel.

Usage

From source file:com.spectralogic.ds3client.helpers.FileObjectGetter_Test.java

@Test
public void testThatSymbolicLinksAreResolved() {
    Assume.assumeFalse(Platform.isWindows());
    final String message = "Hello World";
    final String file = "file.txt";
    try {/*from  w  w w.  j  a v a2s.c  o  m*/
        final Path tempDirectory = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")),
                "ds3");
        final Path realDirectory = Files.createDirectory(Paths.get(tempDirectory.toString(), "dir"));
        final Path symbolicDirectory = Paths.get(tempDirectory.toString(), "symbolic");
        Files.createSymbolicLink(symbolicDirectory, realDirectory);
        Files.createFile(Paths.get(realDirectory.toString(), file));
        final ByteBuffer bb = ByteBuffer.wrap(message.getBytes());

        final SeekableByteChannel getterChannel = new FileObjectGetter(symbolicDirectory).buildChannel(file);
        getterChannel.write(bb);
        getterChannel.close();
        final String content = new String(Files.readAllBytes(Paths.get(realDirectory.toString(), file)));
        assertTrue(message.equals(content));
    } catch (final IOException e) {
        fail("Symbolic links are not handled correctly");
    }
}

From source file:com.rogiel.httpchannel.service.impl.HotFileServiceTest.java

@Test
public void testNonLoguedInUploader() throws IOException {
    assertTrue("This service does not have the capability UploadCapability.FREE_UPLOAD",
            service.getUploadCapabilities().has(UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));

    final Path path = Paths.get("src/test/resources/upload-test-file.txt");
    final UploadChannel channel = UploadServices.upload(service, path).openChannel();
    final SeekableByteChannel inChannel = Files.newByteChannel(path);

    try {//from  w  w w  . jav  a 2  s .co m
        ChannelUtils.copy(inChannel, channel);
    } finally {
        inChannel.close();
        channel.close();
    }

    System.out.println(channel.getDownloadLink());
    Assert.assertNotNull(channel.getDownloadLink());
}

From source file:com.rogiel.httpchannel.service.impl.HotFileServiceTest.java

@Test
public void testLoguedInUploader() throws IOException {
    assertTrue("This service does not have the capability UploadCapability.PREMIUM_UPLOAD",
            service.getUploadCapabilities().has(UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));

    service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD)).login();

    final Path path = Paths.get("src/test/resources/upload-test-file.txt");
    final UploadChannel channel = UploadServices.upload(service, path).openChannel();
    final SeekableByteChannel inChannel = Files.newByteChannel(path);

    try {/*from ww  w  .j  a  v  a2s .c om*/
        ChannelUtils.copy(inChannel, channel);
    } finally {
        inChannel.close();
        channel.close();
    }

    System.out.println(channel.getDownloadLink());
    Assert.assertNotNull(channel.getDownloadLink());
}

From source file:com.rogiel.httpchannel.service.impl.MegaUploadServiceTest.java

@Test
public void testNonLoguedInUploader() throws IOException {
    assertTrue("This service does not have the capability UploadCapability.FREE_UPLOAD",
            service.getUploadCapabilities().has(UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
    final Path path = Paths.get("src/test/resources/upload-test-file.txt");
    final UploadChannel channel = UploadServices.upload(service, path).openChannel();
    final SeekableByteChannel inChannel = Files.newByteChannel(path);

    try {//  w  ww. jav  a 2  s.  c  o m
        ChannelUtils.copy(inChannel, channel);
    } finally {
        inChannel.close();
        channel.close();
    }

    System.out.println(channel.getDownloadLink());
    Assert.assertNotNull(channel.getDownloadLink());
}

From source file:com.spectralogic.ds3client.helpers.channels.WindowedSeekableByteChannel_Test.java

@Test(timeout = 1000)
public void closeChangesIsOpen() throws IOException {
    try (final SeekableByteChannel channel = stringToChannel("aabbbcccc")) {
        final Object lock = new Object();
        final SeekableByteChannel window = new WindowedSeekableByteChannel(channel, lock, 0L, 2L);
        assertThat(window.isOpen(), is(true));
        window.close();
        assertThat(window.isOpen(), is(false));
    }//w  w  w .  j  av a2 s  . com
}

From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java

/**
 * Writes file to disk and copy the contents of the input byte array.
 *
 * @param file     a Path with file path.
 * @param contents a byte[] with the contents
 * @throws IOException// www.j  a va 2  s.c o m
 */
private void writeFile(final Path file, final byte[] contents) throws IOException {
    final ByteBuffer bb = ByteBuffer.wrap(contents);
    //Create the file
    final SeekableByteChannel sbc = Files.newByteChannel(file, FILE_OPEN_OPTIONS);
    //Copy contents to the new File
    sbc.write(bb);
    //Close the byte channel
    sbc.close();
}

From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java

private void replaceFile(final Path file, final byte[] contents) throws IOException {
    final ByteBuffer bb = ByteBuffer.wrap(contents);
    //Create the file
    final SeekableByteChannel sbc = Files.newByteChannel(file, FILE_REPLACE_OPTIONS);
    //Copy contents to the new File
    sbc.write(bb);/*from ww  w .jav a2  s.c o m*/
    //Close the byte channel
    sbc.close();
}

From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java

private void replaceFile(final Path file, final URL url) throws IOException {
    //Create the file
    SeekableByteChannel sbc = null;
    try {//  www .j  a  va  2  s  .co  m
        //Creates a new Readable Byte channel from URL
        final ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        //Create the file
        sbc = Files.newByteChannel(file, FILE_REPLACE_OPTIONS);

        //Clears the buffer
        buffer.clear();

        //Read input Channel
        while (rbc.read(buffer) != -1) {
            // prepare the buffer to be drained
            buffer.flip();
            // write to the channel, may block
            sbc.write(buffer);
            // If partial transfer, shift remainder down
            // If buffer is empty, same as doing clear()
            buffer.compact();
        }
        // EOF will leave buffer in fill state
        buffer.flip();
        // make sure the buffer is fully drained.
        while (buffer.hasRemaining()) {
            sbc.write(buffer);
        }
    } finally {
        if (sbc != null) {
            sbc.close();
        }
    }
}

From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java

/**
 * Writes file to disk and copy the contents of the input byte array.
 *
 * @param file a Path with file path.//  ww  w  .j  av  a2  s  .c  o  m
 * @param url  a remote/local url to be saved as file
 * @throws IOException
 */
private void writeFile(final Path file, final URL url) throws IOException {

    if (url.toString().startsWith("jar:file:///")) {
        try {
            writeZipFile(file, url);
        } catch (Exception e) {
            throw new IOException(e);
        }
    } else {

        //Create the file
        SeekableByteChannel sbc = null;
        try {
            //Creates a new Readable Byte channel from URL
            final ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            //Create the file
            sbc = Files.newByteChannel(file, FILE_OPEN_OPTIONS);

            //Clears the buffer
            buffer.clear();

            //Read input Channel
            while (rbc.read(buffer) != -1) {
                // prepare the buffer to be drained
                buffer.flip();
                // write to the channel, may block
                sbc.write(buffer);
                // If partial transfer, shift remainder down
                // If buffer is empty, same as doing clear()
                buffer.compact();
            }
            // EOF will leave buffer in fill state
            buffer.flip();
            // make sure the buffer is fully drained.
            while (buffer.hasRemaining()) {
                sbc.write(buffer);
            }
        } finally {
            if (sbc != null) {
                sbc.close();
            }
        }
    }
}