It works in a way similar to {@link ByteArrayOutputStream} butthis class works better in the following ways:
28293031323334
private Buffer _buffer; @Before public void setUp() { _buffer = new ByteArrayBuffer(1024); }
3031323334353637383940
@Test public void testTcp() throws Exception { StringEndPoint ep = new StringEndPoint(); ep.setInput(_msg); SipParser parser = new SipParser(new ByteArrayBuffer(1024), ep, new Handler()); try { parser.parse(); fail("expected buffer overflow");
5455565758596061626364
msgs.append("INVITE sip:foo SIP/2.0\r\nX-Seq: " + i + "\r\nContent-Length:0\r\n\r\n"); } StringEndPoint ep = new StringEndPoint(); ep.setInput(msgs.toString()); SipParser parser = new SipParser(new ByteArrayBuffer(55889), ep, new Handler()); for (int i = 0; i < 1000; i++) { seq = -1; parser.parse(); assertEquals(i, seq);
7879808182838485868788899091929394
@Test public void testOverflow() throws Exception { StringEndPoint ep = new StringEndPoint(); ep.setInput(_msg); SipParser parser = new SipParser(new ByteArrayBuffer(1), ep, new Handler()); int size = 1; boolean overflow = false; do { parser.setBuffer(new ByteArrayBuffer(size), true); try { parser.parse(); overflow = false; }
1617181920212223242526
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testAVPCodec() throws IOException { AVP avp = new AVP(Cx.PUBLIC_IDENTITY, "sip:alice@cipango.org"); Buffer buffer = new ByteArrayBuffer(64); Codecs.__avp.encode(buffer, avp); AVP decoded = Codecs.__avp.decode(buffer); assertEquals(avp.getType().getCode(), decoded.getType().getCode());
3233343536373839404142434445
@Test public void testPadding() throws IOException { byte[] b = { 13 }; AVP<byte[]> avp = new AVP<byte[]>(Cx.INTEGRITY_KEY, b); Buffer buffer = new ByteArrayBuffer(64); for (int i = 0; i < 64; i++) buffer.put((byte) 44); buffer.setPutIndex(0); Codecs.__avp.encode(buffer, avp); View view = new View(buffer); view.setGetIndex(view.putIndex() - 3); for (int i = 0; i < 3; i++) assertEquals(0, view.get());
164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
public void run() { try { Buffer fb = new ByteArrayBuffer(4); while (isStarted() && !isClosed()) { fb.clear(); int read = fill(fb); if (read == -1) throw new EofException(); int length = (fb.peek(1) & 0xff) << 16 | (fb.peek(2) & 0xff) << 8 | (fb.peek(3) & 0xff); Buffer b = new ByteArrayBuffer(length); int totalRead = 4; b.put(fb); while (totalRead < length) { read = fill(b);
192193194195196197198
return _messageBufferSize; } protected Buffer newBuffer(int size) { return new ByteArrayBuffer(size); }
250251252253254255256257258
while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } EventHandler handler = new EventHandler(); SipParser parser = new SipParser(new ByteArrayBuffer(os.toByteArray()), handler); parser.parse(); return handler.getMessage(); }
474849505152535455565758
public static Buffer ensureSpace(Buffer buffer, int space) { if (buffer.space() < space) { ByteArrayBuffer largerBuffer = new ByteArrayBuffer(buffer.capacity() + 100); largerBuffer.put(buffer); return largerBuffer; } else return buffer; }