client.java

来自「基于java nio实现的http服务器」· Java 代码 · 共 43 行

JAVA
43
字号
package org.sse.server;

import java.io.*;
import java.nio.channels.SelectionKey;

/**
 * @author Taylor Cowan
 */
public class Client {

	public SelectionKey key;
	public PipedInputStream clientInputStream;
	private OutputStream clientOut;
	private Queue myQueue;
	private boolean requestReady = false;

	public Client(SelectionKey readKey, Queue q) throws IOException {
		key = readKey;
		myQueue = q;
		initializePipe();
	}

	private void initializePipe() throws IOException {
		PipedOutputStream pos = new PipedOutputStream();
		clientInputStream = new PipedInputStream(pos);
		clientOut = new BufferedOutputStream(pos, 1048);
	}

	public synchronized void write(byte[] data) throws IOException {
		clientOut.write(data);
		clientOut.flush();
		if (!requestReady) {
			requestReady = true;
			myQueue.insert(this);
		}
	}

	public synchronized boolean notifyRequestDone() throws IOException {
		if (clientInputStream.available() <= 0)
			requestReady = false;
		return !requestReady;
	}
}

⌨️ 快捷键说明

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