⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workaroundstreamconnection.java

📁 用Java开发手机蓝牙程序。要先安装J2ME
💻 JAVA
字号:
package example.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.StreamConnection;

/**
 * A workaround class for S60 3rd edition and S80 devices. This class will slow
 * down the stream.
 * 
 */
public class WorkaroundStreamConnection extends Object implements
		StreamConnection {

	private static final int SLOW_DOWN = 150; // ms

	private StreamConnection conn;

	public WorkaroundStreamConnection(StreamConnection stream) {
		this.conn = stream;
	}

	public InputStream openInputStream() throws IOException {
		return new InputStream() {
			InputStream in = conn.openInputStream();

			public synchronized int read() throws IOException {
				int ret = -1;
				while (true) {
					ret = in.read();
					if (ret < 0) {
						try {
							Thread.sleep(SLOW_DOWN);
						} catch (InterruptedException e) {
						}
						continue;
					}
					break;
				}
				try {
					Thread.sleep(SLOW_DOWN);
				} catch (InterruptedException e) {
				}
				return ret;
			}
		};
	}

	public DataInputStream openDataInputStream() throws IOException {
		return new DataInputStream(openInputStream());
	}

	public void close() throws IOException {
		conn.close();
	}

	public OutputStream openOutputStream() throws IOException {
		return new OutputStream() {
			OutputStream out = conn.openOutputStream();

			public synchronized void write(int value) throws IOException {
				out.write(value);
				out.flush();
				try {
					Thread.sleep(SLOW_DOWN);
				} catch (InterruptedException e) {
				}
			}
			
			public void flush() throws IOException {
			}
		};
	}

	public DataOutputStream openDataOutputStream() throws IOException {
		return new DataOutputStream(openOutputStream());
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -