在Java Socket中处理大数据流时,需要考虑到内存和性能的问题
BufferedInputStream
和BufferedOutputStream
来实现缓冲。import java.io.*;
import java.net.*;
public class BigDataSocketServer {
public static void main(String[] args) throws IOException {
int port = 12345;
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = new BufferedInputStream(socket.getInputStream());
OutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());
// 处理数据流
handleDataStream(inputStream, outputStream);
inputStream.close();
outputStream.close();
socket.close();
}
}
private static void handleDataStream(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
// 处理数据
outputStream.write(buffer, 0, bytesRead);
}
}
}
private static void handleDataStream(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
totalBytesRead += bytesRead;
// 处理数据
outputStream.write(buffer, 0, bytesRead);
if (totalBytesRead >= CHUNK_SIZE) {
// 发送或接收下一个数据块
totalBytesRead = 0;
}
}
}
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class BigDataSocketServer {
public static void main(String[] args) throws IOException {
int port = 12345;
Selector selector = Selector.open();
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(port));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select();
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
if (key.isAcceptable()) {
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = socketChannel.read(buffer);
if (bytesRead == -1) {
socketChannel.close();
} else {
buffer.flip();
// 处理数据
buffer.clear();
}
}
}
}
}
}
通过以上方法,可以有效地在Java Socket中处理大数据流,同时避免内存和性能问题。