Java 8 FileChannel - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 FileChannel class.

This code example shows how to use the following methods:size, close
This code example shows how to use the following methods:read
        bb.limit(Math.min(off + len, bb.capacity()));
        bb.position(off);
 
        this.bb = bb;
        this.bs = bs;
        int read = channel.read(bb, position);
        if (read > 0) {
            position += read;
        } 
        return read;
    } 
 
    @Override 
    public boolean markSupported() { 
        return true; 
    } 
 
    @Override 
    public void mark(int readlimit) {
        this.markPosition = position;
7
CodeRank
This code example shows how to use the following methods:size, transferTo, close
This code example shows how to use the following methods:size
     * @param length          number of bytes to read 
     */ 
    public static void readFromFileChannelWithEofException(FileChannel channel, long channelPosition, byte[] dest, int destOffset, int length) throws IOException {
        int read = readFromFileChannel(channel, channelPosition, dest, destOffset, length);
        if (read < 0) {
            throw new EOFException("read past EOF. pos [" + channelPosition + "] length: [" + length + "] end: [" + channel.size() + "]");
        } 
    } 
 
    /** 
     * read <i>length</i> bytes from <i>position</i> of a file channel. 
     * 
     * @param channel         channel to read from 
     * @param channelPosition position to read from 
     * @param dest            destination byte array to put data in 
     * @param destOffset      offset in dest to read into 
     * @param length          number of bytes to read 
     * @return total bytes read or -1 if an attempt was made to read past EOF. The method always tries to read all the bytes 
     * that will fit in the destination byte buffer. 
     */ 
7
CodeRank
This code example shows how to use the following methods:write
            // delay buffer write.. 
            int willActuallyWrite = randomInt(src.remaining());
            ByteBuffer mockSrc = src.duplicate();
            mockSrc.limit(mockSrc.position() + willActuallyWrite);
            try { 
                return delegate.write(mockSrc);
            } finally { 
                src.position(mockSrc.position());
            } 
        } 
 
        @Override 
        public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
            return delegate.write(srcs, offset, length);
        } 
 
        @Override 
        public long position() throws IOException {
            return delegate.position();
        } 
7
CodeRank
This code example shows how to use the following methods:force, close, isOpen
    } 
 
 
    @Override 
    public void flush() throws IOException { 
        this.channel.force(true);
        this.highWaterMark.set(this.sizeInBytes.get());
    } 
 
 
    /** 
     * just for test 
     *  
     * @param newValue 
     */ 
    void setSizeInBytes(final long newValue) {
        this.sizeInBytes.set(newValue);
    } 
 
 

7
CodeRank
This code example shows how to use the following methods:position, truncate
  /** 
   * Create empty edits logs file. 
   */ 
  @Override 
  public void create() throws IOException { 
    fc.truncate(0);
    fc.position(0);
    writeHeader(doubleBuf.getCurrentBuf());
    setReadyToFlush(); 
    flush(); 
  } 
 
  /** 
   * Write header information for this EditLogFileOutputStream to the provided 
   * DataOutputSream. 
   *  
   * @param out the output stream to write the header to. 
   * @throws IOException in the event of error writing to the stream. 
   */ 
  @VisibleForTesting 
7
CodeRank
This code example shows how to use the following methods:close
  } 
   
  @Override 
  public void close() throws IOException { 
    if (ch != null) {
      ch.close();
    } 
  } 
} 
7
CodeRank
This code example shows how to use the following methods:size
        buffer = new byte[bufferSize];
 
        // we can cache file length in read-only mode 
        try 
        { 
            fileLength = channel.size();
        } 
        catch (IOException e)
        { 
            throw new FSReadError(e, filePath);
        } 
        validBufferBytes = -1; // that will trigger reBuffer() on demand by read/seek operations
    } 
 
    public static RandomAccessReader open(File file, PoolingSegmentedFile owner)
    { 
        return open(file, DEFAULT_BUFFER_SIZE, owner);
    } 
 
    public static RandomAccessReader open(File file)
7
CodeRank
This code example shows how to use the following methods:force, close
    } 
 
 
    @Override 
    public synchronized void close() throws IOException { 
        this.fc.force(true);
        this.fc.close();
    } 
 
 
    public void write(final Object obj) throws Exception {
        if (!this.fc.isOpen()) {
            return; 
        } 
        if (obj == null) {
            return; 
        } 
        final byte[] result = this.serializer.encodeObject(obj);
        final ByteBuffer buf = ByteBuffer.wrap(result);
 
7
CodeRank

Related Java 8 FileChannel Questions & Answers:

See Code Examples for Java 8 FileChannel Methods: