org.elasticsearch.common.io.stream.StreamOutput Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.io.stream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author kimchy (shay.banon)
*/
public abstract class StreamOutput extends OutputStream {
/**
* bytearr is initialized on demand by writeUTF
*/
private byte[] bytearr = null;
/**
* Writes a single byte.
*/
public abstract void writeByte(byte b) throws IOException;
/**
* Writes an array of bytes.
*
* @param b the bytes to write
*/
public void writeBytes(byte[] b) throws IOException {
writeBytes(b, 0, b.length);
}
/**
* Writes an array of bytes.
*
* @param b the bytes to write
* @param length the number of bytes to write
*/
public void writeBytes(byte[] b, int length) throws IOException {
writeBytes(b, 0, length);
}
/**
* Writes an array of bytes.
*
* @param b the bytes to write
* @param offset the offset in the byte array
* @param length the number of bytes to write
*/
public abstract void writeBytes(byte[] b, int offset, int length) throws IOException;
public final void writeShort(short v) throws IOException {
writeByte((byte) (v >> 8));
writeByte((byte) v);
}
/**
* Writes an int as four bytes.
*/
public void writeInt(int i) throws IOException {
writeByte((byte) (i >> 24));
writeByte((byte) (i >> 16));
writeByte((byte) (i >> 8));
writeByte((byte) i);
}
/**
* Writes an int in a variable-length format. Writes between one and
* five bytes. Smaller values take fewer bytes. Negative numbers are not
* supported.
*/
public void writeVInt(int i) throws IOException {
while ((i & ~0x7F) != 0) {
writeByte((byte) ((i & 0x7f) | 0x80));
i >>>= 7;
}
writeByte((byte) i);
}
/**
* Writes a long as eight bytes.
*/
public void writeLong(long i) throws IOException {
writeInt((int) (i >> 32));
writeInt((int) i);
}
/**
* Writes an long in a variable-length format. Writes between one and five
* bytes. Smaller values take fewer bytes. Negative numbers are not
* supported.
*/
public void writeVLong(long i) throws IOException {
while ((i & ~0x7F) != 0) {
writeByte((byte) ((i & 0x7f) | 0x80));
i >>>= 7;
}
writeByte((byte) i);
}
/**
* Writes a string.
*/
public void writeUTF(String str) throws IOException {
int strlen = str.length();
int utflen = 0;
int c, count = 0;
/* use charAt instead of copying String to char array */
for (int i = 0; i < strlen; i++) {
c = str.charAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
utflen++;
} else if (c > 0x07FF) {
utflen += 3;
} else {
utflen += 2;
}
}
if (this.bytearr == null || (this.bytearr.length < (utflen + 4)))
this.bytearr = new byte[(utflen * 2) + 4];
byte[] bytearr = this.bytearr;
// same as writeInt
bytearr[count++] = (byte) (utflen >> 24);
bytearr[count++] = (byte) (utflen >> 16);
bytearr[count++] = (byte) (utflen >> 8);
bytearr[count++] = (byte) (utflen);
int i = 0;
for (i = 0; i < strlen; i++) {
c = str.charAt(i);
if (!((c >= 0x0001) && (c <= 0x007F))) break;
bytearr[count++] = (byte) c;
}
for (; i < strlen; i++) {
c = str.charAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
bytearr[count++] = (byte) c;
} else if (c > 0x07FF) {
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
} else {
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
writeBytes(bytearr, 0, utflen + 4);
// return utflen + 2;
}
public void writeFloat(float v) throws IOException {
writeInt(Float.floatToIntBits(v));
}
public void writeDouble(double v) throws IOException {
writeLong(Double.doubleToLongBits(v));
}
private static byte ZERO = 0;
private static byte ONE = 1;
/**
* Writes a boolean.
*/
public void writeBoolean(boolean b) throws IOException {
writeByte(b ? ONE : ZERO);
}
/**
* Forces any buffered output to be written.
*/
public abstract void flush() throws IOException;
/**
* Closes this stream to further operations.
*/
public abstract void close() throws IOException;
public abstract void reset() throws IOException;
@Override public void write(int b) throws IOException {
writeByte((byte) b);
}
@Override public void write(byte[] b, int off, int len) throws IOException {
writeBytes(b, off, len);
}
}