Code example for FileChannel

Methods: close, force, isOpen

0
    } 
 
 
    @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);
    } 
 
 
    /** 
     * just for test 
     *  
     * @param waterMark 
     */ 
    void setHighWaterMarker(final long waterMark) {
        this.highWaterMark.set(waterMark);
    } 
 
 
    /** 
     * Just for test 
     *  
     * @return 
     */ 
    long getOffset() { 
        return this.offset;
    } 
 
 
    /** 
     * Just for test 
     *  
     * @return 
     */ 
    public long getSizeInBytes() { 
        return this.sizeInBytes.get();
    } 
 
 
    FileChannel getFileChannel() {
        return this.channel;
    } 
 
 
    /** 
     * ·µ»ØÒ»¸öMessageSet¾µÏñ£¬Ö¸¶¨offsetºÍ³¤¶È 
     */ 
    @Override 
    public MessageSet slice(final long offset, final long limit) throws IOException {
        return new FileMessageSet(this.channel, offset, limit, false);
    } 
 
    static final Log transferLog = LogFactory.getLog("TransferLog");
 
 
    @Override 
    public void read(final ByteBuffer bf, final long offset) throws IOException {
        int size = 0;
        while (bf.hasRemaining()) {
            final int l = this.channel.read(bf, offset + size);
            if (l < 0) {
                break; 
            } 
            size += l;
        } 
    } 
 
 
    @Override 
    public void read(final ByteBuffer bf) throws IOException {
        this.read(bf, this.offset);
    } 
 
 
    @Override 
    public void write(final GetCommand getCommand, final SessionContext ctx) {
        final IoBuffer buf = this.makeHead(getCommand.getOpaque(), this.sizeInBytes.get());
        // transfer to socket 
        this.tryToLogTransferInfo(getCommand, ctx.getConnection());
        ctx.getConnection().transferFrom(buf, null, this.channel, this.offset, this.sizeInBytes.get());
    } 
 
 
    public long write(final WritableByteChannel socketChanel) throws IOException {
        try { 
            return this.getFileChannel().transferTo(this.offset, this.getSizeInBytes(), socketChanel);
        } 
        catch (final IOException e) {
            // Check to see if the IOException is being thrown due to 
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103988 
            final String message = e.getMessage();
            if (message != null && message.contains("temporarily unavailable")) {
                return 0; 
            } 
            throw e;
        } 
    } 
 
 
    private void tryToLogTransferInfo(final GetCommand getCommand, final Connection conn) {
        if (transferLog.isDebugEnabled()) {
            final StringBuilder sb = new StringBuilder("TransferLog[\r\n");
            sb.append("topic:").append(getCommand.getTopic()).append("\r\n");
            sb.append("group:").append(getCommand.getGroup()).append("\r\n");
            sb.append("partition:").append(getCommand.getPartition()).append("\r\n");
            sb.append("offset:").append(this.offset).append("\r\n");
            sb.append("sizeInBytes:").append(this.sizeInBytes.get()).append("\r\n");
            final String addrString =
                    conn != null ? RemotingUtils.getAddrString(conn.getRemoteSocketAddress()) : "unknown";
                    sb.append("client:").append(addrString).append("\r\n");
                    sb.append("]\r\n");
                    transferLog.debug(sb.toString());
        } 
    } 
 
 
    // value totalLen opaque\r\n 
    IoBuffer makeHead(final int opaque, final long size) {
        final IoBuffer buf = IoBuffer.allocate(9 + ByteUtils.stringSize(opaque) + ByteUtils.stringSize(size));
        ByteUtils.setArguments(buf, "value", size, opaque);
        buf.flip();
        return buf;
    } 
 
 
    @Override 
    public void close() throws IOException { 
        if (!this.channel.isOpen()) {
            return; 
        } 
        if (this.mutable) {
            this.flush();
        } 
        this.channel.close();
    } 
 
 
    FileChannel channel() {
        return this.channel;