1 package org.apache.lucene.store;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.util.LuceneTestCase;
23 import org.apache.lucene.util._TestUtil;
25 import org.junit.Test;
27 public class TestCopyBytes extends LuceneTestCase {
29 private byte value(int idx) {
30 return (byte) ((idx%256) * (1+(idx/256)));
35 public void testCopyBytes() throws Exception {
36 int num = atLeast(10);
37 for(int iter=0;iter<num;iter++) {
38 Directory dir = newDirectory();
40 System.out.println("TEST: iter=" + iter + " dir=" + dir);
44 IndexOutput out = dir.createOutput("test");
45 byte[] bytes = new byte[_TestUtil.nextInt(random, 1, 77777)];
46 final int size = _TestUtil.nextInt(random, 1, 1777777);
50 bytes[byteUpto++] = value(upto);
52 if (byteUpto == bytes.length) {
53 out.writeBytes(bytes, 0, bytes.length);
58 out.writeBytes(bytes, 0, byteUpto);
59 assertEquals(size, out.getFilePointer());
61 assertEquals(size, dir.fileLength("test"));
63 // copy from test -> test2
64 final IndexInput in = dir.openInput("test");
66 out = dir.createOutput("test2");
70 if (random.nextBoolean()) {
71 out.writeByte(in.readByte());
74 final int chunk = Math.min(_TestUtil.nextInt(random, 1, bytes.length), size-upto);
75 out.copyBytes(in, chunk);
79 assertEquals(size, upto);
84 IndexInput in2 = dir.openInput("test2");
87 if (random.nextBoolean()) {
88 final byte v = in2.readByte();
89 assertEquals(value(upto), v);
92 final int limit = Math.min(_TestUtil.nextInt(random, 1, bytes.length), size-upto);
93 in2.readBytes(bytes, 0, limit);
94 for(int byteIdx=0;byteIdx<limit;byteIdx++) {
95 assertEquals(value(upto), bytes[byteIdx]);
102 dir.deleteFile("test");
103 dir.deleteFile("test2");
110 public void testCopyBytesWithThreads() throws Exception {
111 int datalen = _TestUtil.nextInt(random, 101, 10000);
112 byte data[] = new byte[datalen];
113 random.nextBytes(data);
115 Directory d = newDirectory();
116 IndexOutput output = d.createOutput("data");
117 output.writeBytes(data, 0, datalen);
120 IndexInput input = d.openInput("data");
121 IndexOutput outputHeader = d.createOutput("header");
122 // copy our 100-byte header
123 input.copyBytes(outputHeader, 100);
124 outputHeader.close();
126 // now make N copies of the remaining bytes
127 CopyThread copies[] = new CopyThread[10];
128 for (int i = 0; i < copies.length; i++) {
129 copies[i] = new CopyThread((IndexInput) input.clone(), d.createOutput("copy" + i));
132 for (int i = 0; i < copies.length; i++) {
136 for (int i = 0; i < copies.length; i++) {
140 for (int i = 0; i < copies.length; i++) {
141 IndexInput copiedData = d.openInput("copy" + i);
142 byte[] dataCopy = new byte[datalen];
143 System.arraycopy(data, 0, dataCopy, 0, 100); // copy the header for easy testing
144 copiedData.readBytes(dataCopy, 100, datalen-100);
145 assertArrayEquals(data, dataCopy);
153 static class CopyThread extends Thread {
154 final IndexInput src;
155 final IndexOutput dst;
157 CopyThread(IndexInput src, IndexOutput dst) {
165 src.copyBytes(dst, src.length()-100);
167 } catch (IOException ex) {
168 throw new RuntimeException(ex);