📄 connectionbaseadapter.java
字号:
/*
* Created on 2005-8-19 by pcy
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package a.a.a.midp.io;
import java.io.*;
import javax.microedition.io.Connection;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.*;
/**
* @author pcy
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
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;
/*protected void verifyPermissionCheck() {
if (permissionChecked) {
return;
}
throw new SecurityException("The permission check was bypassed");
}*/
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;
}
public void checkForPermission(String name) throws InterruptedIOException {
}
/*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);
}
public Connection openPrim(SecurityToken token, String name)
throws IOException {
return openPrim(token, name, Connector.READ_WRITE, false);
}*/
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;
}
public DataInputStream openDataInputStream() throws IOException {
return new DataInputStream(openInputStream());
}
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;
}
public DataOutputStream openDataOutputStream() throws IOException {
return new DataOutputStream(openOutputStream());
}
public void close() throws IOException {
if (connectionOpen) {
connectionOpen = false;
closeCommon();
}
}
protected void closeInputStream() throws IOException {
iStreams--;
closeCommon();
}
protected void closeOutputStream() throws IOException {
oStreams--;
closeCommon();
}
void closeCommon() throws IOException {
if (!connectionOpen && iStreams == 0 && oStreams == 0) {
disconnect();
}
}
protected void ensureOpen() throws IOException {
if (!connectionOpen) {
throw new IOException("Connection closed");
}
}
protected abstract void connect(String name, int mode, boolean timeouts)
throws IOException;
protected abstract void disconnect() throws IOException;
protected abstract int readBytes(byte b[], int off, int len)
throws IOException;
public int available() throws IOException {
return 0;
}
protected abstract int writeBytes(byte b[], int off, int len)
throws IOException;
protected void flush() throws IOException {
}
}
/**
* Input stream for the connection
*/
class BaseInputStream extends InputStream {
/** Pointer to the connection */
private ConnectionBaseAdapter parent;
/** Buffer for single char reads */
byte[] buf = new byte[1];
BaseInputStream(ConnectionBaseAdapter parent) throws IOException {
this.parent = parent;
}
private void ensureOpen() throws InterruptedIOException {
if (parent == null) {
throw new InterruptedIOException("Stream closed");
}
}
public int available() throws IOException {
ensureOpen();
return parent.available();
}
public int read() throws IOException {
if (read(buf, 0, 1) > 0) {
return (buf[0] & 0xFF);
}
return -1;
}
public int read(byte b[], int off, int len) throws IOException {
//int test;
ensureOpen();
if (len == 0) {
return 0;
}
/*
* test the parameters so the subclass will not have to.
* this will avoid crashes in the native code
*/
//test = b[off] + b[len - 1] + b[off + len - 1];
return parent.readBytes(b, off, len);
}
public void close() throws IOException {
if (parent != null) {
parent.closeInputStream();
parent = null;
}
}
}
/**
* Output stream for the connection
*/
class BaseOutputStream extends OutputStream {
/** Pointer to the connection */
ConnectionBaseAdapter parent;
/** Buffer for single char writes */
byte[] buf = new byte[1];
BaseOutputStream(ConnectionBaseAdapter p) {
parent = p;
}
private void ensureOpen() throws InterruptedIOException {
if (parent == null) {
throw new InterruptedIOException("Stream closed");
}
}
public void write(int b) throws IOException {
buf[0] = (byte)b;
write(buf, 0, 1);
}
public void write(byte b[], int off, int len)
throws IOException {
//int test;
int bytesWritten;
ensureOpen();
if (len == 0) {
return;
}
/*
* test the parameters here so subclases do not have to,
* this will avoid a crash in the native code
*/
//test = b[off] + b[len - 1] + b[off + len - 1];
/*
* Polling the native code is done here to allow for simple
* asynchronous native code to be written. Not all implementations
* work this way (they block in the native code) but the same
* Java code works for both.
*/
for (bytesWritten = 0; ; ) {
try {
bytesWritten += parent.writeBytes(b, off + bytesWritten,
len - bytesWritten);
} finally {
if (parent == null) {
throw new InterruptedIOException("Stream closed");
}
}
if (bytesWritten == len) {
break;
}
//GeneralBase.iowait();
}
}
public void flush() throws IOException {
ensureOpen();
parent.flush();
}
public void close() throws IOException {
if (parent != null) {
//pcy: added to flush the buf
flush();
//pcy: finish add
parent.closeOutputStream();
parent = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -