Browse Source

Move to WeakHashMap to reduce resource leakage on long-lived many-exec runs

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278214 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 20 years ago
parent
commit
72e3174bfd
1 changed files with 37 additions and 37 deletions
  1. +37
    -37
      src/main/org/apache/tools/ant/DemuxOutputStream.java

+ 37
- 37
src/main/org/apache/tools/ant/DemuxOutputStream.java View File

@@ -21,19 +21,18 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.WeakHashMap;


/** /**
* Logs content written by a thread and forwards the buffers onto the
* project object which will forward the content to the appropriate
* task.
* Logs content written by a thread and forwards the buffers onto the project object which will forward the content to
* the appropriate task.
* *
* @since 1.4 * @since 1.4
*/ */
public class DemuxOutputStream extends OutputStream { public class DemuxOutputStream extends OutputStream {


/** /**
* A data class to store information about a buffer. Such information
* is stored on a per-thread basis.
* A data class to store information about a buffer. Such information is stored on a per-thread basis.
*/ */
private static class BufferInfo { private static class BufferInfo {
/** /**
@@ -42,26 +41,36 @@ public class DemuxOutputStream extends OutputStream {
private ByteArrayOutputStream buffer; private ByteArrayOutputStream buffer;


/** /**
* Indicates we have just seen a carriage return. It may be part of
* a crlf pair or a single cr invoking processBuffer twice.
* Indicates we have just seen a carriage return. It may be part of a crlf pair or a single cr invoking
* processBuffer twice.
*/ */
private boolean crSeen = false;
private boolean crSeen = false;
} }


/** Maximum buffer size. */
/**
* Maximum buffer size.
*/
private static final int MAX_SIZE = 1024; private static final int MAX_SIZE = 1024;


/** Initial buffer size. */
/**
* Initial buffer size.
*/
private static final int INTIAL_SIZE = 132; private static final int INTIAL_SIZE = 132;


/** Carriage return */
/**
* Carriage return
*/
private static final int CR = 0x0d; private static final int CR = 0x0d;


/** Linefeed */
/**
* Linefeed
*/
private static final int LF = 0x0a; private static final int LF = 0x0a;


/** Mapping from thread to buffer (Thread to BufferInfo). */
private Hashtable buffers = new Hashtable();
/**
* Mapping from thread to buffer (Thread to BufferInfo).
*/
private WeakHashMap buffers = new WeakHashMap();


/** /**
* The project to send output to. * The project to send output to.
@@ -76,12 +85,10 @@ public class DemuxOutputStream extends OutputStream {
/** /**
* Creates a new instance of this class. * Creates a new instance of this class.
* *
* @param project The project instance for which output is being
* demultiplexed. Must not be <code>null</code>.
* @param isErrorStream <code>true</code> if this is the error string,
* otherwise a normal output stream. This is
* passed to the project so it knows
* which stream it is receiving.
* @param project The project instance for which output is being demultiplexed. Must not be
* <code>null</code>.
* @param isErrorStream <code>true</code> if this is the error string, otherwise a normal output stream. This is
* passed to the project so it knows which stream it is receiving.
*/ */
public DemuxOutputStream(Project project, boolean isErrorStream) { public DemuxOutputStream(Project project, boolean isErrorStream) {
this.project = project; this.project = project;
@@ -125,15 +132,15 @@ public class DemuxOutputStream extends OutputStream {
*/ */
private void removeBuffer() { private void removeBuffer() {
Thread current = Thread.currentThread(); Thread current = Thread.currentThread();
buffers.remove (current);
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.
* 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.
* *
* @param cc data to log (byte). * @param cc data to log (byte).
* @exception IOException if the data cannot be written to the stream
* @throws IOException if the data cannot be written to the stream
*/ */
public void write(int cc) throws IOException { public void write(int cc) throws IOException {
final byte c = (byte) cc; final byte c = (byte) cc;
@@ -161,9 +168,7 @@ public class DemuxOutputStream extends OutputStream {
/** /**
* Converts the buffer to a string and sends it to the project. * Converts the buffer to a string and sends it to the project.
* *
* @param buffer the ByteArrayOutputStream used to collect the output
* until a line separator is seen.
*
* @param buffer the ByteArrayOutputStream used to collect the output until a line separator is seen.
* @see Project#demuxOutput(String,boolean) * @see Project#demuxOutput(String,boolean)
*/ */
protected void processBuffer(ByteArrayOutputStream buffer) { protected void processBuffer(ByteArrayOutputStream buffer) {
@@ -175,9 +180,7 @@ public class DemuxOutputStream extends OutputStream {
/** /**
* Converts the buffer to a string and sends it to the project. * Converts the buffer to a string and sends it to the project.
* *
* @param buffer the ByteArrayOutputStream used to collect the output
* until a line separator is seen.
*
* @param buffer the ByteArrayOutputStream used to collect the output until a line separator is seen.
* @see Project#demuxOutput(String,boolean) * @see Project#demuxOutput(String,boolean)
*/ */
protected void processFlush(ByteArrayOutputStream buffer) { protected void processFlush(ByteArrayOutputStream buffer) {
@@ -189,8 +192,7 @@ public class DemuxOutputStream extends OutputStream {
/** /**
* Equivalent to flushing the stream. * Equivalent to flushing the stream.
* *
* @exception IOException if there is a problem closing the stream.
*
* @throws IOException if there is a problem closing the stream.
* @see #flush * @see #flush
*/ */
public void close() throws IOException { public void close() throws IOException {
@@ -199,10 +201,9 @@ public class DemuxOutputStream extends OutputStream {
} }


/** /**
* Writes all remaining data in the buffer associated
* with the current thread to the project.
* Writes all remaining data in the buffer associated with the current thread to the project.
* *
* @exception IOException if there is a problem flushing the stream.
* @throws IOException if there is a problem flushing the stream.
*/ */
public void flush() throws IOException { public void flush() throws IOException {
BufferInfo bufferInfo = getBufferInfo(); BufferInfo bufferInfo = getBufferInfo();
@@ -214,10 +215,9 @@ public class DemuxOutputStream extends OutputStream {
/** /**
* Write a block of characters to the output stream * Write a block of characters to the output stream
* *
* @param b the array containing the data
* @param b the array containing the data
* @param off the offset into the array where data starts * @param off the offset into the array where data starts
* @param len the length of block * @param len the length of block
*
* @throws IOException if the data cannot be written into the stream. * @throws IOException if the data cannot be written into the stream.
*/ */
public void write(byte[] b, int off, int len) throws IOException { public void write(byte[] b, int off, int len) throws IOException {


Loading…
Cancel
Save