KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > NGInputStream


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 JavaDoc;
22 import java.io.IOException JavaDoc;
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 JavaDoc {
31
32     private byte[] header;
33     private boolean eof = false;
34     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 JavaDoc in) {
41         super(in);
42         header = new byte[5];
43     }
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 JavaDoc {
52         if (eof) return;
53         
54         int bytesRead = in.read(header);
55         int thisPass = 0;
56         while (bytesRead < 5) {
57             thisPass = in.read(header, bytesRead, 5 - bytesRead);
58             if (thisPass < 0) {
59                 eof = true;
60                 return;
61             }
62             bytesRead += thisPass;
63         }
64         switch(header[4]) {
65             case NGConstants.CHUNKTYPE_STDIN:
66                         remaining = LongUtils.fromArray(header, 0);
67                         break;
68                         
69             case NGConstants.CHUNKTYPE_STDIN_EOF:
70                         eof = true;
71                         break;
72                         
73             default: throw(new IOException JavaDoc("Unknown stream type: " + (char) header[4]));
74         }
75     }
76     
77     /**
78      * @see java.io.InputStream#available()
79      */

80     public int available() throws IOException JavaDoc {
81         if (eof) return(0);
82         if (remaining > 0) return (in.available());
83         return (Math.max(0, in.available() - 5));
84     }
85     
86     /**
87      * @see java.io.InputStream#markSupported()
88      */

89     public boolean markSupported() {
90         return (false);
91     }
92     
93     /**
94      * @see java.io.InputStream#read()
95      */

96     public int read() throws IOException JavaDoc {
97         // this should be more readable.
98
// this stomps on the first byte of the header array,
99
// which is ok
100
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 JavaDoc {
107         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 JavaDoc {
114         if (remaining == 0) readHeader();
115         if (eof) return(-1);
116
117         int bytesToRead = Math.min((int) remaining, length);
118         int result = in.read(b, offset, bytesToRead);
119         remaining -= result;
120         return (result);
121     }
122
123 }
124
Popular Tags