1 /* |
|
2 * Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 package sun.rmi.transport.proxy; |
|
26 |
|
27 import java.io.*; |
|
28 |
|
29 /** |
|
30 * The HttpSendInputStream class is used by the HttpSendSocket class as |
|
31 * a layer on the top of the InputStream it returns so that it can be |
|
32 * notified of attempts to read from it. This allows the HttpSendSocket |
|
33 * to know when it should push across its output message. |
|
34 */ |
|
35 class HttpSendInputStream extends FilterInputStream { |
|
36 |
|
37 /** the HttpSendSocket object that is providing this stream */ |
|
38 HttpSendSocket owner; |
|
39 |
|
40 /** |
|
41 * Create new filter on a given input stream. |
|
42 * @param in the InputStream to filter from |
|
43 * @param owner the HttpSendSocket that is providing this stream |
|
44 */ |
|
45 public HttpSendInputStream(InputStream in, HttpSendSocket owner) |
|
46 throws IOException |
|
47 { |
|
48 super(in); |
|
49 |
|
50 this.owner = owner; |
|
51 } |
|
52 |
|
53 /** |
|
54 * Mark this stream as inactive for its owner socket, so the next time |
|
55 * a read is attempted, the owner will be notified and a new underlying |
|
56 * input stream obtained. |
|
57 */ |
|
58 public void deactivate() |
|
59 { |
|
60 in = null; |
|
61 } |
|
62 |
|
63 /** |
|
64 * Read a byte of data from the stream. |
|
65 */ |
|
66 public int read() throws IOException |
|
67 { |
|
68 if (in == null) |
|
69 in = owner.readNotify(); |
|
70 return in.read(); |
|
71 } |
|
72 |
|
73 /** |
|
74 * Read into an array of bytes. |
|
75 * @param b the buffer into which the data is to be read |
|
76 * @param off the start offset of the data |
|
77 * @param len the maximum number of bytes to read |
|
78 */ |
|
79 public int read(byte b[], int off, int len) throws IOException |
|
80 { |
|
81 if (len == 0) |
|
82 return 0; |
|
83 if (in == null) |
|
84 in = owner.readNotify(); |
|
85 return in.read(b, off, len); |
|
86 } |
|
87 |
|
88 /** |
|
89 * Skip bytes of input. |
|
90 * @param n the number of bytes to be skipped |
|
91 */ |
|
92 public long skip(long n) throws IOException |
|
93 { |
|
94 if (n == 0) |
|
95 return 0; |
|
96 if (in == null) |
|
97 in = owner.readNotify(); |
|
98 return in.skip(n); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Return the number of bytes that can be read without blocking. |
|
103 */ |
|
104 public int available() throws IOException |
|
105 { |
|
106 if (in == null) |
|
107 in = owner.readNotify(); |
|
108 return in.available(); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Close the stream. |
|
113 */ |
|
114 public void close() throws IOException |
|
115 { |
|
116 owner.close(); |
|
117 } |
|
118 |
|
119 /** |
|
120 * Mark the current position in the stream. |
|
121 * @param readlimit how many bytes can be read before mark becomes invalid |
|
122 */ |
|
123 public synchronized void mark(int readlimit) |
|
124 { |
|
125 if (in == null) { |
|
126 try { |
|
127 in = owner.readNotify(); |
|
128 } |
|
129 catch (IOException e) { |
|
130 return; |
|
131 } |
|
132 } |
|
133 in.mark(readlimit); |
|
134 } |
|
135 |
|
136 /** |
|
137 * Reposition the stream to the last marked position. |
|
138 */ |
|
139 public synchronized void reset() throws IOException |
|
140 { |
|
141 if (in == null) |
|
142 in = owner.readNotify(); |
|
143 in.reset(); |
|
144 } |
|
145 |
|
146 /** |
|
147 * Return true if this stream type supports mark/reset. |
|
148 */ |
|
149 public boolean markSupported() |
|
150 { |
|
151 if (in == null) { |
|
152 try { |
|
153 in = owner.readNotify(); |
|
154 } |
|
155 catch (IOException e) { |
|
156 return false; |
|
157 } |
|
158 } |
|
159 return in.markSupported(); |
|
160 } |
|
161 } |
|