FileDocCategorySizeDatePackage
OutputStream.javaAPI DocApache Lucene 1.4.35049Tue Mar 30 00:48:06 BST 2004org.apache.lucene.store

OutputStream

public abstract class OutputStream extends Object
Abstract class for output to a file in a Directory. A random-access output stream. Used for all Lucene index output operations.
see
Directory
see
InputStream

Fields Summary
static final int
BUFFER_SIZE
private final byte[]
buffer
private long
bufferStart
private int
bufferPosition
Constructors Summary
Methods Summary
public voidclose()
Closes this stream to further operations.

    flush();
  
protected final voidflush()
Forces any buffered output to be written.

    flushBuffer(buffer, bufferPosition);
    bufferStart += bufferPosition;
    bufferPosition = 0;
  
protected abstract voidflushBuffer(byte[] b, int len)
Expert: implements buffer write. Writes bytes at the current position in the output.

param
b the bytes to write
param
len the number of bytes to write

public final longgetFilePointer()
Returns the current position in this file, where the next write will occur.

see
#seek(long)

    return bufferStart + bufferPosition;
  
public abstract longlength()
The number of bytes in the file.

public voidseek(long pos)
Sets current position in this file, where the next write will occur.

see
#getFilePointer()

    flush();
    bufferStart = pos;
  
public final voidwriteByte(byte b)
Writes a single byte.

see
InputStream#readByte()

		  // position in buffer

           
         
    if (bufferPosition >= BUFFER_SIZE)
      flush();
    buffer[bufferPosition++] = b;
  
public final voidwriteBytes(byte[] b, int length)
Writes an array of bytes.

param
b the bytes to write
param
length the number of bytes to write
see
InputStream#readBytes(byte[],int,int)

    for (int i = 0; i < length; i++)
      writeByte(b[i]);
  
public final voidwriteChars(java.lang.String s, int start, int length)
Writes a sequence of UTF-8 encoded characters from a string.

param
s the source of the characters
param
start the first character in the sequence
param
length the number of characters in the sequence
see
InputStream#readChars(char[],int,int)

    final int end = start + length;
    for (int i = start; i < end; i++) {
      final int code = (int)s.charAt(i);
      if (code >= 0x01 && code <= 0x7F)
	writeByte((byte)code);
      else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {
	writeByte((byte)(0xC0 | (code >> 6)));
	writeByte((byte)(0x80 | (code & 0x3F)));
      } else {
	writeByte((byte)(0xE0 | (code >>> 12)));
	writeByte((byte)(0x80 | ((code >> 6) & 0x3F)));
	writeByte((byte)(0x80 | (code & 0x3F)));
      }
    }
  
public final voidwriteInt(int i)
Writes an int as four bytes.

see
InputStream#readInt()

    writeByte((byte)(i >> 24));
    writeByte((byte)(i >> 16));
    writeByte((byte)(i >>  8));
    writeByte((byte) i);
  
public final voidwriteLong(long i)
Writes a long as eight bytes.

see
InputStream#readLong()

    writeInt((int) (i >> 32));
    writeInt((int) i);
  
public final voidwriteString(java.lang.String s)
Writes a string.

see
InputStream#readString()

    int length = s.length();
    writeVInt(length);
    writeChars(s, 0, length);
  
public final voidwriteVInt(int 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.

see
InputStream#readVInt()

    while ((i & ~0x7F) != 0) {
      writeByte((byte)((i & 0x7f) | 0x80));
      i >>>= 7;
    }
    writeByte((byte)i);
  
public final voidwriteVLong(long 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.

see
InputStream#readVLong()

    while ((i & ~0x7F) != 0) {
      writeByte((byte)((i & 0x7f) | 0x80));
      i >>>= 7;
    }
    writeByte((byte)i);