📄 protocol.java
字号:
* an reads to fail so no native action is needed. * * Shutdown the output gracefully closes the sending side of the * TCP connection by sending all pending data and the FIN flag. */ if (!outputShutdown) { shutdownOutput0(); } try { close0(); } catch (IOException ioe) { // ignore } NetworkSubsystem.getInstance(classSecurityToken). unregisterSubsystem(this); } /** * Reads up to <code>len</code> bytes of data from the input stream into * an array of bytes, blocks until at least one byte is available. * Sets the <code>eof</code> field of the connection when the native read * returns -1. * * @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. */ protected int nonBufferedRead(byte b[], int off, int len) throws IOException { int bytesRead; for (;;) { try { /* * Multiple threads blocked on read operation may * return results interleaved arbitrarily. From an * application perspective, the results would be * indeterministic. So "reader locks" are introduced * for "read" operation from the same handle. */ synchronized (readerLock) { bytesRead = read0(b, off, len); } } finally { if (iStreams == 0) { throw new InterruptedIOException("Stream closed"); } } if (bytesRead == -1) { eof = true; return -1; } if (bytesRead != 0) { return bytesRead; } } } /** * Returns the number of bytes that can be read (or skipped over) from * this input stream without blocking by the next caller of a method for * this input stream. The next caller might be the same thread or * another thread. * * @return the number of bytes that can be read from this input stream * without blocking. * @exception IOException if an I/O error occurs. */ public int available() throws IOException { if (count > 0) { /* * The next read will only return the bytes in the buffer, * so only return the number of bytes in the buffer. * While available can return a number less than than the next * read will get, it should not return more. */ return count; } // 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 { /* * Multiple threads blocked on write operation may return results * interleaved arbitrarily. From an application perspective, the * results would be indeterministic. So "writer locks" are * introduced for "write" operation to the same socket. */ synchronized (writerLock) { 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("Illegal Socket Option Value"); } 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 reassignment.</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); } /** * Perform actions required on system resume. Makes nothing. * @throws StateTransitionException */ public void resume() throws StateTransitionException {} /** * Disconnects connection within system suspend procedure. */ public void suspend() { try { disconnect(); } catch (IOException e) { // ignoring to proceed system pause } } /** * Opens a TCP connection to a server. * * @param szIpBytes raw IPv4 address of host * @param port TCP port at host * * @exception IOException if an I/O error occurs. */ private native void open0(byte[] szIpBytes, int port) throws IOException; /** * Reads from the open socket connection. * * @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; /** * Writes to the open socket connection. * * @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; /** * Gets the number of bytes that can be read without blocking. * * @return number of bytes that can be read without blocking * @exception IOException if an I/O error occurs. */ private native int available0() throws IOException; /** * Closes the socket connection. * * @exception IOException if an I/O error occurs when closing the * connection. */ private native void close0() throws IOException; /** * Native finalizer */ private native void finalize(); /** * Gets a byte array that represents an IPv4 or IPv6 address * * @param sHost the hostname to lookup * @param ipBytes_out Output array that receives the * bytes of IP address * @return number of bytes copied to ipBytes_out or -1 for an error */ private native int getIpNumber0(String sHost, byte[] ipBytes_out); /** * Gets the requested IP number. * * @param local <tt>true</tt> to get the local host IP address, or * <tt>false</tt> to get the remote host IP address * @return the IP address as a dotted-quad <tt>String</tt> * @exception IOException if an I/O error occurs. */ private native String getHost0(boolean local) throws IOException; /** * Gets the requested port number. * * @param local <tt>true</tt> to get the local port number, or * <tt>false</tt> to get the remote port number * @return the port number * @exception IOException if an I/O error occurs. */ private native int getPort0(boolean local) throws IOException; /** * Gets the requested socket option. * * @param option socket option to retrieve * @return value of the socket option * @exception IOException if an I/O error occurs. */ private native int getSockOpt0(int option) throws IOException; /** * Sets the requested socket option. * * @param option socket option to set * @param value the value to set <tt>option</tt> to * @exception IOException if an I/O error occurs. */ private native void setSockOpt0(int option, int value) throws IOException; /** * Shuts down the output side of the connection. Any error that might * result from this operation is ignored. */ private native void shutdownOutput0();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -