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

« back to all changes in this revision

Viewing changes to solr/solrj/src/java/org/apache/solr/common/util/FastOutputStream.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
 
 
18
 
package org.apache.solr.common.util;
19
 
 
20
 
import java.io.*;
21
 
 
22
 
/** Single threaded buffered OutputStream
23
 
 *  Internal Solr use only, subject to change.
24
 
 */
25
 
public class FastOutputStream extends OutputStream implements DataOutput {
26
 
  private final OutputStream out;
27
 
  private final byte[] buf;
28
 
  private long written;  // how many bytes written
29
 
  private int pos;
30
 
 
31
 
  public FastOutputStream(OutputStream w) {
32
 
  // use default BUFSIZE of BufferedOutputStream so if we wrap that
33
 
  // it won't cause double buffering.
34
 
    this(w, new byte[8192], 0);
35
 
  }
36
 
 
37
 
  public FastOutputStream(OutputStream sink, byte[] tempBuffer, int start) {
38
 
    this.out = sink;
39
 
    this.buf = tempBuffer;
40
 
    this.pos = start;
41
 
  }
42
 
 
43
 
 
44
 
  public static FastOutputStream wrap(OutputStream sink) {
45
 
   return (sink instanceof FastOutputStream) ? (FastOutputStream)sink : new FastOutputStream(sink);
46
 
  }
47
 
 
48
 
  @Override
49
 
  public void write(int b) throws IOException {
50
 
    write((byte)b);
51
 
  }
52
 
 
53
 
  @Override
54
 
  public void write(byte b[]) throws IOException {
55
 
    write(b,0,b.length);
56
 
  }
57
 
 
58
 
  public void write(byte b) throws IOException {
59
 
    if (pos >= buf.length) {
60
 
      out.write(buf);
61
 
      written += pos;
62
 
      pos=0;
63
 
    }
64
 
    buf[pos++] = b;
65
 
  }
66
 
 
67
 
  @Override
68
 
  public void write(byte arr[], int off, int len) throws IOException {
69
 
    int space = buf.length - pos;
70
 
    if (len < space) {
71
 
      System.arraycopy(arr, off, buf, pos, len);
72
 
      pos += len;
73
 
    } else if (len<buf.length) {
74
 
      // if the data to write is small enough, buffer it.
75
 
      System.arraycopy(arr, off, buf, pos, space);
76
 
      out.write(buf);
77
 
      written += buf.length;
78
 
      pos = len-space;
79
 
      System.arraycopy(arr, off+space, buf, 0, pos);
80
 
    } else {
81
 
      if (pos>0) {
82
 
        out.write(buf,0,pos);  // flush
83
 
        written += pos;
84
 
        pos=0;
85
 
      }
86
 
      // don't buffer, just write to sink
87
 
      out.write(arr, off, len);
88
 
      written += len;            
89
 
    }
90
 
  }
91
 
 
92
 
  /** reserve at least len bytes at the end of the buffer.
93
 
   * Invalid if len > buffer.length
94
 
   * @param len
95
 
   */
96
 
  public void reserve(int len) throws IOException {
97
 
    if (len > (buf.length - pos))
98
 
      flushBuffer();
99
 
  }
100
 
 
101
 
  ////////////////// DataOutput methods ///////////////////
102
 
  public void writeBoolean(boolean v) throws IOException {
103
 
    write(v ? 1:0);
104
 
  }
105
 
 
106
 
  public void writeByte(int v) throws IOException {
107
 
    write((byte)v);
108
 
  }
109
 
 
110
 
  public void writeShort(int v) throws IOException {
111
 
    write((byte)(v >>> 8));
112
 
    write((byte)v);
113
 
  }
114
 
 
115
 
  public void writeChar(int v) throws IOException {
116
 
    writeShort(v);
117
 
  }
118
 
 
119
 
  public void writeInt(int v) throws IOException {
120
 
    reserve(4);
121
 
    buf[pos] = (byte)(v>>>24);
122
 
    buf[pos+1] = (byte)(v>>>16);
123
 
    buf[pos+2] = (byte)(v>>>8);
124
 
    buf[pos+3] = (byte)(v);
125
 
    pos+=4;
126
 
  }
127
 
 
128
 
  public void writeLong(long v) throws IOException {
129
 
    reserve(8);
130
 
    buf[pos] = (byte)(v>>>56);
131
 
    buf[pos+1] = (byte)(v>>>48);
132
 
    buf[pos+2] = (byte)(v>>>40);
133
 
    buf[pos+3] = (byte)(v>>>32);
134
 
    buf[pos+4] = (byte)(v>>>24);
135
 
    buf[pos+5] = (byte)(v>>>16);
136
 
    buf[pos+6] = (byte)(v>>>8);
137
 
    buf[pos+7] = (byte)(v);
138
 
    pos+=8;
139
 
  }
140
 
 
141
 
  public void writeFloat(float v) throws IOException {
142
 
    writeInt(Float.floatToRawIntBits(v));
143
 
  }
144
 
 
145
 
  public void writeDouble(double v) throws IOException {
146
 
    writeLong(Double.doubleToRawLongBits(v));
147
 
  }
148
 
 
149
 
  public void writeBytes(String s) throws IOException {
150
 
    // non-optimized version, but this shouldn't be used anyway
151
 
    for (int i=0; i<s.length(); i++)
152
 
      write((byte)s.charAt(i));
153
 
  }
154
 
 
155
 
  public void writeChars(String s) throws IOException {
156
 
    // non-optimized version
157
 
    for (int i=0; i<s.length(); i++)
158
 
      writeChar(s.charAt(i)); 
159
 
  }
160
 
 
161
 
  public void writeUTF(String s) throws IOException {
162
 
    // non-optimized version, but this shouldn't be used anyway
163
 
    DataOutputStream daos = new DataOutputStream(this);
164
 
    daos.writeUTF(s);
165
 
  }
166
 
 
167
 
 
168
 
  @Override
169
 
  public void flush() throws IOException {
170
 
    flushBuffer();
171
 
    out.flush();
172
 
  }
173
 
 
174
 
  @Override
175
 
  public void close() throws IOException {
176
 
    flushBuffer();
177
 
    out.close();
178
 
  }
179
 
 
180
 
  /** Only flushes the buffer of the FastOutputStream, not that of the
181
 
   * underlying stream.
182
 
   */
183
 
  public void flushBuffer() throws IOException {
184
 
    out.write(buf, 0, pos);
185
 
    written += pos;
186
 
    pos=0;
187
 
  }
188
 
 
189
 
  public long size() {
190
 
    return written + pos;
191
 
  }
192
 
}