|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Copyright 2011-2012 Tavendo GmbH |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | +package de.tavendo.autobahn; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.nio.ByteBuffer; |
| 24 | + |
| 25 | +/** |
| 26 | + * InputStream wrapping a ByteBuffer. This class can be used i.e. to wrap |
| 27 | + * ByteBuffers allocated direct in NIO for socket I/O. The class does not |
| 28 | + * allocate ByteBuffers itself, but assumes the user has already one that |
| 29 | + * just needs to be wrapped to use with InputStream based processing. |
| 30 | + */ |
| 31 | +public class ByteBufferInputStream extends InputStream { |
| 32 | + |
| 33 | + /// ByteBuffer backing this input stream. |
| 34 | + private final ByteBuffer mBuffer; |
| 35 | + |
| 36 | + /** |
| 37 | + * Create input stream over ByteBuffer. |
| 38 | + * |
| 39 | + * @param buffer ByteBuffer to wrap as input stream. |
| 40 | + */ |
| 41 | + public ByteBufferInputStream(ByteBuffer buffer) { |
| 42 | + mBuffer = buffer; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Read one byte from input stream and advance. |
| 47 | + * |
| 48 | + * @return Byte read or -1 when stream end reached. |
| 49 | + */ |
| 50 | + @Override |
| 51 | + public synchronized int read() throws IOException { |
| 52 | + |
| 53 | + if (!mBuffer.hasRemaining()) { |
| 54 | + return -1; |
| 55 | + } else { |
| 56 | + return mBuffer.get() & 0xFF; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Read chunk of bytes from input stream and advance. Read either as many |
| 62 | + * bytes specified or input stream end reached. |
| 63 | + * |
| 64 | + * @param bytes Read bytes into byte array. |
| 65 | + * @param off Read bytes into byte array beginning at this offset. |
| 66 | + * @param len Read at most this many bytes. |
| 67 | + * @return Actual number of bytes read. |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public synchronized int read(byte[] bytes, int off, int len) |
| 71 | + throws IOException { |
| 72 | + |
| 73 | + if (bytes == null) { |
| 74 | + throw new NullPointerException(); |
| 75 | + } else if (off < 0 || len < 0 || len > bytes.length - off) { |
| 76 | + throw new IndexOutOfBoundsException(); |
| 77 | + } else if (len == 0) { |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | + int length = Math.min(mBuffer.remaining(), len); |
| 82 | + if (length == 0) { |
| 83 | + return -1; |
| 84 | + } |
| 85 | + |
| 86 | + mBuffer.get(bytes, off, length); |
| 87 | + return length; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments