1 7 8 package java.net; 9 10 import java.io.FileDescriptor ; 11 import java.io.FileInputStream ; 12 import java.io.IOException ; 13 import java.nio.channels.FileChannel ; 14 15 import sun.net.ConnectionResetException; 16 17 26 class SocketInputStream extends FileInputStream 27 { 28 static { 29 init(); 30 } 31 32 private boolean eof; 33 private PlainSocketImpl impl = null; 34 private byte temp[]; 35 private Socket socket = null; 36 37 43 SocketInputStream(PlainSocketImpl impl) throws IOException { 44 super(impl.getFileDescriptor()); 45 this.impl = impl; 46 socket = impl.getSocket(); 47 } 48 49 61 public final FileChannel getChannel() { 62 return null; 63 } 64 65 77 private native int socketRead0(FileDescriptor fd, 78 byte b[], int off, int len, 79 int timeout) 80 throws IOException ; 81 82 89 public int read(byte b[]) throws IOException { 90 return read(b, 0, b.length); 91 } 92 93 103 public int read(byte b[], int off, int length) throws IOException { 104 int n; 105 106 if (eof) { 108 return -1; 109 } 110 111 if (impl.isConnectionReset()) { 113 throw new SocketException ("Connection reset"); 114 } 115 116 if (length <= 0 || off < 0 || off + length > b.length) { 118 if (length == 0) { 119 return 0; 120 } 121 throw new ArrayIndexOutOfBoundsException (); 122 } 123 124 boolean gotReset = false; 125 126 FileDescriptor fd = impl.acquireFD(); 128 try { 129 n = socketRead0(fd, b, off, length, impl.getTimeout()); 130 if (n > 0) { 131 return n; 132 } 133 } catch (ConnectionResetException rstExc) { 134 gotReset = true; 135 } finally { 136 impl.releaseFD(); 137 } 138 139 143 if (gotReset) { 144 impl.setConnectionResetPending(); 145 impl.acquireFD(); 146 try { 147 n = socketRead0(fd, b, off, length, impl.getTimeout()); 148 if (n > 0) { 149 return n; 150 } 151 } catch (ConnectionResetException rstExc) { 152 } finally { 153 impl.releaseFD(); 154 } 155 } 156 157 161 if (impl.isClosedOrPending()) { 162 throw new SocketException ("Socket closed"); 163 } 164 if (impl.isConnectionResetPending()) { 165 impl.setConnectionReset(); 166 } 167 if (impl.isConnectionReset()) { 168 throw new SocketException ("Connection reset"); 169 } 170 eof = true; 171 return -1; 172 } 173 174 177 public int read() throws IOException { 178 if (eof) { 179 return -1; 180 } 181 temp = new byte[1]; 182 int n = read(temp, 0, 1); 183 if (n <= 0) { 184 return -1; 185 } 186 return temp[0] & 0xff; 187 } 188 189 195 public long skip(long numbytes) throws IOException { 196 if (numbytes <= 0) { 197 return 0; 198 } 199 long n = numbytes; 200 int buflen = (int) Math.min(1024, n); 201 byte data[] = new byte[buflen]; 202 while (n > 0) { 203 int r = read(data, 0, (int) Math.min((long) buflen, n)); 204 if (r < 0) { 205 break; 206 } 207 n -= r; 208 } 209 return numbytes - n; 210 } 211 212 216 public int available() throws IOException { 217 return impl.available(); 218 } 219 220 223 private boolean closing = false; 224 public void close() throws IOException { 225 if (closing) 227 return; 228 closing = true; 229 if (socket != null) { 230 if (!socket.isClosed()) 231 socket.close(); 232 } else 233 impl.close(); 234 closing = false; 235 } 236 237 void setEOF(boolean eof) { 238 this.eof = eof; 239 } 240 241 244 protected void finalize() {} 245 246 249 private native static void init(); 250 } 251 252 | Popular Tags |