@Test public void testDecode() throws ProtocolException { String c1 = "123\n"; String c2 = "123\n456\n"; String c3 = "123\n456"; String c4 = "\n123\n"; String c5 = "123456789012345678901\n"; String c6 = "测试\nhello\n"; String c7 = "1234567"; List<String> l = null; l = decoder.decode(c1.getBytes(UTF_8)); Assert.assertEquals("123", l.get(0)); l = decoder.decode(c2.getBytes(UTF_8)); Assert.assertEquals("123", l.get(0)); Assert.assertEquals("456", l.get(1)); l = decoder.decode(c3.getBytes(UTF_8)); Assert.assertEquals("123", l.get(0)); l = decoder.decode("\n".getBytes(UTF_8)); Assert.assertEquals("456", l.get(0)); l = decoder.decode(c4.getBytes(UTF_8)); Assert.assertEquals("", l.get(0)); Assert.assertEquals("123", l.get(1)); try { l = decoder.decode(c5.getBytes(UTF_8)); Assert.fail(); } catch(Exception e) { Assert.assertTrue(true); } l = decoder.decode(c6.getBytes(UTF_8)); Assert.assertEquals("测试", l.get(0)); Assert.assertEquals("hello", l.get(1)); l = decoder.decode(c7.getBytes(UTF_8)); Assert.assertEquals(0, l.size()); l = decoder.decode(c7.getBytes(UTF_8)); Assert.assertEquals(0, l.size()); try { l = decoder.decode(c7.getBytes(UTF_8)); Assert.fail(); } catch(Exception e) { Assert.assertTrue(true); } decoder.decode(c7.getBytes(UTF_8)); decoder.decode(c7.getBytes(UTF_8)); l = decoder.decode("\n".getBytes(UTF_8)); Assert.assertEquals(c7 + c7, l.get(0)); System.out.println(String.format("[CRAFT-ATOM-PROTOCOL-TEXTLINE] (^_^) <%s> Case -> test decode. ", CaseCounter.incr(16))); }
@Override public List<String> decode(byte[] bytes) throws ProtocolException { List<String> strs = new ArrayList<String>(); adapt(); buf.append(bytes); while (searchIndex < buf.length()) { int idx = buf.indexOf(delimiterBytes, searchIndex); if (idx < 0) { if (buf.length() > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = buf.length(); break; } byte[] lineBytes = buf.array(splitIndex, idx); if (lineBytes.length > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = splitIndex = idx + delimiterLen; strs.add(new String(lineBytes, charset)); } return strs; }
TextLineDecoder extends AbstractProtocolDecoder implements ProtocolDecoder<String> { @Override public List<String> decode(byte[] bytes) throws ProtocolException { List<String> strs = new ArrayList<String>(); adapt(); buf.append(bytes); while (searchIndex < buf.length()) { int idx = buf.indexOf(delimiterBytes, searchIndex); if (idx < 0) { if (buf.length() > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = buf.length(); break; } byte[] lineBytes = buf.array(splitIndex, idx); if (lineBytes.length > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = splitIndex = idx + delimiterLen; strs.add(new String(lineBytes, charset)); } return strs; } }
TextLineDecoder extends AbstractProtocolDecoder implements ProtocolDecoder<String> { @Override public List<String> decode(byte[] bytes) throws ProtocolException { List<String> strs = new ArrayList<String>(); adapt(); buf.append(bytes); while (searchIndex < buf.length()) { int idx = buf.indexOf(delimiterBytes, searchIndex); if (idx < 0) { if (buf.length() > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = buf.length(); break; } byte[] lineBytes = buf.array(splitIndex, idx); if (lineBytes.length > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = splitIndex = idx + delimiterLen; strs.add(new String(lineBytes, charset)); } return strs; } TextLineDecoder();  TextLineDecoder(Charset charset);  TextLineDecoder(Charset charset, String delimiter);  TextLineDecoder(Charset charset, String delimiter, int defaultBufferSize);  TextLineDecoder(Charset charset, String delimiter, int defaultBufferSize, int maxSize); }
TextLineDecoder extends AbstractProtocolDecoder implements ProtocolDecoder<String> { @Override public List<String> decode(byte[] bytes) throws ProtocolException { List<String> strs = new ArrayList<String>(); adapt(); buf.append(bytes); while (searchIndex < buf.length()) { int idx = buf.indexOf(delimiterBytes, searchIndex); if (idx < 0) { if (buf.length() > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = buf.length(); break; } byte[] lineBytes = buf.array(splitIndex, idx); if (lineBytes.length > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = splitIndex = idx + delimiterLen; strs.add(new String(lineBytes, charset)); } return strs; } TextLineDecoder();  TextLineDecoder(Charset charset);  TextLineDecoder(Charset charset, String delimiter);  TextLineDecoder(Charset charset, String delimiter, int defaultBufferSize);  TextLineDecoder(Charset charset, String delimiter, int defaultBufferSize, int maxSize); @Override List<String> decode(byte[] bytes); void setDelimiter(String delimiter); }
TextLineDecoder extends AbstractProtocolDecoder implements ProtocolDecoder<String> { @Override public List<String> decode(byte[] bytes) throws ProtocolException { List<String> strs = new ArrayList<String>(); adapt(); buf.append(bytes); while (searchIndex < buf.length()) { int idx = buf.indexOf(delimiterBytes, searchIndex); if (idx < 0) { if (buf.length() > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = buf.length(); break; } byte[] lineBytes = buf.array(splitIndex, idx); if (lineBytes.length > maxSize) { buf.reset(defaultBufferSize); throw new ProtocolException(ProtocolExceptionType.LINE_LENGTH_LIMIT, maxSize); } searchIndex = splitIndex = idx + delimiterLen; strs.add(new String(lineBytes, charset)); } return strs; } TextLineDecoder();  TextLineDecoder(Charset charset);  TextLineDecoder(Charset charset, String delimiter);  TextLineDecoder(Charset charset, String delimiter, int defaultBufferSize);  TextLineDecoder(Charset charset, String delimiter, int defaultBufferSize, int maxSize); @Override List<String> decode(byte[] bytes); void setDelimiter(String delimiter);  }