1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.utils;
1920import org.junit.Assert;
21import org.junit.Test;
2223import java.io.IOException;
24import java.io.UTFDataFormatException;
2526importstatic org.junit.Assert.assertEquals;
2728publicclassTestUnsafeByteArrayOutputStream {
29 @Test
30publicvoid testWriteBytes() throws IOException {
31 UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
32int length = os.getByteArray().length;
3334 StringBuilder sb = new StringBuilder();
35for (int i = 0; i < length; i++) {
36 sb.append("\u00ea");
37 }
3839 String s = sb.toString();
40 os.writeBytes(s);
4142 UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
4344for (int i = 0; i < s.length(); i++) {
45 assertEquals((byte) s.charAt(i), is.readByte());
46 }
4748 os.close();
49 }
5051 @Test
52publicvoid testWriteChars() throws IOException {
53 UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
54int length = os.getByteArray().length;
5556 StringBuilder sb = new StringBuilder();
57for (int i = 0; i < length; i++) {
58 sb.append("\u10ea");
59 }
6061 String s = sb.toString();
62 os.writeChars(s);
6364 UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
6566for (int i = 0; i < s.length(); i++) {
67 assertEquals(s.charAt(i), is.readChar());
68 }
6970 os.close();
71 }
7273 @Test
74publicvoid testWriteUTF() throws IOException {
75 StringBuilder sb = new StringBuilder();
76for (int i = 0; i < 20; i++) {
77 sb.append("\u06ea");
78 }
7980 String s = sb.toString();
8182 assertEquals(s, writeAndReadUTF(s));
83 }
8485 @Test
86publicvoid testWriteLongUTF() throws IOException {
87int maxLength = 65535;
88 StringBuilder sb = new StringBuilder();
89for (int i = 0; i < maxLength; i++) {
90 sb.append("a");
91 }
9293 String s = sb.toString();
9495 assertEquals(s, writeAndReadUTF(s));
9697 s = sb.append("a").toString();
98try {
99 writeAndReadUTF(s);
100thrownew IllegalStateException("Exception expected");
101 } catch (Exception e) {
102 Assert.assertTrue(e instanceof UTFDataFormatException);
103 }
104 }
105106private String writeAndReadUTF(String s) throws IOException {
107 UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
108 os.writeUTF(s);
109 os.close();
110 UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
111return is.readUTF();
112 }
113 }