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.
18
package org.apache.solr.common.util;
22
/** Single threaded buffered OutputStream
23
* Internal Solr use only, subject to change.
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
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);
37
public FastOutputStream(OutputStream sink, byte[] tempBuffer, int start) {
39
this.buf = tempBuffer;
44
public static FastOutputStream wrap(OutputStream sink) {
45
return (sink instanceof FastOutputStream) ? (FastOutputStream)sink : new FastOutputStream(sink);
49
public void write(int b) throws IOException {
54
public void write(byte b[]) throws IOException {
58
public void write(byte b) throws IOException {
59
if (pos >= buf.length) {
68
public void write(byte arr[], int off, int len) throws IOException {
69
int space = buf.length - pos;
71
System.arraycopy(arr, off, buf, 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);
77
written += buf.length;
79
System.arraycopy(arr, off+space, buf, 0, pos);
82
out.write(buf,0,pos); // flush
86
// don't buffer, just write to sink
87
out.write(arr, off, len);
92
/** reserve at least len bytes at the end of the buffer.
93
* Invalid if len > buffer.length
96
public void reserve(int len) throws IOException {
97
if (len > (buf.length - pos))
101
////////////////// DataOutput methods ///////////////////
102
public void writeBoolean(boolean v) throws IOException {
106
public void writeByte(int v) throws IOException {
110
public void writeShort(int v) throws IOException {
111
write((byte)(v >>> 8));
115
public void writeChar(int v) throws IOException {
119
public void writeInt(int v) throws IOException {
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);
128
public void writeLong(long v) throws IOException {
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);
141
public void writeFloat(float v) throws IOException {
142
writeInt(Float.floatToRawIntBits(v));
145
public void writeDouble(double v) throws IOException {
146
writeLong(Double.doubleToRawLongBits(v));
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));
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));
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);
169
public void flush() throws IOException {
175
public void close() throws IOException {
180
/** Only flushes the buffer of the FastOutputStream, not that of the
183
public void flushBuffer() throws IOException {
184
out.write(buf, 0, pos);
190
return written + pos;