001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io.delta;
016    
017    import java.io.IOException;
018    
019    import java.nio.ByteBuffer;
020    import java.nio.channels.ReadableByteChannel;
021    
022    /**
023     * @author Connor McKay
024     */
025    public class ByteChannelReader {
026    
027            public ByteChannelReader(ReadableByteChannel readableByteChannel)
028                    throws IOException {
029    
030                    this(readableByteChannel, 1024);
031            }
032    
033            public ByteChannelReader(
034                            ReadableByteChannel readableByteChannel, int bufferLength)
035                    throws IOException {
036    
037                    _readableByteChannel = readableByteChannel;
038    
039                    _byteBuffer = ByteBuffer.allocate(bufferLength);
040    
041                    if (_readableByteChannel.read(_byteBuffer) == -1) {
042                            _eof = true;
043                    }
044                    else {
045                            _eof = false;
046                    }
047    
048                    _byteBuffer.flip();
049            }
050    
051            public void ensureData(int length) throws IOException {
052                    if (_byteBuffer.remaining() < length) {
053                            read();
054    
055                            if (_eof || (_byteBuffer.remaining() < length)) {
056                                    throw new IOException("Unexpected EOF");
057                            }
058                    }
059            }
060    
061            public byte get() {
062                    return _byteBuffer.get();
063            }
064    
065            public byte get(int offset) {
066                    return _byteBuffer.get(_byteBuffer.position() + offset);
067            }
068    
069            public ByteBuffer getBuffer() {
070                    return _byteBuffer;
071            }
072    
073            public boolean hasRemaining() {
074                    return _byteBuffer.hasRemaining();
075            }
076    
077            public void maybeRead(int length) throws IOException {
078                    if (!_eof && (_byteBuffer.remaining() < length)) {
079                            read();
080                    }
081            }
082    
083            public void read() throws IOException {
084                    if (_eof) {
085                            return;
086                    }
087    
088                    _byteBuffer.compact();
089    
090                    if (_readableByteChannel.read(_byteBuffer) == -1) {
091                            _eof = true;
092                    }
093                    else {
094                            _eof = false;
095                    }
096    
097                    _byteBuffer.flip();
098            }
099    
100            public int remaining() {
101                    return _byteBuffer.remaining();
102            }
103    
104            public void resizeBuffer(int minBufferLength) {
105                    if (_byteBuffer.capacity() >= minBufferLength) {
106                            return;
107                    }
108    
109                    ByteBuffer newBuffer = ByteBuffer.allocate(minBufferLength);
110    
111                    newBuffer.put(_byteBuffer);
112                    newBuffer.flip();
113    
114                    _byteBuffer = newBuffer;
115            }
116    
117            public int skip(int length) {
118                    length = Math.min(_byteBuffer.remaining(), length);
119    
120                    _byteBuffer.position(_byteBuffer.position() + length);
121    
122                    return length;
123            }
124    
125            private ByteBuffer _byteBuffer;
126            private boolean _eof;
127            private ReadableByteChannel _readableByteChannel;
128    
129    }