1 package org.apache.lucene.util;
3 import java.io.IOException;
4 import java.util.Arrays;
8 import org.apache.lucene.util.LuceneTestCase;
9 import org.apache.lucene.util.UnsafeByteArrayInputStream;
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed with
14 * this work for additional information regarding copyright ownership.
15 * The ASF licenses this file to You under the Apache License, Version 2.0
16 * (the "License"); you may not use this file except in compliance with
17 * the License. You may obtain a copy of the License at
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
28 public class UnsafeByteArrayInputStreamTest extends LuceneTestCase {
31 public void testSimple() throws IOException {
33 byte[] buffer = new byte[length];
34 for (int i = 0; i < length; ++i) {
37 byte[] result = new byte[buffer.length];
38 UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(buffer);
41 int by = ubais.read();
43 result[index++] = (byte) (by);
47 assertEquals(length, index);
48 assertTrue(Arrays.equals(buffer, result));
52 public void testStartPos() throws IOException {
54 byte[] buffer = new byte[length];
55 for (int i = 0; i < length; ++i) {
59 byte[] result = new byte[buffer.length];
60 UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(buffer, startPos, length);
63 int by = ubais.read();
65 result[index++] = (byte) (by);
69 assertEquals(length - startPos, index);
70 for (int i = startPos; i < length; i++) {
71 assertEquals(buffer[i], result[i - startPos]);
76 public void testReinit() throws IOException {
78 byte[] buffer = new byte[length];
79 for (int i = 0; i < length; ++i) {
82 byte[] result = new byte[buffer.length];
83 UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(buffer);
86 int by = ubais.read();
88 result[index++] = (byte) (by);
92 assertEquals(length, index);
93 assertTrue(Arrays.equals(buffer, result));
96 byte[] buffer2 = new byte[length2];
97 for (int i = 0; i < length2; ++i) {
98 buffer2[i] = (byte) (90 + i);
100 byte[] result2 = new byte[buffer2.length];
101 ubais.reInit(buffer2);
104 int by2 = ubais.read();
106 result2[index2++] = (byte) (by2);
110 assertEquals(length2, index2);
111 assertTrue(Arrays.equals(buffer2, result2));
115 public void testDefaultCtor() throws Exception {
116 UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream();
117 assertEquals(0, ubais.available());
118 assertEquals(-1, ubais.read());
122 public void testMark() throws Exception {
123 byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
124 UnsafeByteArrayInputStream ubais = new UnsafeByteArrayInputStream(bytes);
125 assertTrue(ubais.markSupported());
128 for (int i = 0; i < markIndex; i++) {
131 ubais.mark(markIndex);
132 for (int i = markIndex; i < bytes.length; i++) {
136 assertEquals(bytes.length - markIndex, ubais.available());