| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 9 | * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 10 | * |
| 11 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 12 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 14 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 15 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 16 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 17 | */ |
| 18 |
| 19 | package jawnae.pyronet; |
| 20 |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 |
| 25 | public class ByteStream { |
| 26 | private final List<ByteBuffer> queue; |
| 27 |
| 28 | public ByteStream() { |
| 29 | // the queue is expected to be relatively small, and iterated often. |
| 30 | // hence removing the first element will be fast, even when using an |
| 31 | // ArrayList |
| 32 | this.queue = new ArrayList<ByteBuffer>(); |
| 33 | } |
| 34 |
| 35 | /** |
| 36 | * Appends the ByteBuffer instance to the ByteStream. The bytes are not |
| 37 | * copied, so do not modify the contents of the ByteBuffer. |
| 38 | */ |
| 39 |
| 40 | public void append(ByteBuffer buf) { |
| 41 | if (buf == null) |
| 42 | throw new NullPointerException(); |
| 43 | this.queue.add(buf); |
| 44 | } |
| 45 |
| 46 | /** |
| 47 | * Returns whether there are any bytes pending in this stream |
| 48 | */ |
| 49 |
| 50 | public boolean hasData() { |
| 51 | int size = this.queue.size(); |
| 52 | for (ByteBuffer aQueue : this.queue) { |
| 53 | if (aQueue.hasRemaining()) { |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 |
| 60 | public int getByteCount() { |
| 61 | int size = this.queue.size(); |
| 62 |
| 63 | int sum = 0; |
| 64 | for (ByteBuffer aQueue : this.queue) { |
| 65 | sum += aQueue.remaining(); |
| 66 | } |
| 67 | return sum; |
| 68 | } |
| 69 |
| 70 | /** |
| 71 | * Fills the specified buffer with as much bytes as possible. When N bytes |
| 72 | * are read, the buffer position will be increased by N |
| 73 | */ |
| 74 |
| 75 | public void get(ByteBuffer dst) { |
| 76 | if (dst == null) { |
| 77 | throw new NullPointerException(); |
| 78 | } |
| 79 |
| 80 | for (ByteBuffer data: this.queue) { |
| 81 | // data pos/lim must not be modified |
| 82 | data = data.slice(); |
| 83 |
| 84 | if (data.remaining() > dst.remaining()) { |
| 85 | data.limit(dst.remaining()); |
| 86 | dst.put(data); |
| 87 | break; |
| 88 | } |
| 89 |
| 90 | dst.put(data); |
| 91 |
| 92 | if (!dst.hasRemaining()) { |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 |
| 98 | /** |
| 99 | * Discards the specified amount of bytes from the stream. |
| 100 | * |
| 101 | * @throws PyroException |
| 102 | * if it failed to discard the specified number of bytes |
| 103 | */ |
| 104 |
| 105 | public void discard(int count) { |
| 106 | int original = count; |
| 107 |
| 108 | while (count > 0) { |
| 109 | // peek at the first buffer |
| 110 | ByteBuffer data = this.queue.get(0); |
| 111 |
| 112 | if (count < data.remaining()) { |
| 113 | // discarding less bytes than remaining in buffer |
| 114 | data.position(data.position() + count); |
| 115 | count = 0; |
| 116 | break; |
| 117 | } |
| 118 |
| 119 | // discard the first buffer |
| 120 | this.queue.remove(0); |
| 121 | count -= data.remaining(); |
| 122 | } |
| 123 |
| 124 | if (count != 0) { |
| 125 | // apparantly we cannot discard the amount of bytes |
| 126 | // the user demanded, this is a bug in other code |
| 127 | throw new PyroException("discarded " + (original - count) + "/" |
| 128 | + original + " bytes"); |
| 129 | } |
| 130 | } |
| 131 |
| 132 | public byte read() { |
| 133 | ByteBuffer data = this.queue.get(0); |
| 134 | byte result = data.get(); |
| 135 | if (!data.hasRemaining()) { |
| 136 | // discard the first buffer |
| 137 | this.queue.remove(0); |
| 138 | } |
| 139 | return result; |
| 140 | } |
| 141 |
| 142 | } |
| 143 |