Examples of SeekableByteChannel


Examples of java.nio.channels.SeekableByteChannel

    super.close();
  }

  private SeekableByteChannel openByteChannelAtCurrentPosition() {
    try {
      SeekableByteChannel channel = Files.newByteChannel(logfile, StandardOpenOption.READ);
      Optional<Long> previousPosition = store.getPosition(tailMetadata);
      if (previousPosition.isPresent()) {
        long storePosition = previousPosition.get();

        if (storePosition < channel.size()) {
          LOG.warn("Found {} with size {} and position {}, resetting to 0", logfile, channel.size(), storePosition);
          savePosition(0);
        } else {
          channel.position(previousPosition.get());
        }
      }
      return channel;
    } catch (IOException e) {
      throw Throwables.propagate(e);
View Full Code Here

Examples of java.nio.channels.SeekableByteChannel

  @Test
  public void testOpenChannelsClosed() throws IOException {
    Path p = fs.getPath("/foo");
    FileChannel fc = FileChannel.open(p, READ, WRITE, CREATE);
    SeekableByteChannel sbc = Files.newByteChannel(p, READ);
    AsynchronousFileChannel afc = AsynchronousFileChannel.open(p, READ, WRITE);

    assertTrue(fc.isOpen());
    assertTrue(sbc.isOpen());
    assertTrue(afc.isOpen());

    fs.close();

    assertFalse(fc.isOpen());
    assertFalse(sbc.isOpen());
    assertFalse(afc.isOpen());

    try {
      fc.size();
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
      sbc.size();
      fail();
    } catch (ClosedChannelException expected) {
    }

    try {
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

        final String userBasedPath = System.getProperty( "user.dir" ) + "/byte_some_file_here.txt";

        final Path path = GeneralPathImpl.create( fsProvider.getFileSystem( URI.create( "file:///" ) ), userBasedPath, false );
        assertThat( path.toFile().exists() ).isFalse();

        final SeekableByteChannel channel = fsProvider.newByteChannel( path, null );

        assertThat( channel ).isNotNull();
        assertThat( path.toFile().exists() ).isTrue();
        path.toFile().delete();
        assertThat( path.toFile().exists() ).isFalse();
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

        final String userBasedPath = System.getProperty( "user.dir" ) + "/my_byte_some_file_here.txt";

        final Path path = GeneralPathImpl.create( fsProvider.getFileSystem( URI.create( "file:///" ) ), userBasedPath, false );
        assertThat( path.toFile().exists() ).isFalse();

        final SeekableByteChannel channel = fsProvider.newByteChannel( path, null );

        assertThat( channel ).isNotNull();
        assertThat( path.toFile().exists() ).isTrue();

        assertThat( channel.isOpen() ).isTrue();

        channel.close();

        assertThat( channel.isOpen() ).isFalse();

//        try {
//            channel.position();
//            fail( "method not implemented - exception expected!" );
//        } catch ( NotImplementedException ex ) {
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

    @Test
    public void test() throws IOException {
        final File tempFile = File.createTempFile( "foo", "bar" );

        final SeekableByteChannel sbc = new SeekableByteChannelFileBasedImpl( new RandomAccessFile( tempFile, "rw" ).getChannel() );

        assertTrue( sbc.isOpen() );
        sbc.write( ByteBuffer.wrap( "CONTENT\n?!".getBytes() ) );

        sbc.position( 0L );
        ByteBuffer buffer = ByteBuffer.allocate( 10 );
        sbc.read( buffer );
        assertEquals( "CONTENT\n?!", new String( buffer.array() ) );

        sbc.close();
        assertFalse( sbc.isOpen() );

    }
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

    @Test
    public void testPosition() throws IOException {
        final File tempFile = File.createTempFile( "foo", "bar" );

        final SeekableByteChannel sbc = new SeekableByteChannelFileBasedImpl( new RandomAccessFile( tempFile, "rw" ).getChannel() );

        assertTrue( sbc.isOpen() );
        sbc.write( ByteBuffer.wrap( "CONTENT\n?!".getBytes() ) );

        assertEquals( 10L, sbc.position() );
        assertEquals( 10L, sbc.size() );

        sbc.position( 1L );
        ByteBuffer buffer = ByteBuffer.allocate( 8 );
        sbc.read( buffer );
        assertEquals( "ONTENT\n?", new String( buffer.array() ) );

        assertEquals( 9L, sbc.position() );

        sbc.position( 0L );
        sbc.truncate( 2L );
        ByteBuffer buffer3 = ByteBuffer.allocate( 2 );
        sbc.read( buffer3 );
        assertEquals( "CO", new String( buffer3.array() ) );

        sbc.close();
        assertFalse( sbc.isOpen() );

    }
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

    }

    @Override
    public SeekableByteChannel newByteChannel( final Path path,
                                               final OpenOption... options ) throws IllegalArgumentException, UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        final SeekableByteChannel sbc = service.newByteChannel( path, options );

        return new SeekableByteChannelWrapperImpl( sbc ) {
            @Override
            public void close() throws java.io.IOException {
                new FileSystemSyncLock<Void>( service.getId(), path.getFileSystem() ).execute( clusterService, new FutureTask<Void>( new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        sbc.close();
                        return null;
                    }
                } ) );
            }
        };
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

    @Override
    public SeekableByteChannel newByteChannel( final Path path,
                                               final Set<? extends OpenOption> options,
                                               final FileAttribute<?>... attrs ) throws IllegalArgumentException, UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        final SeekableByteChannel sbc = service.newByteChannel( path, options, attrs );

        return new SeekableByteChannelWrapperImpl( sbc ) {
            @Override
            public void close() throws java.io.IOException {
                new FileSystemSyncLock<Void>( service.getId(), path.getFileSystem() ).execute( clusterService, new FutureTask<Void>( new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        sbc.close();
                        return null;
                    }
                } ) );
            }
        };
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

        if ( exists( dot( path ) ) ) {
            properties.load( newInputStream( dot( path ) ) );
        }
        final FileAttribute<?>[] allAttrs = consolidate( properties, attrs );

        final SeekableByteChannel result = Files.newByteChannel( path, buildOptions( options ), allAttrs );

        if ( isFileScheme( path ) ) {
            buildDotFile( path, newOutputStream( dot( path ) ), allAttrs );
        }
View Full Code Here

Examples of org.uberfire.java.nio.channels.SeekableByteChannel

    public synchronized Path write( final Path path,
                                    final byte[] bytes,
                                    final Set<? extends OpenOption> options,
                                    final FileAttribute<?>... attrs ) throws IllegalArgumentException, IOException, UnsupportedOperationException {
        waitFSUnlock( path );
        SeekableByteChannel byteChannel;
        try {
            byteChannel = newByteChannel( path, buildOptions( options ), attrs );
        } catch ( final FileAlreadyExistsException ex ) {
            ( (AbstractPath) path ).clearCache();
            byteChannel = newByteChannel( path, buildOptions( options, TRUNCATE_EXISTING ), attrs );
        }

        try {
            byteChannel.write( ByteBuffer.wrap( bytes ) );
            byteChannel.close();
        } catch ( final java.io.IOException e ) {
            throw new IOException( e );
        }

        return path;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.