Coverage Report - org.apache.xmlrpc.webserver.ServletOutputStreamImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletOutputStreamImpl
56% 
64% 
2,75
 
 1  
 package org.apache.xmlrpc.webserver;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.OutputStream;
 5  
 
 6  
 import javax.servlet.ServletOutputStream;
 7  
 
 8  
 
 9  
 /** Default implementation of a servlet output stream.
 10  
  * Handles output of HTTP headers.
 11  
  */
 12  
 class ServletOutputStreamImpl extends ServletOutputStream {
 13  
         private final OutputStream target;
 14  
         private final HttpServletResponseImpl res;
 15  70
         private final byte[] buffer = new byte[HttpServletResponseImpl.BUFFER_SIZE];
 16  
         private int bufferOffset;
 17  
         private boolean closed;
 18  
         private boolean committed;
 19  
 
 20  70
         ServletOutputStreamImpl(OutputStream pTarget, HttpServletResponseImpl pResponse) {
 21  70
                 target = pTarget;
 22  70
                 res = pResponse;
 23  70
         }
 24  
 
 25  
         public void write(int b) throws IOException {
 26  0
                 if (closed) {
 27  0
                         throw new IOException("This output stream is already closed.");
 28  
                 }
 29  0
                 if (bufferOffset == buffer.length) {
 30  0
                         flush();
 31  
                 }
 32  0
                 buffer[bufferOffset++] = (byte) b;
 33  0
         }
 34  
 
 35  
         public void write(byte[] pChars, int pOffset, int pLen) throws IOException {
 36  70
                 if (closed) {
 37  0
                         throw new IOException("This output stream is already closed.");
 38  
                 }
 39  14950
                 while (pLen-- > 0) {
 40  14880
                         if (bufferOffset == buffer.length) {
 41  0
                                 flush();
 42  
                         }
 43  14880
                         buffer[bufferOffset++] = pChars[pOffset++];
 44  
                 }
 45  70
         }
 46  
 
 47  
         private void flush(boolean pClosing) throws IOException {
 48  70
                 if (!committed) {
 49  70
                         committed = true;
 50  70
                         String headers = res.getHttpHeaders(pClosing ? new Integer(bufferOffset) : null);
 51  70
                         target.write(headers.getBytes("US-ASCII"));
 52  
                 }
 53  70
                 if (bufferOffset > 0) {
 54  70
                         target.write(buffer, 0, bufferOffset);
 55  
                 }
 56  70
         }
 57  
 
 58  
         public void close() throws IOException {
 59  140
                 if (!closed) {
 60  70
                         flush(true);
 61  70
                         closed = true;
 62  70
                         target.close();
 63  
                 }
 64  140
         }
 65  
 
 66  
         public void flush() throws IOException {
 67  0
                 if (closed) {
 68  0
                         throw new IOException("This output stream is already closed.");
 69  
                 }
 70  0
                 flush(false);
 71  0
                 target.flush();
 72  0
         }
 73  
 
 74  
         void reset() {
 75  0
                 if (committed) {
 76  0
                         throw new IllegalStateException("The response is already committed. A reset cannot be performed.");
 77  
                 }
 78  0
         }
 79  
 
 80  
         boolean isCommitted() {
 81  0
                 return committed;
 82  
         }
 83  
 }