Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/rmi/transport/proxy/HttpInputStream.java
+++ new/src/share/classes/sun/rmi/transport/proxy/HttpInputStream.java
1 1 /*
2 2 * Copyright (c) 1996, 2001, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package sun.rmi.transport.proxy;
26 26
27 27 import java.io.*;
28 28
29 29 import sun.rmi.runtime.Log;
30 30
31 31 /**
32 32 * The HttpInputStream class assists the HttpSendSocket and HttpReceiveSocket
33 33 * classes by filtering out the header for the message as well as any
34 34 * data after its proper content length.
35 35 */
36 36 class HttpInputStream extends FilterInputStream {
37 37
38 38 /** bytes remaining to be read from proper content of message */
39 39 protected int bytesLeft;
40 40
41 41 /** bytes remaining to be read at time of last mark */
42 42 protected int bytesLeftAtMark;
43 43
44 44 /**
45 45 * Create new filter on a given input stream.
46 46 * @param in the InputStream to filter from
47 47 */
48 48 public HttpInputStream(InputStream in) throws IOException
49 49 {
50 50 super(in);
51 51
52 52 if (in.markSupported())
53 53 in.mark(0); // prevent resetting back to old marks
54 54
55 55 // pull out header, looking for content length
56 56
57 57 DataInputStream dis = new DataInputStream(in);
58 58 String key = "Content-length:".toLowerCase();
59 59 boolean contentLengthFound = false;
60 60 String line;
61 61 do {
62 62 line = dis.readLine();
↓ open down ↓ |
62 lines elided |
↑ open up ↑ |
63 63
64 64 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
65 65 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
66 66 "received header line: \"" + line + "\"");
67 67 }
68 68
69 69 if (line == null)
70 70 throw new EOFException();
71 71
72 72 if (line.toLowerCase().startsWith(key)) {
73 - if (contentLengthFound)
74 - ; // what would we want to do in this case??
73 + // if contentLengthFound is true
74 + // we should probably do something here
75 75 bytesLeft =
76 76 Integer.parseInt(line.substring(key.length()).trim());
77 77 contentLengthFound = true;
78 78 }
79 79
80 80 // The idea here is to go past the first blank line.
81 81 // Some DataInputStream.readLine() documentation specifies that
82 82 // it does include the line-terminating character(s) in the
83 83 // returned string, but it actually doesn't, so we'll cover
84 84 // all cases here...
85 85 } while ((line.length() != 0) &&
86 86 (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
87 87
88 88 if (!contentLengthFound || bytesLeft < 0) {
89 89 // This really shouldn't happen, but if it does, shoud we fail??
90 90 // For now, just give up and let a whole lot of bytes through...
91 91 bytesLeft = Integer.MAX_VALUE;
92 92 }
93 93 bytesLeftAtMark = bytesLeft;
94 94
95 95 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
96 96 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
97 97 "content length: " + bytesLeft);
98 98 }
99 99 }
100 100
101 101 /**
102 102 * Returns the number of bytes that can be read with blocking.
103 103 * Make sure that this does not exceed the number of bytes remaining
104 104 * in the proper content of the message.
105 105 */
106 106 public int available() throws IOException
107 107 {
108 108 int bytesAvailable = in.available();
109 109 if (bytesAvailable > bytesLeft)
110 110 bytesAvailable = bytesLeft;
111 111
112 112 return bytesAvailable;
113 113 }
114 114
115 115 /**
116 116 * Read a byte of data from the stream. Make sure that one is available
117 117 * from the proper content of the message, else -1 is returned to
118 118 * indicate to the user that the end of the stream has been reached.
119 119 */
120 120 public int read() throws IOException
121 121 {
122 122 if (bytesLeft > 0) {
123 123 int data = in.read();
124 124 if (data != -1)
125 125 -- bytesLeft;
126 126
127 127 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
128 128 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
129 129 "received byte: '" +
130 130 ((data & 0x7F) < ' ' ? " " : String.valueOf((char) data)) +
131 131 "' " + data);
132 132 }
133 133
134 134 return data;
135 135 }
136 136 else {
137 137 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
138 138 "read past content length");
139 139
140 140 return -1;
141 141 }
142 142 }
143 143
144 144 public int read(byte b[], int off, int len) throws IOException
145 145 {
146 146 if (bytesLeft == 0 && len > 0) {
147 147 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
148 148 "read past content length");
149 149
150 150 return -1;
151 151 }
152 152 if (len > bytesLeft)
153 153 len = bytesLeft;
154 154 int bytesRead = in.read(b, off, len);
155 155 bytesLeft -= bytesRead;
156 156
157 157 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
158 158 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
159 159 "read " + bytesRead + " bytes, " + bytesLeft + " remaining");
160 160 }
161 161
162 162 return bytesRead;
163 163 }
164 164
165 165 /**
166 166 * Mark the current position in the stream (for future calls to reset).
167 167 * Remember where we are within the proper content of the message, so
168 168 * that a reset method call can recreate our state properly.
169 169 * @param readlimit how many bytes can be read before mark becomes invalid
170 170 */
171 171 public void mark(int readlimit)
172 172 {
173 173 in.mark(readlimit);
174 174 if (in.markSupported())
175 175 bytesLeftAtMark = bytesLeft;
176 176 }
177 177
178 178 /**
179 179 * Repositions the stream to the last marked position. Make sure to
180 180 * adjust our position within the proper content accordingly.
181 181 */
182 182 public void reset() throws IOException
183 183 {
184 184 in.reset();
185 185 bytesLeft = bytesLeftAtMark;
186 186 }
187 187
188 188 /**
189 189 * Skips bytes of the stream. Make sure to adjust our
190 190 * position within the proper content accordingly.
191 191 * @param n number of bytes to be skipped
192 192 */
193 193 public long skip(long n) throws IOException
194 194 {
195 195 if (n > bytesLeft)
196 196 n = bytesLeft;
197 197 long bytesSkipped = in.skip(n);
198 198 bytesLeft -= bytesSkipped;
199 199 return bytesSkipped;
200 200 }
201 201 }
↓ open down ↓ |
117 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX