1 7 8 package java.net; 9 10 import java.io.FileDescriptor ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.nio.channels.FileChannel ; 14 15 24 class SocketOutputStream extends FileOutputStream 25 { 26 static { 27 init(); 28 } 29 30 private PlainSocketImpl impl = null; 31 private byte temp[] = new byte[1]; 32 private Socket socket = null; 33 34 40 SocketOutputStream(PlainSocketImpl impl) throws IOException { 41 super(impl.getFileDescriptor()); 42 this.impl = impl; 43 socket = impl.getSocket(); 44 } 45 46 58 public final FileChannel getChannel() { 59 return null; 60 } 61 62 70 private native void socketWrite0(FileDescriptor fd, byte[] b, int off, 71 int len) throws IOException ; 72 73 81 private void socketWrite(byte b[], int off, int len) throws IOException { 82 83 if (len <= 0 || off < 0 || off + len > b.length) { 84 if (len == 0) { 85 return; 86 } 87 throw new ArrayIndexOutOfBoundsException (); 88 } 89 90 FileDescriptor fd = impl.acquireFD(); 91 try { 92 socketWrite0(fd, b, off, len); 93 } catch (SocketException se) { 94 if (se instanceof sun.net.ConnectionResetException) { 95 impl.setConnectionResetPending(); 96 se = new SocketException ("Connection reset"); 97 } 98 if (impl.isClosedOrPending()) { 99 throw new SocketException ("Socket closed"); 100 } else { 101 throw se; 102 } 103 } finally { 104 impl.releaseFD(); 105 } 106 } 107 108 113 public void write(int b) throws IOException { 114 temp[0] = (byte)b; 115 socketWrite(temp, 0, 1); 116 } 117 118 123 public void write(byte b[]) throws IOException { 124 socketWrite(b, 0, b.length); 125 } 126 127 135 public void write(byte b[], int off, int len) throws IOException { 136 socketWrite(b, off, len); 137 } 138 139 142 private boolean closing = false; 143 public void close() throws IOException { 144 if (closing) 146 return; 147 closing = true; 148 if (socket != null) { 149 if (!socket.isClosed()) 150 socket.close(); 151 } else 152 impl.close(); 153 closing = false; 154 } 155 156 159 protected void finalize() {} 160 161 164 private native static void init(); 165 166 } 167 | Popular Tags |