Updated Branches: refs/heads/trunk 6b54593af -> 21548aa8d
GIRAPH-724: UnsafeByteArrayOutputStream.writeUTF should call ensureSize (yhdong via majakabiljo) Project: http://git-wip-us.apache.org/repos/asf/giraph/repo Commit: http://git-wip-us.apache.org/repos/asf/giraph/commit/21548aa8 Tree: http://git-wip-us.apache.org/repos/asf/giraph/tree/21548aa8 Diff: http://git-wip-us.apache.org/repos/asf/giraph/diff/21548aa8 Branch: refs/heads/trunk Commit: 21548aa8d8a5ed1c3414dd1743432fe207d80df4 Parents: 6b54593 Author: Maja Kabiljo <[email protected]> Authored: Wed Jan 15 14:41:32 2014 -0800 Committer: Maja Kabiljo <[email protected]> Committed: Wed Jan 15 14:41:32 2014 -0800 ---------------------------------------------------------------------- CHANGELOG | 2 + .../utils/UnsafeByteArrayOutputStream.java | 12 +-- .../utils/TestUnsafeByteArrayOutputStream.java | 90 ++++++++++++++++++++ 3 files changed, 98 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/giraph/blob/21548aa8/CHANGELOG ---------------------------------------------------------------------- diff --git a/CHANGELOG b/CHANGELOG index a14c80b..7b11341 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ Giraph Change Log Release 1.1.0 - unreleased + GIRAPH-724: UnsafeByteArrayOutputStream.writeUTF should call ensureSize (yhdong via majakabiljo) + GIRAPH-821: proper handling of NegativeArraySizeException for all ByteArray backed messagestores (pavanka via majakabiljo) http://git-wip-us.apache.org/repos/asf/giraph/blob/21548aa8/giraph-core/src/main/java/org/apache/giraph/utils/UnsafeByteArrayOutputStream.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/utils/UnsafeByteArrayOutputStream.java b/giraph-core/src/main/java/org/apache/giraph/utils/UnsafeByteArrayOutputStream.java index 9ff1242..4b413da 100644 --- a/giraph-core/src/main/java/org/apache/giraph/utils/UnsafeByteArrayOutputStream.java +++ b/giraph-core/src/main/java/org/apache/giraph/utils/UnsafeByteArrayOutputStream.java @@ -256,7 +256,8 @@ public class UnsafeByteArrayOutputStream extends OutputStream int len = s.length(); ensureSize(len); for (int i = 0; i < len; i++) { - buf[pos++] = (byte) s.charAt(i); + int v = s.charAt(i); + writeByte(v); } } @@ -264,11 +265,10 @@ public class UnsafeByteArrayOutputStream extends OutputStream public void writeChars(String s) throws IOException { // Note that this code is mostly copied from DataOutputStream int len = s.length(); - ensureSize(len); + ensureSize(len * SIZE_OF_CHAR); for (int i = 0; i < len; i++) { int v = s.charAt(i); - buf[pos++] = (byte) ((v >>> 8) & 0xFF); - buf[pos++] = (byte) ((v >>> 0) & 0xFF); + writeChar(v); } } @@ -291,8 +291,8 @@ public class UnsafeByteArrayOutputStream extends OutputStream } } - buf[pos++] = (byte) ((utflen >>> 8) & 0xFF); - buf[pos++] = (byte) ((utflen >>> 0) & 0xFF); + ensureSize(utflen + SIZE_OF_SHORT); + writeShort(utflen); int i = 0; for (i = 0; i < strlen; i++) { http://git-wip-us.apache.org/repos/asf/giraph/blob/21548aa8/giraph-core/src/test/java/org/apache/giraph/utils/TestUnsafeByteArrayOutputStream.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/utils/TestUnsafeByteArrayOutputStream.java b/giraph-core/src/test/java/org/apache/giraph/utils/TestUnsafeByteArrayOutputStream.java new file mode 100644 index 0000000..2017f1f --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/utils/TestUnsafeByteArrayOutputStream.java @@ -0,0 +1,90 @@ +/* + * 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.giraph.utils; + +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class TestUnsafeByteArrayOutputStream { + @Test + public void testWriteBytes() throws IOException { + UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); + int length = os.getByteArray().length; + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) { + sb.append("\u00ea"); + } + + String s = sb.toString(); + os.writeBytes(s); + + UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray()); + + for (int i = 0; i < s.length(); i++) { + assertEquals((byte) s.charAt(i), is.readByte()); + } + + os.close(); + } + + @Test + public void testWriteChars() throws IOException { + UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); + int length = os.getByteArray().length; + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) { + sb.append("\u10ea"); + } + + String s = sb.toString(); + os.writeChars(s); + + UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray()); + + for (int i = 0; i < s.length(); i++) { + assertEquals(s.charAt(i), is.readChar()); + } + + os.close(); + } + + @Test + public void testWriteUTF() throws IOException { + UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(); + int length = os.getByteArray().length; + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) { + sb.append("\u06ea"); + } + + String s = sb.toString(); + os.writeUTF(s); + + UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray()); + + assertEquals(s, is.readUTF()); + + os.close(); + } +}
