1 /* 2 * Copyright (c) 1995, 2018, 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 26 package rdma.ch; 27 28 import java.io.FileDescriptor; 29 import java.io.FileOutputStream; 30 import java.io.IOException; 31 import java.nio.channels.FileChannel; 32 import java.net.Socket; 33 import java.net.SocketException; 34 35 class RdmaSocketOutputStream extends FileOutputStream 36 { 37 static { 38 init(); 39 } 40 41 private RdmaSocketImpl impl = null; 42 private byte temp[] = new byte[1]; 43 private Socket socket = null; 44 45 RdmaSocketOutputStream(RdmaSocketImpl impl) throws IOException { 46 super(impl.getFileDescriptor()); 47 this.impl = impl; 48 socket = impl.getSocket(); 49 } 50 51 public final FileChannel getChannel() { 52 return null; 53 } 54 55 /** 56 * Writes to the socket. 57 * @param fd the FileDescriptor 58 * @param b the data to be written 59 * @param off the start offset in the data 60 * @param len the number of bytes that are written 61 * @exception IOException If an I/O error has occurred. 62 */ 63 private native void rdmaSocketWrite0(FileDescriptor fd, byte[] b, int off, 64 int len) throws IOException; 65 66 /** 67 * Writes to the socket with appropriate locking of the 68 * FileDescriptor. 69 * @param b the data to be written 70 * @param off the start offset in the data 71 * @param len the number of bytes that are written 72 * @exception IOException If an I/O error has occurred. 73 */ 74 private void rdmaSocketWrite(byte b[], int off, int len) throws IOException { 75 76 77 if (len <= 0 || off < 0 || len > b.length - off) { 78 if (len == 0) { 79 return; 80 } 81 throw new ArrayIndexOutOfBoundsException("len == " + len 82 + " off == " + off + " buffer length == " + b.length); 83 } 84 85 FileDescriptor fd = impl.acquireFD(); 86 try { 87 rdmaSocketWrite0(fd, b, off, len); 88 } catch (SocketException se) { 89 if (se instanceof sun.net.ConnectionResetException) { 90 impl.setConnectionResetPending(); 91 se = new SocketException("Connection reset"); 92 } 93 if (impl.isClosedOrPending()) { 94 throw new SocketException("Socket closed"); 95 } else { 96 throw se; 97 } 98 } finally { 99 impl.releaseFD(); 100 } 101 } 102 103 /** 104 * Writes a byte to the socket. 105 * @param b the data to be written 106 * @exception IOException If an I/O error has occurred. 107 */ 108 public void write(int b) throws IOException { 109 temp[0] = (byte)b; 110 rdmaSocketWrite(temp, 0, 1); 111 } 112 113 /** 114 * Writes the contents of the buffer <i>b</i> to the socket. 115 * @param b the data to be written 116 * @exception SocketException If an I/O error has occurred. 117 */ 118 public void write(byte b[]) throws IOException { 119 rdmaSocketWrite(b, 0, b.length); 120 } 121 122 /** 123 * Writes <i>length</i> bytes from buffer <i>b</i> starting at 124 * offset <i>len</i>. 125 * @param b the data to be written 126 * @param off the start offset in the data 127 * @param len the number of bytes that are written 128 * @exception SocketException If an I/O error has occurred. 129 */ 130 public void write(byte b[], int off, int len) throws IOException { 131 rdmaSocketWrite(b, off, len); 132 } 133 134 /** 135 * Closes the stream. 136 */ 137 private boolean closing = false; 138 public void close() throws IOException { 139 if (closing) 140 return; 141 closing = true; 142 if (socket != null) { 143 if (!socket.isClosed()) 144 socket.close(); 145 } else 146 impl.close(); 147 closing = false; 148 } 149 150 /** 151 * Perform class load-time initializations. 152 */ 153 private static native void init(); 154 155 }