[logback-dev] svn commit: r1687 - logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet

noreply.ceki at qos.ch noreply.ceki at qos.ch
Mon May 5 17:55:11 CEST 2008


Author: ceki
Date: Mon May  5 17:55:10 2008
New Revision: 1687

Modified:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletResponse.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletInputStream.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletOutputStream.java

Log:
- refactoring TeeFilter and co

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletResponse.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletResponse.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletResponse.java	Mon May  5 17:55:10 2008
@@ -11,7 +11,7 @@
 public class TeeHttpServletResponse extends HttpServletResponseWrapper {
 
   TeeServletOutputStream teeServletOutputStream;
-  PrintWriter writer;
+  PrintWriter teeWriter;
 
   public TeeHttpServletResponse(HttpServletResponse httpServletResponse) {
     super(httpServletResponse);
@@ -27,17 +27,17 @@
 
   @Override
   public PrintWriter getWriter() throws IOException {
-    if (this.writer == null) {
-      this.writer = new PrintWriter(new OutputStreamWriter(getOutputStream()),
+    if (this.teeWriter == null) {
+      this.teeWriter = new PrintWriter(new OutputStreamWriter(getOutputStream()),
           true);
     }
-    return this.writer;
+    return this.teeWriter;
   }
 
   @Override
   public void flushBuffer() {
-    if (this.writer != null) {
-      this.writer.flush();
+    if (this.teeWriter != null) {
+      this.teeWriter.flush();
     }
   }
 
@@ -45,15 +45,15 @@
     // teeServletOutputStream can be null if the getOutputStream method is never
     // called.
     if (teeServletOutputStream != null) {
-      return teeServletOutputStream.getOutputBuffer();
+      return teeServletOutputStream.getOutputStreamAsByteArray();
     } else {
       return null;
     }
   }
 
   void finish() throws IOException {
-    if (this.writer != null) {
-      this.writer.close();
+    if (this.teeWriter != null) {
+      this.teeWriter.close();
     }
     if (this.teeServletOutputStream != null) {
       this.teeServletOutputStream.close();

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletInputStream.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletInputStream.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletInputStream.java	Mon May  5 17:55:10 2008
@@ -1,7 +1,6 @@
 package ch.qos.logback.access.servlet;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -30,12 +29,9 @@
       if (len < 0) {
         in = originalSIS;
       } else {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
         inputBuffer = new byte[len];
-        int n = 0;
-        while ((n = originalSIS.read(inputBuffer, 0, len)) != -1) {
-          baos.write(inputBuffer, 0, n);
-        }
+        int n = originalSIS.read(inputBuffer, 0, len);
+        assert n == len;
         this.in = new ByteArrayInputStream(inputBuffer);       
         originalSIS.close();
       }

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletOutputStream.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletOutputStream.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeServletOutputStream.java	Mon May  5 17:55:10 2008
@@ -9,24 +9,24 @@
 public class TeeServletOutputStream extends ServletOutputStream {
 
   final ServletOutputStream underlyingStream;
-  final ByteArrayOutputStream baos;
+  final ByteArrayOutputStream baosCopy;
 
   TeeServletOutputStream(ServletResponse httpServletResponse)
       throws IOException {
     // System.out.println("TeeServletOutputStream.constructor() called");
     this.underlyingStream = httpServletResponse.getOutputStream();
-    baos = new ByteArrayOutputStream();
+    baosCopy = new ByteArrayOutputStream();
   }
 
-  byte[] getOutputBuffer() {
-    return baos.toByteArray();
+  byte[] getOutputStreamAsByteArray() {
+    return baosCopy.toByteArray();
   }
 
   @Override
   public void write(int val) throws IOException {
     if (underlyingStream != null) {
       underlyingStream.write(val);
-      baos.write(val);
+      baosCopy.write(val);
     }
   }
 
@@ -49,31 +49,27 @@
     // called");
     // System.out.println(new String(byteArray, offset, length));
     underlyingStream.write(byteArray, offset, length);
-    baos.write(byteArray, offset, length);
+    baosCopy.write(byteArray, offset, length);
   }
 
+  @Override
   public void close() throws IOException {
     // System.out.println("CLOSE TeeServletOutputStream.close() called");
 
     // If the servlet accessing the stream is using a writer instead of
-    // an OutputStream, it will probably call os.close() begore calling
-    // writer.close. Thus, the undelying output stream will be called
+    // an OutputStream, it will probably call os.close() before calling
+    // writer.close. Thus, the underlying output stream will be called
     // before the data sent to the writer could be flushed.
   }
 
-  public void finish() throws IOException {
-    flush();
-    underlyingStream.close();
-    baos.close();
-  }
 
+  @Override
   public void flush() throws IOException {
     if (underlyingStream == null) {
       return;
     }
     // System.out.println("FLUSH TeeServletOutputStream.flush() called");
     underlyingStream.flush();
-    baos.flush();
+    baosCopy.flush();
   }
-
 }



More information about the logback-dev mailing list