Code example for WeakHashMap

Methods: get, put, remove

0
     * 
     * @return a BufferInfo for the current thread to write data to 
     */ 
    private BufferInfo getBufferInfo() { 
        Thread current = Thread.currentThread();
        BufferInfo bufferInfo = (BufferInfo) buffers.get(current);
        if (bufferInfo == null) {
            bufferInfo = new BufferInfo();
            bufferInfo.buffer = new ByteArrayOutputStream(INTIAL_SIZE);
            bufferInfo.crSeen = false;
            buffers.put(current, bufferInfo);
        } 
        return bufferInfo;
    } 
 
    /** 
     * Resets the buffer for the current thread. 
     */ 
    private void resetBufferInfo() { 
        Thread current = Thread.currentThread();
        BufferInfo bufferInfo = (BufferInfo) buffers.get(current);
        try { 
            bufferInfo.buffer.close();
        } catch (IOException e) {
            // Shouldn't happen 
        } 
        bufferInfo.buffer = new ByteArrayOutputStream();
        bufferInfo.crSeen = false;
    } 
 
    /** 
     * Removes the buffer for the current thread. 
     */ 
    private void removeBuffer() { 
        Thread current = Thread.currentThread();
        buffers.remove (current);
    } 
 
    /** 
     * Writes the data to the buffer and flushes the buffer if a line 
     * separator is detected or if the buffer has reached its maximum size.