📄 protocol.java
字号:
// The buffer is empty, so the next read will go directly to native return available0(); } /** * Writes <code>len</code> bytes from the specified byte array * starting at offset <code>off</code> to this output stream. * <p> * Polling the will be done by our super class. * * @param b the data. * @param off the start offset in the data. * @param len the number of bytes to write. * @return number of bytes written * @exception IOException if an I/O error occurs. In particular, * an <code>IOException</code> is thrown if the output * stream is closed. */ public int writeBytes(byte b[], int off, int len) throws IOException { return write0(b, off, len); } /** * Called once by the child output stream. The output side of the socket * will be shutdown and then the parent method will be called. * * @exception IOException if the subclass throws one */ protected void closeOutputStream() throws IOException { /* * Shutdown the output gracefully closes the sending side of the * TCP connection by sending all pending data and the FIN flag. */ shutdownOutput0(); outputShutdown = true; super.closeOutputStream(); } /** * Check a socket option to make sure it's a valid option. * * @param option socket option identifier (KEEPALIVE, LINGER, * SNDBUF, RCVBUF, or DELAY) * @exception IllegalArgumentException if the value is not * valid (e.g. negative value) * * @see #getSocketOption * @see #setSocketOption */ private void checkOption(byte option) throws IllegalArgumentException { if (option == SocketConnection.KEEPALIVE || option == SocketConnection.LINGER || option == SocketConnection.SNDBUF || option == SocketConnection.RCVBUF || option == SocketConnection.DELAY) { return; } throw new IllegalArgumentException("Unsupported Socket Option"); } /** * Set a socket option for the connection. * <P> * Options inform the low level networking code about intended * usage patterns that the application will use in dealing with * the socket connection. * </P> * * @param option socket option identifier (KEEPALIVE, LINGER, * SNDBUF, RCVBUF, or DELAY) * @param value numeric value for specified option (must be positive) * @exception IllegalArgumentException if the value is not * valid (e.g. negative value) * @exception IOException if the connection was closed * * @see #getSocketOption */ public void setSocketOption(byte option, int value) throws IllegalArgumentException, IOException { checkOption(option); if (value < 0) { throw new IllegalArgumentException("Unsupported Socket Option"); } ensureOpen(); setSockOpt0(option, value); } /** * Get a socket option for the connection. * * @param option socket option identifier (KEEPALIVE, LINGER, * SNDBUF, RCVBUF, or DELAY) * @return positive numeric value for specified option or -1 if the * value is not available. * @exception IllegalArgumentException if the option identifier is * not valid * @exception IOException if the connection was closed * @see #setSocketOption */ public int getSocketOption(byte option) throws IllegalArgumentException, IOException { checkOption(option); ensureOpen(); return getSockOpt0(option); } /** * Gets the local address to which the socket is bound. * * <P>The host address(IP number) that can be used to connect to this * end of the socket connection from an external system. * Since IP addresses may be dynamically assigned a remote application * will need to be robust in the face of IP number reasssignment.</P> * <P> The local hostname (if available) can be accessed from * <code>System.getProperty("microedition.hostname")</code> * </P> * * @return the local address to which the socket is bound. * @exception IOException if the connection was closed * @see ServerSocketConnection */ public String getLocalAddress() throws IOException { ensureOpen(); return getHost0(true); } /** * Returns the local port to which this socket is bound. * * @return the local port number to which this socket is connected. * @exception IOException if the connection was closed * @see ServerSocketConnection */ public int getLocalPort() throws IOException { ensureOpen(); return getPort0(true); } /** * Gets the remote address to which the socket is bound. * The address can be either the remote host name or the IP * address(if available). * * @return the remote address to which the socket is bound. * @exception IOException if the connection was closed */ public String getAddress() throws IOException { ensureOpen(); return getHost0(false); } /** * Returns the remote port to which this socket is bound. * * @return the remote port number to which this socket is connected. * @exception IOException if the connection was closed */ public int getPort() throws IOException { ensureOpen(); return getPort0(false); } /** * Connect to a server and fillin the handle field. * * @param szHost host as a zero terminated ASCII string * @param port TCP port at host * * @exception IOException if an I/O error occurs. */ private native void open0(byte[] szHost, int port) throws IOException; /* * A note about read0() and write0() * * These routines will return the number of bytes transferred. It this * value is zero then it means that the data could not be read or written * and the calling code should call GeneralBase.iowait() to let some other * thread run. */ /** * Read from the socket, accesses the handle field. * * @param b the buffer into which the data is read. * @param off the start offset in array <code>b</code> * at which the data is written. * @param len the maximum number of bytes to read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end of * the stream has been reached. * @exception IOException if an I/O error occurs. */ private native int read0(byte b[], int off, int len) throws IOException; /** * Write to the socket, accesses the handle field. * * @param b the buffer of the data to write * @param off the start offset in array <code>b</code> * at which the data is written. * @param len the number of bytes to write. * @return the total number of bytes written * @exception IOException if an I/O error occurs. */ private native int write0(byte b[], int off, int len) throws IOException; /** * Get the number of bytes that can be read without blocking, * accesses the handle field. * * @return number of bytes that can be read without blocking * @exception IOException if an I/O error occurs. */ private native int available0() throws IOException; /** * Close the connection, accesses the handle field. * * @exception IOException if an I/O error occurs when closing the * connection. */ private native void close0() throws IOException; /** * Register with the native cleanup code, accesses the handle field. */ private native void registerCleanup(); /** * Native finalizer */ private native void finalize(); /** * Get the requested IP number. * * @param local <code>true</code for the local host, and * <code>false</code>for the remote host * @return the IP address as a String */ private native String getHost0(boolean local); /** * Get the requested port number. * * @param local <code>true</code for the local host, and * <code>false</code>for the remote host * @return the port number of the requested end point */ private native int getPort0(boolean local); /** * Get the requested socket option. * * @param option socket option to retrieve * @return value of the socket option */ private native int getSockOpt0(int option); /** * Set the requested socket option. * * @param option socket option to set * @param value of the socket option */ private native void setSockOpt0(int option, int value); /** * Shutdown the output side of the connection. */ private native void shutdownOutput0();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -