Coverage details for com.martiansoftware.nailgun.NGInputStream

LineHitsSource
1 /*
2  
3   Copyright 2004, Martian Software, Inc.
4  
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8  
9   http://www.apache.org/licenses/LICENSE-2.0
10  
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16  
17 */
18  
19 package com.martiansoftware.nailgun;
20  
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23  
24 /**
25  * A FilterInputStream that is able to read the chunked stdin stream
26  * from a NailGun client.
27  *
28  * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
29  */
30 class NGInputStream extends FilterInputStream {
31  
32     private byte[] header;
332    private boolean eof = false;
342    private long remaining = 0;
35     
36     /**
37      * Creates a new NGInputStream wrapping the specified InputStream
38      * @param in the InputStream to wrap
39      */
40     public NGInputStream(java.io.InputStream in) {
412        super(in);
422        header = new byte[5];
432    }
44  
45     /**
46      * Reads a NailGun chunk header from the underlying InputStream.
47      *
48      * @throws IOException if thrown by the underlying InputStream,
49      * or if an unexpected NailGun chunk type is encountered.
50      */
51     private void readHeader() throws IOException {
526        if (eof) return;
53         
546        int bytesRead = in.read(header);
556        int thisPass = 0;
566        while (bytesRead < 5) {
570            thisPass = in.read(header, bytesRead, 5 - bytesRead);
580            if (thisPass < 0) {
590                eof = true;
600                return;
61             }
620            bytesRead += thisPass;
63         }
646        switch(header[4]) {
65             case NGConstants.CHUNKTYPE_STDIN:
664                        remaining = LongUtils.fromArray(header, 0);
674                        break;
68                         
69             case NGConstants.CHUNKTYPE_STDIN_EOF:
702                        eof = true;
712                        break;
72                         
730            default: throw(new IOException("Unknown stream type: " + (char) header[4]));
74         }
756    }
76     
77     /**
78      * @see java.io.InputStream#available()
79      */
80     public int available() throws IOException {
811        if (eof) return(0);
821        if (remaining > 0) return (in.available());
831        return (Math.max(0, in.available() - 5));
84     }
85     
86     /**
87      * @see java.io.InputStream#markSupported()
88      */
89     public boolean markSupported() {
901        return (false);
91     }
92     
93     /**
94      * @see java.io.InputStream#read()
95      */
96     public int read() throws IOException {
97         // this should be more readable.
98         // this stomps on the first byte of the header array,
99         // which is ok
10016        return((read(header, 0, 1) == -1) ? -1 : (int) header[0]);
101     }
102     
103     /**
104      * @see java.io.InputStream.read(byte[])
105      */
106     public int read(byte[] b) throws IOException {
1073        return (read(b, 0, b.length));
108     }
109     
110     /**
111      * @see java.io.InputStream.read(byte[],offset,length)
112      */
113     public int read(byte[] b, int offset, int length) throws IOException {
11419        if (remaining == 0) readHeader();
11519        if (eof) return(-1);
116  
11717        int bytesToRead = Math.min((int) remaining, length);
11817        int result = in.read(b, offset, bytesToRead);
11917        remaining -= result;
12017        return (result);
121     }
122  
123 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.