Repository: incubator-ignite Updated Branches: refs/heads/ignite-430-1 5f4a5ccae -> bd91e3261
ignite-430 Implement IgniteSocketStreamer to stream data from TCP socket. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/bd91e326 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/bd91e326 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/bd91e326 Branch: refs/heads/ignite-430-1 Commit: bd91e3261289abc44a687eb23fd3f6b320a8ea31 Parents: 5f4a5cc Author: agura <ag...@gridgain.com> Authored: Tue Apr 28 01:16:11 2015 +0300 Committer: agura <ag...@gridgain.com> Committed: Tue Apr 28 01:16:11 2015 +0300 ---------------------------------------------------------------------- .../util/nio/GridNioDelimitedBuffer.java | 22 +++- .../util/nio/GridNioDelimitedBufferTest.java | 112 +++++++++++++++++++ .../testsuites/IgniteUtilSelfTestSuite.java | 1 + 3 files changed, 132 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd91e326/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBuffer.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBuffer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBuffer.java index 230297d..2b764ec 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBuffer.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBuffer.java @@ -30,7 +30,7 @@ public class GridNioDelimitedBuffer { private final byte[] delim; /** Data. */ - private byte[] data = new byte[512]; + private byte[] data = new byte[16384]; /** Count. */ private int cnt; @@ -73,8 +73,24 @@ public class GridNioDelimitedBuffer { if (b == delim[idx]) idx++; - else - idx = (b == delim[0]) ? 1 : 0; + else if (idx > 0) { + int pos = cnt - idx; + + idx = 0; + + for (int i = pos; i < cnt; i++) { + if (data[pos] == delim[idx]) { + pos++; + + idx++; + } + else { + pos = cnt - idx; + + idx = 0; + } + } + } if (idx == delim.length) { byte[] bytes = Arrays.copyOfRange(data, 0, cnt - delim.length); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd91e326/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBufferTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBufferTest.java b/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBufferTest.java new file mode 100644 index 0000000..a0dd2e5 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioDelimitedBufferTest.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.util.nio; + +import junit.framework.TestCase; + +import java.nio.*; +import java.util.*; + +/** + * Tests for {@link GridNioDelimitedBuffer}. + */ +public class GridNioDelimitedBufferTest extends TestCase { + /** */ + private static final String ASCII = "ASCII"; + + /** + * Tests simple delimiter (excluded from alphabet) + */ + 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); + } + + /** + * Tests compound delimiter (included to alphabet) + */ + public void testDelim() throws Exception { + byte[] delim = "aabb".getBytes(ASCII); + + List<String> strs = Arrays.asList("za", "zaa", "zaab", "zab", "zaabaababbbbabaab"); + + int buffSize = 0; + + for (String str : strs) + buffSize += str.length() + delim.length; + + 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); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bd91e326/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java index 941b06e..32cd038 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java @@ -67,6 +67,7 @@ public class IgniteUtilSelfTestSuite extends TestSuite { suite.addTestSuite(GridNioSelfTest.class); suite.addTestSuite(GridNioFilterChainSelfTest.class); suite.addTestSuite(GridNioSslSelfTest.class); + suite.addTestSuite(GridNioDelimitedBufferTest.class); return suite; }