Code example for SessionOutputBuffer

Methods: flush, write, writeLine

0
    protected void flushCacheWithAppend(byte bufferToAppend[], int off, int len) throws IOException { 
        this.out.writeLine(Integer.toHexString(this.cachePosition + len)); 
        this.out.write(this.cache, 0, this.cachePosition); 
        this.out.write(bufferToAppend, off, len); 
        this.out.writeLine(""); 
        this.cachePosition = 0; 
    } 
 
    protected void writeClosingChunk() throws IOException { 
        // Write the final chunk. 
        this.out.writeLine("0"); 
        this.out.writeLine(""); 
    } 
 
    // ----------------------------------------------------------- Public Methods 
    /** 
     * Must be called to ensure the internal cache is flushed and the closing 
     * chunk is written. 
     * @throws IOException in case of an I/O error 
     */ 
    public void finish() throws IOException { 
        if (!this.wroteLastChunk) { 
            flushCache(); 
            writeClosingChunk(); 
            this.wroteLastChunk = true; 
        } 
    } 
 
    // -------------------------------------------- OutputStream Methods 
    public void write(int b) throws IOException { 
        if (this.closed) { 
            throw new IOException("Attempted write to closed stream."); 
        } 
        this.cache[this.cachePosition] = (byte) b; 
        this.cachePosition++; 
        if (this.cachePosition == this.cache.length) flushCache(); 
    } 
 
    /** 
     * Writes the array. If the array does not fit within the buffer, it is 
     * not split, but rather written out as one large chunk. 
     */ 
    public void write(byte b[]) throws IOException { 
        write(b, 0, b.length); 
    } 
 
    /** 
     * Writes the array. If the array does not fit within the buffer, it is 
     * not split, but rather written out as one large chunk. 
     */ 
    public void write(byte src[], int off, int len) throws IOException { 
        if (this.closed) { 
            throw new IOException("Attempted write to closed stream."); 
        } 
        if (len >= this.cache.length - this.cachePosition) { 
            flushCacheWithAppend(src, off, len); 
        } else { 
            System.arraycopy(src, off, cache, this.cachePosition, len); 
            this.cachePosition += len; 
        } 
    } 
 
    /** 
     * Flushes the content buffer and the underlying stream. 
     */ 
    public void flush() throws IOException { 
        flushCache(); 
        this.out.flush(); 
    } 
 
    /** 
     * Finishes writing to the underlying stream, but does NOT close the underlying stream. 
     */ 
    public void close() throws IOException { 
        if (!this.closed) { 
            this.closed = true; 
            finish(); 
            this.out.flush(); 
        } 
    } 
}