📄 protocol.java
字号:
/* * Copyright (c) 1999-2001 Sun Microsystems, Inc., 901 San Antonio Road, * Palo Alto, CA 94303, U.S.A. All Rights Reserved. * * Sun Microsystems, Inc. has intellectual property rights relating * to the technology embodied in this software. In particular, and * without limitation, these intellectual property rights may include * one or more U.S. patents, foreign patents, or pending * applications. Sun, Sun Microsystems, the Sun logo, Java, KJava, * and all Sun-based and Java-based marks are trademarks or * registered trademarks of Sun Microsystems, Inc. in the United * States and other countries. * * This software is distributed under licenses restricting its use, * copying, distribution, and decompilation. No part of this * software may be reproduced in any form by any means without prior * written authorization of Sun and its licensors, if any. * * FEDERAL ACQUISITIONS: Commercial Software -- Government Users * Subject to Standard License Terms and Conditions */package com.sun.cldc.io.j2me.socket;import java.io.*;import javax.microedition.io.*;import com.sun.cldc.io.*;/** * Connection to the J2ME socket API. * * @author Nik Shaylor * @author Efren Serra * @version 1.0 1/16/2000 */public class Protocol extends NetworkConnectionBase implements StreamConnection { /**********************************************************\ * WARNING - 'handle' MUST be the first instance variable * * It is used by native code that assumes this. * \**********************************************************/ /** Socket object used by native code */ int handle; /** Private variable the native code uses */ int iocb; /** Access mode */ private int mode; /** Open count */ int opens = 0; /** Connection open flag */ private boolean copen = false; /** Input stream open flag */ protected boolean isopen = false; /** Output stream open flag */ protected boolean osopen = false; /** Size of the read ahead / write behind buffers */ public static int bufferSize = 0; /* Default is no buffering */ /** * Class initializer */ static { /* See if a read ahead / write behind buffer size has been specified */ String size = System.getProperty("com.sun.cldc.io.j2me.socket.buffersize"); if (size != null) { try { bufferSize = Integer.parseInt(size); } catch(NumberFormatException ex) {} } } /** * Open the connection */ public void open(String name, int mode, boolean timeouts) throws IOException { throw new RuntimeException("Should not be called"); } /** * Open the connection * @param name the target for the connection * @param writeable a flag that is true if the caller expects * to write to the connection * @param timeouts a flag to indicate that the caller wants * timeout exceptions * <p> * The name string for this protocol should be: * "socket://<name or IP number>:<port number> */ public Connection openPrim(String name, int mode, boolean timeouts) throws IOException { try { open0(name, mode, timeouts); registerCleanup(); opens++; copen = true; this.mode = mode; return this; } catch(InterruptedException x) { return null; } } /** * Open the connection * * @param handle an already formed socket handle * <p> * This function is only used by com.sun.cldc.io.j2me.socketserver; */ public void open(int handle, int mode) throws IOException { this.handle = handle; opens++; copen = true; this.mode = mode; } /** * Ensure connection is open */ void ensureOpen() throws IOException { if (!copen) { throw new IOException("Connection closed"); } } /** * Returns an input stream for this socket. * * @return an input stream for reading bytes from this socket. * @exception IOException if an I/O error occurs when creating the * input stream. */ synchronized public InputStream openInputStream() throws IOException { ensureOpen(); if ((mode&Connector.READ) == 0) { throw new IOException("Connection not open for reading"); } if (isopen) { throw new IOException("Input stream already opened"); } isopen = true; InputStream in; if (bufferSize == 0) { in = new PrivateInputStream(this); } else { in = new PrivateInputStreamWithBuffer(this, bufferSize); } opens++; return in; } /** * Returns an output stream for this socket. * * @return an output stream for writing bytes to this socket. * @exception IOException if an I/O error occurs when creating the * output stream. */ synchronized public OutputStream openOutputStream() throws IOException { ensureOpen(); if ((mode&Connector.WRITE) == 0) { throw new IOException("Connection not open for writing"); } if (osopen) { throw new IOException("Output stream already opened"); } osopen = true; OutputStream os; os = new PrivateOutputStream(this); opens++; return os; } /** * Close the connection. * * @exception IOException if an I/O error occurs when closing the * connection. */ synchronized public void close() throws IOException { if (copen) { copen = false; realClose(); } } /** * Close the connection. * * @exception IOException if an I/O error occurs. */ synchronized void realClose() throws IOException { if (--opens == 0) { close0(); } } /* * 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. */ protected native void open0(String name, int mode, boolean timeouts) throws IOException, InterruptedException; protected native int read0(byte b[], int off, int len) throws IOException; protected native int write0(byte b[], int off, int len) throws IOException; protected native int available0() throws IOException; protected native void close0() throws IOException; private native void registerCleanup();}/** * Input stream for the connection */class PrivateInputStream extends InputStream { /** * The reference to the protocol. */ protected Protocol parent; /** * The end of file flag. */ boolean eof = false; /** * Single byte buffer. */ byte[] bytebuf; /** * Constructor * @param pointer to the connection object * * @exception IOException if an I/O error occurs. */ public PrivateInputStream(Protocol parent) throws IOException { this.parent = parent; } /** * Check the stream is open * * @exception IOException if the stream is not open. */ void ensureOpen() throws IOException { if (parent == null) { throw new IOException("Stream closed"); } } /** * Reads the next byte of data from the input stream. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if an I/O error occurs. */ synchronized public int read() throws IOException { if (bytebuf == null) { bytebuf = new byte[1]; } int res = read(bytebuf, 0, 1); if (res == 1) { return bytebuf[0] & 0xFF; } else { return res; } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -