Line | Hits | Source |
---|---|---|
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; | |
33 | 2 | private boolean eof = false; |
34 | 2 | 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) { | |
41 | 2 | super(in); |
42 | 2 | header = new byte[5]; |
43 | 2 | } |
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 { | |
52 | 6 | if (eof) return; |
53 | ||
54 | 6 | int bytesRead = in.read(header); |
55 | 6 | int thisPass = 0; |
56 | 6 | while (bytesRead < 5) { |
57 | 0 | thisPass = in.read(header, bytesRead, 5 - bytesRead); |
58 | 0 | if (thisPass < 0) { |
59 | 0 | eof = true; |
60 | 0 | return; |
61 | } | |
62 | 0 | bytesRead += thisPass; |
63 | } | |
64 | 6 | switch(header[4]) { |
65 | case NGConstants.CHUNKTYPE_STDIN: | |
66 | 4 | remaining = LongUtils.fromArray(header, 0); |
67 | 4 | break; |
68 | ||
69 | case NGConstants.CHUNKTYPE_STDIN_EOF: | |
70 | 2 | eof = true; |
71 | 2 | break; |
72 | ||
73 | 0 | default: throw(new IOException("Unknown stream type: " + (char) header[4])); |
74 | } | |
75 | 6 | } |
76 | ||
77 | /** | |
78 | * @see java.io.InputStream#available() | |
79 | */ | |
80 | public int available() throws IOException { | |
81 | 1 | if (eof) return(0); |
82 | 1 | if (remaining > 0) return (in.available()); |
83 | 1 | return (Math.max(0, in.available() - 5)); |
84 | } | |
85 | ||
86 | /** | |
87 | * @see java.io.InputStream#markSupported() | |
88 | */ | |
89 | public boolean markSupported() { | |
90 | 1 | 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 | |
100 | 16 | 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 { | |
107 | 3 | 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 { | |
114 | 19 | if (remaining == 0) readHeader(); |
115 | 19 | if (eof) return(-1); |
116 | ||
117 | 17 | int bytesToRead = Math.min((int) remaining, length); |
118 | 17 | int result = in.read(b, offset, bytesToRead); |
119 | 17 | remaining -= result; |
120 | 17 | return (result); |
121 | } | |
122 | ||
123 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |