/** {@inheritDoc} */ @Override public byte[] decode(GridNioSession ses, ByteBuffer buf) throws IOException, IgniteCheckedException { GridNioDelimitedBuffer nioBuf = ses.meta(BUF_META_KEY); // Decode for a given session is called per one thread, so there should not be any concurrency issues. // However, we make some additional checks. if (nioBuf == null) { nioBuf = new GridNioDelimitedBuffer(delim); GridNioDelimitedBuffer old = ses.addMeta(BUF_META_KEY, nioBuf); assert old == null; } return nioBuf.read(buf); }
GridNioDelimitedBuffer delimBuff = new GridNioDelimitedBuffer(delim);
/** * Tests simple delimiter (excluded from alphabet) */ @Test public void testReadZString() throws Exception { Random rnd = new Random(); int buffSize = 0; byte[] delim = new byte[] {0}; List<String> strs = new ArrayList<>(50); for (int i = 0; i < 50; i++) { int len = rnd.nextInt(128) + 1; buffSize += len + delim.length; StringBuilder sb = new StringBuilder(len); for (int j = 0; j < len; j++) sb.append((char)(rnd.nextInt(26) + 'a')); strs.add(sb.toString()); } ByteBuffer buff = ByteBuffer.allocate(buffSize); for (String str : strs) { buff.put(str.getBytes(ASCII)); buff.put(delim); } buff.flip(); byte[] msg; GridNioDelimitedBuffer delimBuff = new GridNioDelimitedBuffer(delim); List<String> res = new ArrayList<>(strs.size()); while ((msg = delimBuff.read(buff)) != null) res.add(new String(msg, ASCII)); assertEquals(strs, res); }
/** {@inheritDoc} */ @Override public byte[] decode(GridNioSession ses, ByteBuffer buf) throws IOException, IgniteCheckedException { GridNioDelimitedBuffer nioBuf = ses.meta(BUF_META_KEY); // Decode for a given session is called per one thread, so there should not be any concurrency issues. // However, we make some additional checks. if (nioBuf == null) { nioBuf = new GridNioDelimitedBuffer(delim); GridNioDelimitedBuffer old = ses.addMeta(BUF_META_KEY, nioBuf); assert old == null; } return nioBuf.read(buf); }