1 package org.apache.lucene.util;
3 import java.io.IOException;
7 import org.apache.lucene.util.LuceneTestCase;
8 import org.apache.lucene.util.UnsafeByteArrayOutputStream;
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed with
13 * this work for additional information regarding copyright ownership.
14 * The ASF licenses this file to You under the Apache License, Version 2.0
15 * (the "License"); you may not use this file except in compliance with
16 * the License. You may obtain a copy of the License at
18 * http://www.apache.org/licenses/LICENSE-2.0
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
27 public class UnsafeByteArrayOutputStreamTest extends LuceneTestCase {
30 public void testSimpleWrite() throws IOException {
32 byte[] buffer = new byte[length];
33 UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
35 for (int i = 0; i < 100; i++) {
36 ubaos.write((byte) i);
39 byte[] result = ubaos.toByteArray();
41 assertEquals(length, ubaos.length());
43 for (int j = 0; j < length; ++j) {
44 assertEquals(result[j], j);
49 public void testArrayWrite() throws IOException {
51 byte[] buffer = new byte[length];
52 UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
54 for (int i = 0; i < 100; i++) {
55 ubaos.write((byte) i);
59 byte[] buffer2 = new byte[length2];
60 for (int i = 0; i < length2; i++) {
61 buffer2[i] = (byte) (8 + i);
66 byte[] result = ubaos.toByteArray();
68 assertEquals(length + length2, ubaos.length());
70 for (int j = 0; j < length; ++j) {
71 assertEquals(result[j], j);
73 for (int j = 0; j < length2; ++j) {
74 assertEquals(result[j + length], buffer2[j]);
79 public void testArrayWriteStartNotZero() throws IOException {
81 byte[] buffer = new byte[length];
82 UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
84 for (int i = 0; i < 100; i++) {
85 ubaos.write((byte) i);
89 byte[] buffer2 = new byte[length2];
90 for (int i = 0; i < length2; i++) {
91 buffer2[i] = (byte) (8 + i);
96 ubaos.write(buffer2, start, length3);
98 byte[] result = ubaos.toByteArray();
100 assertEquals(length + length3, ubaos.length());
102 for (int j = 0; j < length; ++j) {
103 assertEquals(result[j], j);
105 for (int j = 0; j < length3; ++j) {
106 assertEquals(result[j + length], buffer2[j + start]);
111 public void testBufferGrow() throws IOException {
113 byte[] buffer = new byte[length / 10];
114 UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buffer);
116 for (int i = 0; i < length; i++) {
117 ubaos.write((byte) i);
120 byte[] result = ubaos.toByteArray();
122 assertEquals(length, ubaos.length());
124 for (int j = 0; j < length; ++j) {
125 assertEquals(result[j], j);
128 buffer = ubaos.toByteArray();
131 byte[] buffer2 = new byte[length2];
132 for (int i = 0; i < length2; i++) {
133 buffer2[i] = (byte) (8 + i);
136 ubaos.reInit(buffer2);
137 for (int i = 0; i < length2; i++) {
141 byte[] result2 = ubaos.toByteArray();
143 assertEquals(length2, ubaos.length());
145 for (int j = 0; j < length2; ++j) {
146 assertEquals(result2[j], j + 7);
149 for (int i = 0; i < length; i++) {
150 assertEquals(buffer[i], i);
155 public void testStartPos() throws Exception {
156 byte[] buf = new byte[10];
157 for (int i = 0; i < buf.length; i++) {
162 UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream(buf, startPos);
164 for (int i = 0; i < numValues; i++) {
165 ubaos.write((i + 1) * 2);
168 // the length of the buffer should be whatever was written after startPos
170 assertEquals("invalid buffer length", startPos + numValues, ubaos.length());
172 assertEquals("invalid startPos", startPos, ubaos.getStartPos());
174 byte[] bytes = ubaos.toByteArray();
175 for (int i = 0; i < startPos; i++) {
176 assertEquals(i, bytes[i]);
179 for (int i = startPos, j = 0; j < numValues; i++, j++) {
180 assertEquals((j + 1) * 2, bytes[i]);
183 for (int i = startPos + numValues; i < buf.length; i++) {
184 assertEquals(i, bytes[i]);
190 public void testDefaultCtor() throws Exception {
191 UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream();
193 for (int i = 0; i < numValues; i++) {
197 assertEquals("invalid buffer length", numValues, ubaos.length());
199 byte[] bytes = ubaos.toByteArray();
200 for (int i = 0; i < numValues; i++) {
201 assertEquals(i, bytes[i]);
205 @Test(expected=IllegalArgumentException.class)
206 public void testIllegalBufferSize() throws Exception {
207 UnsafeByteArrayOutputStream ubaos = new UnsafeByteArrayOutputStream();
208 ubaos.reInit(new byte[0]);