public abstract class DataOutput extends Object
DataOutput
may only be used from one thread, because it is not
thread safe (it keeps internal state like file position).
Constructor and Description |
---|
DataOutput() |
Modifier and Type | Method and Description |
---|---|
void |
copyBytes(DataInput input,
long numBytes)
Copy numBytes bytes from input to ourself.
|
abstract void |
writeByte(byte b)
Writes a single byte.
|
void |
writeBytes(byte[] b,
int length)
Writes an array of bytes.
|
abstract void |
writeBytes(byte[] b,
int offset,
int length)
Writes an array of bytes.
|
void |
writeInt(int i)
Writes an int as four bytes.
|
void |
writeLong(long i)
Writes a long as eight bytes.
|
void |
writeShort(short i)
Writes a short as two bytes.
|
void |
writeString(String s)
Writes a string.
|
void |
writeStringSet(Set<String> set)
Writes a String set.
|
void |
writeStringStringMap(Map<String,String> map)
Writes a String map.
|
void |
writeVInt(int i)
Writes an int in a variable-length format.
|
void |
writeVLong(long i)
Writes an long in a variable-length format.
|
void |
writeZInt(int i)
Write a
zig-zag -encoded
variable-length integer. |
void |
writeZLong(long i)
Write a
zig-zag -encoded
variable-length long. |
public abstract void writeByte(byte b) throws IOException
The most primitive data type is an eight-bit byte. Files are accessed as sequences of bytes. All other data types are defined as sequences of bytes, so file formats are byte-order independent.
IOException
DataInput.readByte()
public void writeBytes(byte[] b, int length) throws IOException
b
- the bytes to writelength
- the number of bytes to writeIOException
DataInput.readBytes(byte[],int,int)
public abstract void writeBytes(byte[] b, int offset, int length) throws IOException
b
- the bytes to writeoffset
- the offset in the byte arraylength
- the number of bytes to writeIOException
DataInput.readBytes(byte[],int,int)
public void writeInt(int i) throws IOException
32-bit unsigned integer written as four bytes, high-order bytes first.
IOException
DataInput.readInt()
public void writeShort(short i) throws IOException
IOException
DataInput.readShort()
public final void writeVInt(int i) throws IOException
VByte is a variable-length format for positive integers is defined where the high-order bit of each byte indicates whether more bytes remain to be read. The low-order seven bits are appended as increasingly more significant bits in the resulting integer value. Thus values from zero to 127 may be stored in a single byte, values from 128 to 16,383 may be stored in two bytes, and so on.
VByte Encoding Example
Value | Byte 1 | Byte 2 | Byte 3 |
---|---|---|---|
0 | 00000000 | ||
1 | 00000001 | ||
2 | 00000010 | ||
... | |||
127 | 01111111 | ||
128 | 10000000 | 00000001 | |
129 | 10000001 | 00000001 | |
130 | 10000010 | 00000001 | |
... | |||
16,383 | 11111111 | 01111111 | |
16,384 | 10000000 | 10000000 | 00000001 |
16,385 | 10000001 | 10000000 | 00000001 |
... |
This provides compression while still being efficient to decode.
i
- Smaller values take fewer bytes. Negative numbers are
supported, but should be avoided.IOException
- If there is an I/O error writing to the underlying medium.DataInput.readVInt()
public final void writeZInt(int i) throws IOException
zig-zag
-encoded
variable-length
integer. This is typically useful
to write small signed ints and is equivalent to calling
writeVInt(BitUtil.zigZagEncode(i))
.IOException
DataInput.readZInt()
public void writeLong(long i) throws IOException
64-bit unsigned integer written as eight bytes, high-order bytes first.
IOException
DataInput.readLong()
public final void writeVLong(long i) throws IOException
The format is described further in writeVInt(int)
.
IOException
DataInput.readVLong()
public final void writeZLong(long i) throws IOException
zig-zag
-encoded
variable-length
long. Writes between one and ten
bytes. This is typically useful to write small signed ints.IOException
DataInput.readZLong()
public void writeString(String s) throws IOException
Writes strings as UTF-8 encoded bytes. First the length, in bytes, is
written as a VInt
, followed by the bytes.
IOException
DataInput.readString()
public void copyBytes(DataInput input, long numBytes) throws IOException
IOException
public void writeStringStringMap(Map<String,String> map) throws IOException
First the size is written as an Int32
,
followed by each key-value pair written as two consecutive
String
s.
map
- Input map. May be null (equivalent to an empty map)IOException
public void writeStringSet(Set<String> set) throws IOException
First the size is written as an Int32
,
followed by each value written as a
String
.
set
- Input set. May be null (equivalent to an empty set)IOException
Copyright © 2000-2014 Apache Software Foundation. All Rights Reserved.