~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/solrj/src/test/org/apache/solr/common/util/TestFastInputStream.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
package org.apache.solr.common.util;
18
 
 
19
 
import org.apache.lucene.util.LuceneTestCase;
20
 
import org.junit.Test;
21
 
 
22
 
import java.io.*;
23
 
import java.util.zip.GZIPInputStream;
24
 
import java.util.zip.GZIPOutputStream;
25
 
 
26
 
/**
27
 
 * Test for FastInputStream.
28
 
 *
29
 
 * @version $Id: TestFastInputStream.java 1052938 2010-12-26 20:21:48Z rmuir $
30
 
 * @see org.apache.solr.common.util.FastInputStream
31
 
 */
32
 
public class TestFastInputStream extends LuceneTestCase {
33
 
  @Test
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());
40
 
    gzos.close();
41
 
    NamedListCodec.writeVInt(10, fos);
42
 
    fos.flushBuffer();
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");
48
 
 
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");
55
 
  }
56
 
 
57
 
  //code copied from NamedListCodec#readChars
58
 
  public static void readChars(InputStream in, char[] buffer, int start, int length)
59
 
          throws IOException {
60
 
    final int end = start + length;
61
 
    for (int i = start; i < end; i++) {
62
 
      int b = in.read();
63
 
      if ((b & 0x80) == 0)
64
 
        buffer[i] = (char) b;
65
 
      else if ((b & 0xE0) != 0xE0) {
66
 
        buffer[i] = (char) (((b & 0x1F) << 6)
67
 
                | (in.read() & 0x3F));
68
 
      } else
69
 
        buffer[i] = (char) (((b & 0x0F) << 12)
70
 
                | ((in.read() & 0x3F) << 6)
71
 
                | (in.read() & 0x3F));
72
 
    }
73
 
  }
74
 
 
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)
81
 
        os.write(code);
82
 
      else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {
83
 
        os.write(0xC0 | (code >> 6));
84
 
        os.write(0x80 | (code & 0x3F));
85
 
      } else {
86
 
        os.write(0xE0 | (code >>> 12));
87
 
        os.write(0x80 | ((code >> 6) & 0x3F));
88
 
        os.write(0x80 | (code & 0x3F));
89
 
      }
90
 
    }
91
 
  }
92
 
}