| 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 org.telegram.messenger.BuffersStorage; |
| 22 | import org.telegram.messenger.ByteBufferDesc; |
| 23 |
| 24 | import java.nio.ByteBuffer; |
| 25 | import java.util.ArrayList; |
| 26 |
| 27 | public class ByteStream { |
| 28 | private final ArrayList<ByteBufferDesc> queue; |
| 29 |
| 30 | public ByteStream() { |
| 31 | this.queue = new ArrayList<ByteBufferDesc>(); |
| 32 | } |
| 33 |
| 34 | public void append(ByteBufferDesc buf) { |
| 35 | if (buf == null) { |
| 36 | throw new NullPointerException(); |
| 37 | } |
| 38 | this.queue.add(buf); |
| 39 | } |
| 40 |
| 41 | public boolean hasData() { |
| 42 | int size = this.queue.size(); |
| 43 | for (ByteBufferDesc aQueue : this.queue) { |
| 44 | if (aQueue.hasRemaining()) { |
| 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 |
| 51 | public void get(ByteBuffer dst) { |
| 52 | if (dst == null) { |
| 53 | throw new NullPointerException(); |
| 54 | } |
| 55 |
| 56 | for (ByteBufferDesc bufferDesc : this.queue) { |
| 57 | ByteBuffer data = bufferDesc.buffer.slice(); |
| 58 |
| 59 | if (data.remaining() > dst.remaining()) { |
| 60 | data.limit(dst.remaining()); |
| 61 | dst.put(data); |
| 62 | break; |
| 63 | } |
| 64 |
| 65 | dst.put(data); |
| 66 |
| 67 | if (!dst.hasRemaining()) { |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 |
| 73 | public void discard(int count) { |
| 74 | int original = count; |
| 75 |
| 76 | while (count > 0) { |
| 77 | ByteBufferDesc data = this.queue.get(0); |
| 78 |
| 79 | if (count < data.buffer.remaining()) { |
| 80 | data.position(data.position() + count); |
| 81 | count = 0; |
| 82 | break; |
| 83 | } |
| 84 |
| 85 | this.queue.remove(0); |
| 86 | BuffersStorage.getInstance().reuseFreeBuffer(data); |
| 87 | count -= data.buffer.remaining(); |
| 88 | } |
| 89 |
| 90 | if (count != 0) { |
| 91 | throw new PyroException("discarded " + (original - count) + "/" + original + " bytes"); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 |