📄 connectionbaseadapter.java
字号:
/* * @(#)ConnectionBaseAdapter.java 1.25 02/10/14 @(#) * * Copyright (c) 2000-2002 Sun Microsystems, Inc. All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */package com.sun.midp.io;import com.sun.cldc.io.ConnectionBaseInterface;import com.sun.cldc.io.GeneralBase;import com.sun.midp.midlet.*;import com.sun.midp.security.*;import java.io.*;import javax.microedition.io.*;/** * Protocol classes extend this class to gain some of the common functionality * needed to implement a CLDC Generic Connection. * <p> * The common functionality includes:</p> * <ul> * <li>Supplies the input and output stream classes for a StreamConnection</li> * <li>Limits the number of streams opened according to mode, but the limit * can be overridden. Read-write allows 1 input and 1 output, write-only * allows 1 output, read-only allows 1 input</li> * <li>Only "disconnects" when the connection and all streams are closed</li> * <li>Throws I/O exceptions when used after being closed</li> * <li>Provides a more efficient implementation of * {@link InputStream#read(byte[], int, int)}, which is called by * {@link InputStream#read()} * <li>Provides a more efficient implementation of * {@link OutputStream#write(byte[], int, int)}, which is called by * {@link OutputStream#write(int)} * </ul> * <p align="center"> * <b>Class Relationship Diagram</b></p> * <p align="center"> * <img src="doc-files/ConnectionBaseAdapter.gif" border=0></p> * * @author Stephen Flores * @version 3.0 9/1/2000 */public abstract class ConnectionBaseAdapter implements ConnectionBaseInterface, StreamConnection { /** Flag indicating if the connection is open. */ protected boolean connectionOpen = false; /** Number of input streams that were opened. */ protected int iStreams = 0; /** * Maximum number of open input streams. Set this * to zero to prevent openInputStream from giving out a stream in * write-only mode. */ protected int maxIStreams = 1; /** Number of output streams were opened. */ protected int oStreams = 0; /** * Maximum number of output streams. Set this * to zero to prevent openOutputStream from giving out a stream in * read-only mode. */ protected int maxOStreams = 1; /** The security permission required by a subclass. -1 for none. */ protected int requiredPermission = -1; /** Protocol name of subclass to prefix name of resource, can be null. */ protected String protocol; /** The subclass needs know that the permission occured. */ private boolean permissionChecked; /** * Lets a subclass know that the security check was done. * Called by the connect method of subclasses that require permission. * <p> * This method is needed since the security check must be put off * until connection since the name of the resource must be * presented to the user in the permission question. * An attacker could directly construct a in a subclass of the * protocol and then call its connect method. A subclass can detect * this attack by calling this method. * * @exception SecurityException if the check did not happen. */ protected void verifyPermissionCheck() { if (permissionChecked) { return; } throw new SecurityException("The permission check was bypassed"); } /** * Check for required permission and open a connection to a target. * * @param name URL for the connection, without the * without the protocol part * @param mode I/O access mode, see {@link Connector} * @param timeouts flag to indicate that the caller * wants timeout exceptions * @return this Connection object * * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException If the connection cannot * be found. * @exception IOException If some other kind of I/O error occurs. */ public Connection openPrim(String name, int mode, boolean timeouts) throws IOException { checkForPermission(name); switch (mode) { case Connector.READ: case Connector.WRITE: case Connector.READ_WRITE: break; default: throw new IllegalArgumentException("Illegal mode"); } connect(name, mode, timeouts); connectionOpen = true; return this; } /** * Check for the required permission, if needed. * * @param name name of resource to insert into the permission question * * @exception IOInterruptedException if another thread interrupts the * calling thread while this method is waiting to preempt the * display. */ public void checkForPermission(String name) throws InterruptedIOException { Scheduler scheduler; MIDletSuite midletSuite; if (permissionChecked) { return; } permissionChecked = true; if (requiredPermission == -1) { return; } scheduler = Scheduler.getScheduler(); midletSuite = scheduler.getMIDletSuite(); // there is no suite running when installing from the command line if (midletSuite != null) { if (protocol != null) { name = protocol + ":" + name; } try { midletSuite.checkForPermission(requiredPermission, name); } catch (InterruptedException ie) { throw new InterruptedIOException( "Interrupted while trying to ask the user permission"); } } } /** * Check for the required permission and open a connection to a target. * This method can be used with permissions greater than * the current MIDlet suite. * * @param token security token of the calling class * @param name URL for the connection, without the * without the protocol part * @param mode I/O access mode, see {@link Connector} * @param timeouts flag to indicate that the caller * wants timeout exceptions * @return this Connection object * * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException If the connection cannot * be found. * @exception IOException If some other kind of I/O error occurs. */ public Connection openPrim(SecurityToken token, String name, int mode, boolean timeouts) throws IOException { if (requiredPermission != -1) { token.checkIfPermissionAllowed(requiredPermission); } permissionChecked = true; return openPrim(name, mode, timeouts); } /** * Check for required permission and open a connection to a target. * This method can be used with permissions greater than * the current MIDlet suite. Assume read/write and no timeouts. * * @param token security token of the calling class * @param name URL for the connection, without the * without the protocol part * @return this Connection object * * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException If the connection cannot * be found. * @exception IOException If some other kind of I/O error occurs. */ public Connection openPrim(SecurityToken token, String name) throws IOException { return openPrim(token, name, Connector.READ_WRITE, false); } /** * Returns an input stream. * * @return an input stream for writing bytes to this port. * @exception IOException if an I/O error occurs when creating the * output stream. */ public InputStream openInputStream() throws IOException { InputStream i; ensureOpen(); if (maxIStreams == 0) { throw new IOException("no more input streams available"); } i = new BaseInputStream(this); maxIStreams--; iStreams++; return i; } /** * Open and return a data input stream for a connection. * * @return An input stream * @exception IOException If an I/O error occurs */ public DataInputStream openDataInputStream() throws IOException { return new DataInputStream(openInputStream()); } /** * Returns an output stream. * * @return an output stream for writing bytes to this port. * @exception IOException if an I/O error occurs when creating the * output stream. */ public OutputStream openOutputStream() throws IOException { OutputStream o; ensureOpen(); if (maxOStreams == 0) { throw new IOException("no more output streams available"); } o = new BaseOutputStream(this); maxOStreams--; oStreams++; return o; } /** * Open and return a data output stream for a connection. * * @return An input stream * @exception IOException If an I/O error occurs */ public DataOutputStream openDataOutputStream() throws IOException { return new DataOutputStream(openOutputStream()); } /** * Close the connection. * * @exception IOException if an I/O error occurs when closing the * connection. */ public void close() throws IOException { if (connectionOpen) { connectionOpen = false; closeCommon(); } } /** * Called once by each child input stream. * If the input stream is marked open, it will be marked closed and * the if the connection and output stream are closed the disconnect * method will be called. * * @exception IOException if the subclass throws one */ protected void closeInputStream() throws IOException { iStreams--; closeCommon(); } /** * Called once by each child output stream. * If the output stream is marked open, it will be marked closed and * the if the connection and input stream are closed the disconnect * method will be called. * * @exception IOException if the subclass throws one */ protected void closeOutputStream() throws IOException { oStreams--; closeCommon(); } /** * Disconnect if the connection and all the streams and the closed. * * @exception IOException if an I/O error occurs when closing the * connection. */ void closeCommon() throws IOException { if (!connectionOpen && iStreams == 0 && oStreams == 0) { disconnect(); } } /** * Check if the connection is open. * * @exception IOException is thrown, if the stream is not open. */ protected void ensureOpen() throws IOException { if (!connectionOpen) { throw new IOException("Connection closed"); } } /** * Connect to a target. * * @param name URL for the connection, without the protocol * part * @param mode I/O access mode, see {@link Connector} * @param timeouts flag to indicate that the called wants * timeout exceptions * * @exception IllegalArgumentException If a parameter is invalid. * @exception ConnectionNotFoundException If the connection cannot be * found. * @exception IOException If some other kind of I/O error occurs. */ protected abstract void connect(String name, int mode, boolean timeouts) throws IOException; /** * Free up the connection resources. * * @exception IOException if an I/O error occurs. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -