| 1 | /* |
| 2 | * Copyright (c) 2008, https://code.google.com/p/pyronet/ |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 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.traffic; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
| 23 | public abstract class ByteSinkEndsWith implements ByteSink { |
| 24 | private final ByteBuffer result; |
| 25 | |
| 26 | private final byte[] endsWith; |
| 27 | |
| 28 | private final boolean includeEndsWith; |
| 29 | |
| 30 | private int matchCount; |
| 31 | |
| 32 | private int filled; |
| 33 | |
| 34 | public ByteSinkEndsWith(byte[] endsWith, int capacity, |
| 35 | boolean includeEndsWith) { |
| 36 | if (endsWith == null || endsWith.length == 0) |
| 37 | throw new IllegalStateException(); |
| 38 | this.result = ByteBuffer.allocate(capacity); |
| 39 | this.endsWith = endsWith; |
| 40 | this.includeEndsWith = includeEndsWith; |
| 41 | |
| 42 | this.reset(); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void reset() { |
| 47 | this.result.clear(); |
| 48 | this.matchCount = 0; |
| 49 | this.filled = 0; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public int feed(byte b) { |
| 54 | if (this.endsWith[this.matchCount] == b) { |
| 55 | this.matchCount++; |
| 56 | } else { |
| 57 | this.matchCount = 0; |
| 58 | } |
| 59 | |
| 60 | this.result.put(this.filled, b); |
| 61 | |
| 62 | this.filled += 1; |
| 63 | |
| 64 | if (this.matchCount == this.endsWith.length) { |
| 65 | int len = this.filled |
| 66 | - (this.includeEndsWith ? 0 : this.endsWith.length); |
| 67 | this.result.limit(len); |
| 68 | this.onReady(this.result); |
| 69 | return FEED_ACCEPTED_LAST; |
| 70 | } |
| 71 | |
| 72 | return ByteSink.FEED_ACCEPTED; |
| 73 | } |
| 74 | } |
| 75 | |