2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.solr.common.util;
19
import org.apache.lucene.util.LuceneTestCase;
20
import org.junit.Test;
23
import java.util.zip.GZIPInputStream;
24
import java.util.zip.GZIPOutputStream;
27
* Test for FastInputStream.
29
* @version $Id: TestFastInputStream.java 1052938 2010-12-26 20:21:48Z rmuir $
30
* @see org.apache.solr.common.util.FastInputStream
32
public class TestFastInputStream extends LuceneTestCase {
34
public void testgzip() throws Exception {
35
ByteArrayOutputStream b = new ByteArrayOutputStream();
36
FastOutputStream fos = new FastOutputStream(b);
37
GZIPOutputStream gzos = new GZIPOutputStream(fos);
38
String ss = "Helloooooooooooooooooooo";
39
writeChars(gzos, ss, 0, ss.length());
41
NamedListCodec.writeVInt(10, fos);
43
GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(b.toByteArray(), 0, b.size()));
44
char[] cbuf = new char[ss.length()];
45
readChars(gzis, cbuf, 0, ss.length());
46
assertEquals(new String(cbuf), ss);
47
// System.out.println("passes w/o FastInputStream");
49
ByteArrayInputStream bis = new ByteArrayInputStream(b.toByteArray(), 0, b.size());
50
gzis = new GZIPInputStream(new FastInputStream(bis));
51
cbuf = new char[ss.length()];
52
readChars(gzis, cbuf, 0, ss.length());
53
assertEquals(new String(cbuf), ss);
54
// System.out.println("passes w FastInputStream");
57
//code copied from NamedListCodec#readChars
58
public static void readChars(InputStream in, char[] buffer, int start, int length)
60
final int end = start + length;
61
for (int i = start; i < end; i++) {
65
else if ((b & 0xE0) != 0xE0) {
66
buffer[i] = (char) (((b & 0x1F) << 6)
67
| (in.read() & 0x3F));
69
buffer[i] = (char) (((b & 0x0F) << 12)
70
| ((in.read() & 0x3F) << 6)
71
| (in.read() & 0x3F));
75
// code copied rfrom NamedlistCode#writechars
76
public static void writeChars(OutputStream os, String s, int start, int length) throws IOException {
77
final int end = start + length;
78
for (int i = start; i < end; i++) {
79
final int code = (int) s.charAt(i);
80
if (code >= 0x01 && code <= 0x7F)
82
else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {
83
os.write(0xC0 | (code >> 6));
84
os.write(0x80 | (code & 0x3F));
86
os.write(0xE0 | (code >>> 12));
87
os.write(0x80 | ((code >> 6) & 0x3F));
88
os.write(0x80 | (code & 0x3F));