jmoore      01/07/12 10:29:04

  Modified:    contribs/JimMoore LoggingOutputStream.java
  Log:
  Previously it assumed that blank lines contained exactly one byte, the newline 
character.  That worked fine on Unix, but not other OSes like Windows.  It now handles 
it in an OS independant manner.
  
  Revision  Changes    Path
  1.2       +28 -31    jakarta-log4j/contribs/JimMoore/LoggingOutputStream.java
  
  Index: LoggingOutputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/contribs/JimMoore/LoggingOutputStream.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LoggingOutputStream.java  2001/04/20 17:32:56     1.1
  +++ LoggingOutputStream.java  2001/07/12 17:29:02     1.2
  @@ -3,7 +3,8 @@
    *
    * This software is published under the terms of the Apache Software
    * License version 1.1, a copy of which has been included with this
  - * distribution in the LICENSE.APL file.  */
  + * distribution in the LICENSE.APL file.
  + */
   
   
   import java.io.*;
  @@ -13,8 +14,8 @@
   /**
    * An OutputStream that flushes out to a Category.<p>
    * 
  - * Note that no data is written out to the Category until the stream
  - * is flushed or closed.<p>
  + * Note that no data is written out to the Category until the stream is
  + *   flushed or closed.<p>
    * 
    * Example:<pre>
    * // make sure everything sent to System.err is logged
  @@ -25,21 +26,22 @@
    * </pre>
    * 
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jim Moore</a>
  - * @see Category */
  + * @see Category
  + */
   public class LoggingOutputStream extends OutputStream {
  +  protected static final String LINE_SEPERATOR = 
System.getProperty("line.separator");
   
  +
     /**
      * Used to maintain the contract of {@link #close()}.
      */
     protected boolean hasBeenClosed = false;
   
  -
     /**
      * The internal buffer where data is stored. 
      */
     protected byte[] buf;
   
  -
     /**
      * The number of valid bytes in the buffer. This value is always 
      *   in the range <tt>0</tt> through <tt>buf.length</tt>; elements 
  @@ -54,25 +56,23 @@
      */
     private int bufLength;
   
  -
     /**
      * The default number of bytes in the buffer. =2048
      */
     public static final int DEFAULT_BUFFER_LENGTH = 2048;
   
   
  -
     /**
      * The category to write to.
      */
     protected Category category;
   
  -
     /**
      * The priority to use when writing to the Category.
      */
     protected Priority priority;
   
  +
     private LoggingOutputStream() {
       // illegal
     }
  @@ -96,7 +96,7 @@
       if (priority == null) {
         throw new IllegalArgumentException("priority == null");
       }
  -    
  +
       this.priority = priority;
       category = cat;
       bufLength = DEFAULT_BUFFER_LENGTH;
  @@ -105,11 +105,9 @@
     }
   
   
  -  
     /**
      * Closes this output stream and releases any system resources
  -   *   associated with this stream. The general contract of
  -<code>close</code>
  +   *   associated with this stream. The general contract of <code>close</code>
      *   is that it closes the output stream. A closed stream cannot perform
      *   output operations and cannot be reopened.
      */
  @@ -117,15 +115,14 @@
       flush();
       hasBeenClosed = true;
     }
  -  
   
   
     /**
      * Writes the specified byte to this output stream. The general
  -   *   contract for <code>write</code> is that one byte is written
  -   *   to the output stream. The byte to be written is the eight
  -   *   low-order bits of the argument <code>b</code>. The 24
  -   *   high-order bits of <code>b</code> are ignored.
  +   * contract for <code>write</code> is that one byte is written
  +   * to the output stream. The byte to be written is the eight
  +   * low-order bits of the argument <code>b</code>. The 24
  +   * high-order bits of <code>b</code> are ignored.
      * 
      * @param b          the <code>byte</code> to write
      * 
  @@ -139,33 +136,28 @@
         throw new IOException("The stream has been closed.");
       }
   
  -
       // don't log nulls
       if (b == 0) {
         return;
       }
   
  -
       // would this be writing past the buffer?
       if (count == bufLength) {
         // grow the buffer
         final int newBufLength = bufLength+DEFAULT_BUFFER_LENGTH;
         final byte[] newBuf = new byte[newBufLength];
  -      
  -      System.arraycopy(buf, 0, newBuf, 0, bufLength);
   
  +      System.arraycopy(buf, 0, newBuf, 0, bufLength);
   
         buf = newBuf;
         bufLength = newBufLength;
       }
   
  -
       buf[count] = (byte)b;
       count++;
     }
   
   
  -  
     /**
      * Flushes this output stream and forces any buffered output bytes
      *   to be written out. The general contract of <code>flush</code> is
  @@ -175,30 +167,35 @@
      *   intended destination.
      */
     public void flush() {
  -    if (count == 0)  {
  +    if (count == 0) {
         return;
       }
   
       // don't print out blank lines; flushing from PrintStream puts out these
  -    if (count == 1 && ((char)buf[0]) == '\n')  {
  -      reset();
  -      return;
  +    if (count == LINE_SEPERATOR.length()) {
  +      if ( ((char)buf[0]) == LINE_SEPERATOR.charAt(0)  &&
  +           ( ( count == 1 ) ||  // <- Unix & Mac, -> Windows
  +             ( (count == 2) && ((char)buf[1]) == LINE_SEPERATOR.charAt(1) ) ) ) {
  +        reset();
  +        return;
  +      }
       }
   
       final byte[] theBytes = new byte[count];
  +
       System.arraycopy(buf, 0, theBytes, 0, count);
  +
       category.log(priority, new String(theBytes));
  +
       reset();
     }
   
   
  -
     private void reset() {
       // not resetting the buffer -- assuming that if it grew that it
       //   will likely grow similarly again
       count = 0;
     }
  -}
   
  -
  +}
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to