📁🌐 I/O操作优化:让程序"读写"更流畅

41 阅读13分钟

"I/O优化就像优化交通系统,用对了方法,数据传输如行云流水!" 🚗💨

🎯 什么是I/O操作优化?

想象一下,你是一个超级忙碌的快递员 📦。每天都要收发很多包裹,如果你不善于规划路线和选择交通工具,那效率太低了!

I/O操作优化就像是学会最聪明的快递配送方法,让数据读写更高效,程序运行更流畅!

🏃‍♂️ 核心思想:用优化换I/O效率,用技术换性能

未优化:同步I/O → 阻塞等待 → 效率低下
已优化:异步I/O → 非阻塞处理 → 效率提升

效率提升:5-10倍! 🎉

🎨 I/O操作优化的四种策略

1. 文件I/O优化 - 让文件"读写"更快速 📁

生活比喻: 就像整理文件柜,用对了方法,找文件更快,放文件更有序!

@Service
public class FileIOOptimizationService {
    
    // 缓冲机制
    public static class BufferOptimization {
        
        // 缓冲区大小调优
        public void optimizedFileRead(String filePath) throws IOException {
            // 未优化:小缓冲区
            try (FileInputStream fis = new FileInputStream(filePath)) {
                byte[] smallBuffer = new byte[1024]; // 1KB缓冲区
                int bytesRead;
                while ((bytesRead = fis.read(smallBuffer)) != -1) {
                    processData(smallBuffer, bytesRead);
                }
            }
            
            // 优化:大缓冲区
            try (BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(filePath), 8192)) { // 8KB缓冲区
                byte[] largeBuffer = new byte[8192];
                int bytesRead;
                while ((bytesRead = bis.read(largeBuffer)) != -1) {
                    processData(largeBuffer, bytesRead);
                }
            }
        }
        
        public void optimizedFileWrite(String filePath, byte[] data) throws IOException {
            // 未优化:直接写入
            try (FileOutputStream fos = new FileOutputStream(filePath)) {
                fos.write(data);
            }
            
            // 优化:缓冲写入
            try (BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(filePath), 8192)) {
                bos.write(data);
                bos.flush(); // 确保数据写入
            }
        }
        
        private void processData(byte[] data, int length) {
            // 处理数据
        }
        
        // 自适应缓冲区大小
        public void adaptiveBufferSize(String filePath) throws IOException {
            File file = new File(filePath);
            long fileSize = file.length();
            
            int bufferSize;
            if (fileSize < 1024 * 1024) { // 小于1MB
                bufferSize = 4096; // 4KB
            } else if (fileSize < 10 * 1024 * 1024) { // 小于10MB
                bufferSize = 16384; // 16KB
            } else { // 大于10MB
                bufferSize = 65536; // 64KB
            }
            
            try (BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(filePath), bufferSize)) {
                byte[] buffer = new byte[bufferSize];
                int bytesRead;
                while ((bytesRead = bis.read(buffer)) != -1) {
                    processData(buffer, bytesRead);
                }
            }
        }
    }
    
    // 直接内存使用
    public static class DirectMemoryOptimization {
        
        public void useDirectMemory(String filePath) throws IOException {
            try (FileChannel channel = FileChannel.open(Paths.get(filePath), 
                    StandardOpenOption.READ)) {
                
                long fileSize = channel.size();
                ByteBuffer directBuffer = ByteBuffer.allocateDirect((int) fileSize);
                
                // 直接内存读取
                channel.read(directBuffer);
                directBuffer.flip();
                
                // 处理数据
                processDirectBuffer(directBuffer);
            }
        }
        
        private void processDirectBuffer(ByteBuffer buffer) {
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                // 处理字节
            }
        }
        
        // 内存映射文件
        public void useMemoryMappedFile(String filePath) throws IOException {
            try (RandomAccessFile file = new RandomAccessFile(filePath, "r");
                 FileChannel channel = file.getChannel()) {
                
                MappedByteBuffer mappedBuffer = channel.map(
                    FileChannel.MapMode.READ_ONLY, 0, channel.size());
                
                // 直接访问映射的内存
                processMappedBuffer(mappedBuffer);
            }
        }
        
        private void processMappedBuffer(MappedByteBuffer buffer) {
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                // 处理字节
            }
        }
    }
    
    // 零拷贝技术
    public static class ZeroCopyOptimization {
        
        public void zeroCopyTransfer(String sourcePath, String targetPath) throws IOException {
            try (FileChannel sourceChannel = FileChannel.open(Paths.get(sourcePath), 
                    StandardOpenOption.READ);
                 FileChannel targetChannel = FileChannel.open(Paths.get(targetPath), 
                    StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
                
                // 零拷贝传输
                long transferred = 0;
                long size = sourceChannel.size();
                
                while (transferred < size) {
                    long count = sourceChannel.transferTo(transferred, size - transferred, targetChannel);
                    transferred += count;
                }
            }
        }
        
        // 文件到网络零拷贝
        public void zeroCopyToNetwork(String filePath, SocketChannel socketChannel) throws IOException {
            try (FileChannel fileChannel = FileChannel.open(Paths.get(filePath), 
                    StandardOpenOption.READ)) {
                
                long transferred = 0;
                long size = fileChannel.size();
                
                while (transferred < size) {
                    long count = fileChannel.transferTo(transferred, size - transferred, socketChannel);
                    transferred += count;
                }
            }
        }
    }
    
    // 内存映射文件
    public static class MemoryMappedFileOptimization {
        
        public void processLargeFile(String filePath) throws IOException {
            try (RandomAccessFile file = new RandomAccessFile(filePath, "r");
                 FileChannel channel = file.getChannel()) {
                
                long fileSize = channel.size();
                long position = 0;
                long chunkSize = 1024 * 1024; // 1MB chunks
                
                while (position < fileSize) {
                    long currentChunkSize = Math.min(chunkSize, fileSize - position);
                    
                    MappedByteBuffer buffer = channel.map(
                        FileChannel.MapMode.READ_ONLY, position, currentChunkSize);
                    
                    processChunk(buffer);
                    position += currentChunkSize;
                }
            }
        }
        
        private void processChunk(MappedByteBuffer buffer) {
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                // 处理字节
            }
        }
        
        // 读写内存映射文件
        public void readWriteMappedFile(String filePath) throws IOException {
            try (RandomAccessFile file = new RandomAccessFile(filePath, "rw");
                 FileChannel channel = file.getChannel()) {
                
                MappedByteBuffer buffer = channel.map(
                    FileChannel.MapMode.READ_WRITE, 0, channel.size());
                
                // 读取数据
                while (buffer.hasRemaining()) {
                    byte b = buffer.get();
                    // 处理读取的数据
                }
                
                // 写入数据
                buffer.position(0);
                buffer.put("Hello World".getBytes());
                buffer.force(); // 强制写入磁盘
            }
        }
    }
}

2. 异步I/O - 让I/O"非阻塞"更高效 ⚡

生活比喻: 就像点外卖,不用等外卖到了才做其他事,可以边等边做其他事情!

@Service
public class AsyncIOOptimizationService {
    
    // NIO非阻塞I/O
    public static class NIONonBlockingIO {
        
        public void nonBlockingFileRead(String filePath) throws IOException {
            try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(
                    Paths.get(filePath), StandardOpenOption.READ)) {
                
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                
                // 异步读取
                Future<Integer> result = channel.read(buffer, 0);
                
                // 可以做其他事情
                doOtherWork();
                
                // 获取结果
                Integer bytesRead = result.get();
                buffer.flip();
                
                processBuffer(buffer);
            }
        }
        
        private void doOtherWork() {
            // 做其他工作
        }
        
        private void processBuffer(ByteBuffer buffer) {
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                // 处理字节
            }
        }
        
        // 异步文件写入
        public void nonBlockingFileWrite(String filePath, byte[] data) throws IOException {
            try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(
                    Paths.get(filePath), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
                
                ByteBuffer buffer = ByteBuffer.wrap(data);
                
                // 异步写入
                Future<Integer> result = channel.write(buffer, 0);
                
                // 可以做其他事情
                doOtherWork();
                
                // 获取结果
                Integer bytesWritten = result.get();
                log.info("写入字节数: {}", bytesWritten);
            }
        }
    }
    
    // AIO异步I/O
    public static class AIOAsyncIO {
        
        public void aioFileRead(String filePath) throws IOException {
            try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(
                    Paths.get(filePath), StandardOpenOption.READ)) {
                
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                
                // 使用CompletionHandler异步读取
                channel.read(buffer, 0, null, new CompletionHandler<Integer, Void>() {
                    @Override
                    public void completed(Integer result, Void attachment) {
                        log.info("异步读取完成,读取字节数: {}", result);
                        buffer.flip();
                        processBuffer(buffer);
                    }
                    
                    @Override
                    public void failed(Throwable exc, Void attachment) {
                        log.error("异步读取失败", exc);
                    }
                });
                
                // 可以做其他事情
                doOtherWork();
            }
        }
        
        private void processBuffer(ByteBuffer buffer) {
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                // 处理字节
            }
        }
        
        private void doOtherWork() {
            // 做其他工作
        }
        
        // 异步文件写入
        public void aioFileWrite(String filePath, byte[] data) throws IOException {
            try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(
                    Paths.get(filePath), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
                
                ByteBuffer buffer = ByteBuffer.wrap(data);
                
                // 使用CompletionHandler异步写入
                channel.write(buffer, 0, null, new CompletionHandler<Integer, Void>() {
                    @Override
                    public void completed(Integer result, Void attachment) {
                        log.info("异步写入完成,写入字节数: {}", result);
                    }
                    
                    @Override
                    public void failed(Throwable exc, Void attachment) {
                        log.error("异步写入失败", exc);
                    }
                });
                
                // 可以做其他事情
                doOtherWork();
            }
        }
    }
    
    // 事件驱动模型
    public static class EventDrivenModel {
        
        public void eventDrivenFileProcessing(String filePath) throws IOException {
            try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(
                    Paths.get(filePath), StandardOpenOption.READ)) {
                
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                long position = 0;
                
                // 事件驱动的文件处理
                processFileChunk(channel, buffer, position);
            }
        }
        
        private void processFileChunk(AsynchronousFileChannel channel, ByteBuffer buffer, long position) {
            channel.read(buffer, position, null, new CompletionHandler<Integer, Void>() {
                @Override
                public void completed(Integer result, Void attachment) {
                    if (result > 0) {
                        buffer.flip();
                        processBuffer(buffer);
                        buffer.clear();
                        
                        // 继续处理下一块
                        processFileChunk(channel, buffer, position + result);
                    } else {
                        log.info("文件处理完成");
                    }
                }
                
                @Override
                public void failed(Throwable exc, Void attachment) {
                    log.error("文件处理失败", exc);
                }
            });
        }
        
        private void processBuffer(ByteBuffer buffer) {
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                // 处理字节
            }
        }
    }
    
    // Reactor模式
    public static class ReactorPattern {
        private final Selector selector;
        private final ExecutorService executor;
        
        public ReactorPattern() throws IOException {
            this.selector = Selector.open();
            this.executor = Executors.newFixedThreadPool(4);
        }
        
        public void startReactor() {
            executor.submit(new Reactor());
        }
        
        private class Reactor implements Runnable {
            @Override
            public void run() {
                while (true) {
                    try {
                        int readyChannels = selector.select();
                        
                        if (readyChannels == 0) {
                            continue;
                        }
                        
                        Set<SelectionKey> selectedKeys = selector.selectedKeys();
                        Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
                        
                        while (keyIterator.hasNext()) {
                            SelectionKey key = keyIterator.next();
                            keyIterator.remove();
                            
                            if (key.isAcceptable()) {
                                handleAccept(key);
                            } else if (key.isReadable()) {
                                handleRead(key);
                            } else if (key.isWritable()) {
                                handleWrite(key);
                            }
                        }
                    } catch (IOException e) {
                        log.error("Reactor异常", e);
                    }
                }
            }
            
            private void handleAccept(SelectionKey key) {
                // 处理连接接受
            }
            
            private void handleRead(SelectionKey key) {
                // 处理读取
            }
            
            private void handleWrite(SelectionKey key) {
                // 处理写入
            }
        }
        
        public void shutdown() {
            executor.shutdown();
        }
    }
}

3. 批量处理 - 让I/O"批量"更高效 📦

生活比喻: 就像批量处理邮件,一次处理多封比一封一封处理效率高多了!

@Service
public class BatchProcessingOptimizationService {
    
    // 批量读写
    public static class BatchReadWrite {
        
        public void batchFileRead(String filePath) throws IOException {
            try (BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(filePath), 8192)) {
                
                byte[] buffer = new byte[8192];
                int bytesRead;
                
                while ((bytesRead = bis.read(buffer)) != -1) {
                    // 批量处理数据
                    processBatchData(buffer, bytesRead);
                }
            }
        }
        
        public void batchFileWrite(String filePath, List<byte[]> dataList) throws IOException {
            try (BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(filePath), 8192)) {
                
                // 批量写入
                for (byte[] data : dataList) {
                    bos.write(data);
                }
                bos.flush();
            }
        }
        
        private void processBatchData(byte[] data, int length) {
            // 批量处理数据
        }
        
        // 批量文件操作
        public void batchFileOperations(List<String> filePaths) {
            filePaths.parallelStream().forEach(filePath -> {
                try {
                    processFile(filePath);
                } catch (IOException e) {
                    log.error("处理文件失败: {}", filePath, e);
                }
            });
        }
        
        private void processFile(String filePath) throws IOException {
            // 处理单个文件
        }
    }
    
    // 批量操作
    public static class BatchOperations {
        
        public void batchDatabaseOperations(List<DataRecord> records) {
            // 批量插入
            batchInsert(records);
            
            // 批量更新
            batchUpdate(records);
            
            // 批量删除
            batchDelete(records);
        }
        
        private void batchInsert(List<DataRecord> records) {
            // 批量插入逻辑
        }
        
        private void batchUpdate(List<DataRecord> records) {
            // 批量更新逻辑
        }
        
        private void batchDelete(List<DataRecord> records) {
            // 批量删除逻辑
        }
        
        // 批量网络操作
        public void batchNetworkOperations(List<NetworkRequest> requests) {
            requests.parallelStream().forEach(request -> {
                try {
                    processNetworkRequest(request);
                } catch (Exception e) {
                    log.error("处理网络请求失败", e);
                }
            });
        }
        
        private void processNetworkRequest(NetworkRequest request) throws Exception {
            // 处理网络请求
        }
        
        private static class DataRecord {
            // 数据记录
        }
        
        private static class NetworkRequest {
            // 网络请求
        }
    }
    
    // 事务批处理
    public static class TransactionBatchProcessing {
        
        public void batchTransactionProcessing(List<Transaction> transactions) {
            // 按批次处理事务
            int batchSize = 100;
            for (int i = 0; i < transactions.size(); i += batchSize) {
                int endIndex = Math.min(i + batchSize, transactions.size());
                List<Transaction> batch = transactions.subList(i, endIndex);
                
                processTransactionBatch(batch);
            }
        }
        
        private void processTransactionBatch(List<Transaction> batch) {
            // 处理事务批次
            try {
                // 开始事务
                beginTransaction();
                
                // 处理批次中的每个事务
                for (Transaction transaction : batch) {
                    processTransaction(transaction);
                }
                
                // 提交事务
                commitTransaction();
            } catch (Exception e) {
                // 回滚事务
                rollbackTransaction();
                log.error("事务批处理失败", e);
            }
        }
        
        private void beginTransaction() {
            // 开始事务
        }
        
        private void commitTransaction() {
            // 提交事务
        }
        
        private void rollbackTransaction() {
            // 回滚事务
        }
        
        private void processTransaction(Transaction transaction) {
            // 处理单个事务
        }
        
        private static class Transaction {
            // 事务
        }
    }
    
    // 流式处理
    public static class StreamProcessing {
        
        public void streamFileProcessing(String filePath) throws IOException {
            try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
                lines
                    .filter(line -> !line.isEmpty())
                    .map(this::processLine)
                    .forEach(this::handleProcessedLine);
            }
        }
        
        private String processLine(String line) {
            // 处理行数据
            return line.toUpperCase();
        }
        
        private void handleProcessedLine(String processedLine) {
            // 处理处理后的行
        }
        
        // 并行流处理
        public void parallelStreamProcessing(String filePath) throws IOException {
            try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
                lines
                    .parallel()
                    .filter(line -> !line.isEmpty())
                    .map(this::processLine)
                    .collect(Collectors.toList())
                    .forEach(this::handleProcessedLine);
            }
        }
        
        // 大文件流式处理
        public void largeFileStreamProcessing(String filePath) throws IOException {
            try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath))) {
                reader.lines()
                    .filter(line -> !line.isEmpty())
                    .map(this::processLine)
                    .forEach(this::handleProcessedLine);
            }
        }
    }
}

4. 网络I/O优化 - 让网络"传输"更快速 🌐

生活比喻: 就像优化快递网络,用对了方法,包裹传输更快更可靠!

@Service
public class NetworkIOOptimizationService {
    
    // 连接管理
    public static class ConnectionManagement {
        
        // 连接池
        public static class ConnectionPool {
            private final BlockingQueue<Socket> connectionPool;
            private final AtomicInteger activeConnections;
            private final int maxConnections;
            private final String host;
            private final int port;
            
            public ConnectionPool(String host, int port, int maxConnections) {
                this.host = host;
                this.port = port;
                this.maxConnections = maxConnections;
                this.connectionPool = new LinkedBlockingQueue<>();
                this.activeConnections = new AtomicInteger(0);
                
                // 预创建连接
                for (int i = 0; i < Math.min(10, maxConnections); i++) {
                    try {
                        Socket socket = createConnection();
                        connectionPool.offer(socket);
                    } catch (IOException e) {
                        log.error("创建连接失败", e);
                    }
                }
            }
            
            public Socket getConnection() throws InterruptedException, IOException {
                Socket connection = connectionPool.poll();
                if (connection == null && activeConnections.get() < maxConnections) {
                    connection = createConnection();
                    activeConnections.incrementAndGet();
                }
                return connection;
            }
            
            public void returnConnection(Socket connection) {
                if (connection != null && connection.isConnected()) {
                    connectionPool.offer(connection);
                } else {
                    activeConnections.decrementAndGet();
                }
            }
            
            private Socket createConnection() throws IOException {
                Socket socket = new Socket(host, port);
                socket.setTcpNoDelay(true); // 禁用Nagle算法
                socket.setSoTimeout(5000); // 设置超时
                return socket;
            }
        }
        
        // 长连接复用
        public static class LongConnectionReuse {
            private final Map<String, Socket> connectionCache = new ConcurrentHashMap<>();
            private final ScheduledExecutorService cleanupExecutor;
            
            public LongConnectionReuse() {
                this.cleanupExecutor = Executors.newSingleThreadScheduledExecutor();
                
                // 定期清理无效连接
                cleanupExecutor.scheduleAtFixedRate(this::cleanupConnections, 0, 30, TimeUnit.SECONDS);
            }
            
            public Socket getConnection(String host, int port) throws IOException {
                String key = host + ":" + port;
                Socket connection = connectionCache.get(key);
                
                if (connection == null || !connection.isConnected()) {
                    connection = createConnection(host, port);
                    connectionCache.put(key, connection);
                }
                
                return connection;
            }
            
            private Socket createConnection(String host, int port) throws IOException {
                Socket socket = new Socket(host, port);
                socket.setKeepAlive(true); // 启用keep-alive
                socket.setTcpNoDelay(true);
                return socket;
            }
            
            private void cleanupConnections() {
                connectionCache.entrySet().removeIf(entry -> {
                    Socket socket = entry.getValue();
                    if (!socket.isConnected()) {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            log.error("关闭连接失败", e);
                        }
                        return true;
                    }
                    return false;
                });
            }
            
            public void shutdown() {
                cleanupExecutor.shutdown();
                connectionCache.values().forEach(socket -> {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        log.error("关闭连接失败", e);
                    }
                });
            }
        }
        
        // 连接超时控制
        public static class ConnectionTimeoutControl {
            private final int connectTimeout;
            private final int readTimeout;
            private final int writeTimeout;
            
            public ConnectionTimeoutControl(int connectTimeout, int readTimeout, int writeTimeout) {
                this.connectTimeout = connectTimeout;
                this.readTimeout = readTimeout;
                this.writeTimeout = writeTimeout;
            }
            
            public Socket createConnectionWithTimeout(String host, int port) throws IOException {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress(host, port), connectTimeout);
                socket.setSoTimeout(readTimeout);
                return socket;
            }
            
            public void setTimeouts(Socket socket) throws SocketException {
                socket.setSoTimeout(readTimeout);
                // 注意:Java Socket API不直接支持写超时,需要通过其他方式实现
            }
        }
        
        // 心跳机制
        public static class HeartbeatMechanism {
            private final ScheduledExecutorService heartbeatExecutor;
            private final Map<Socket, Long> lastHeartbeat = new ConcurrentHashMap<>();
            
            public HeartbeatMechanism() {
                this.heartbeatExecutor = Executors.newSingleThreadScheduledExecutor();
                
                // 定期发送心跳
                heartbeatExecutor.scheduleAtFixedRate(this::sendHeartbeat, 0, 30, TimeUnit.SECONDS);
            }
            
            public void registerConnection(Socket socket) {
                lastHeartbeat.put(socket, System.currentTimeMillis());
            }
            
            public void unregisterConnection(Socket socket) {
                lastHeartbeat.remove(socket);
            }
            
            private void sendHeartbeat() {
                lastHeartbeat.entrySet().removeIf(entry -> {
                    Socket socket = entry.getKey();
                    long lastTime = entry.getValue();
                    
                    if (System.currentTimeMillis() - lastTime > 60000) { // 1分钟超时
                        try {
                            socket.close();
                        } catch (IOException e) {
                            log.error("关闭超时连接失败", e);
                        }
                        return true;
                    }
                    
                    // 发送心跳
                    try {
                        socket.getOutputStream().write("HEARTBEAT\n".getBytes());
                        socket.getOutputStream().flush();
                    } catch (IOException e) {
                        log.error("发送心跳失败", e);
                        return true;
                    }
                    
                    return false;
                });
            }
            
            public void shutdown() {
                heartbeatExecutor.shutdown();
            }
        }
    }
    
    // 数据传输优化
    public static class DataTransferOptimization {
        
        // 数据压缩
        public static class DataCompression {
            
            public byte[] compressData(byte[] data) throws IOException {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
                    gzos.write(data);
                }
                return baos.toByteArray();
            }
            
            public byte[] decompressData(byte[] compressedData) throws IOException {
                ByteArrayInputStream bais = new ByteArrayInputStream(compressedData);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try (GZIPInputStream gzis = new GZIPInputStream(bais)) {
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = gzis.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                }
                return baos.toByteArray();
            }
            
            // 网络传输压缩
            public void sendCompressedData(Socket socket, byte[] data) throws IOException {
                byte[] compressedData = compressData(data);
                
                // 发送压缩数据长度
                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                dos.writeInt(compressedData.length);
                dos.write(compressedData);
                dos.flush();
            }
            
            public byte[] receiveCompressedData(Socket socket) throws IOException {
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                
                // 接收压缩数据长度
                int compressedLength = dis.readInt();
                byte[] compressedData = new byte[compressedLength];
                dis.readFully(compressedData);
                
                return decompressData(compressedData);
            }
        }
        
        // 数据分片
        public static class DataFragmentation {
            
            public void sendFragmentedData(Socket socket, byte[] data) throws IOException {
                int fragmentSize = 1024; // 1KB fragments
                int totalFragments = (data.length + fragmentSize - 1) / fragmentSize;
                
                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                
                // 发送总片段数
                dos.writeInt(totalFragments);
                
                for (int i = 0; i < totalFragments; i++) {
                    int start = i * fragmentSize;
                    int end = Math.min(start + fragmentSize, data.length);
                    int fragmentLength = end - start;
                    
                    // 发送片段索引和长度
                    dos.writeInt(i);
                    dos.writeInt(fragmentLength);
                    dos.write(data, start, fragmentLength);
                }
                
                dos.flush();
            }
            
            public byte[] receiveFragmentedData(Socket socket) throws IOException {
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                
                // 接收总片段数
                int totalFragments = dis.readInt();
                Map<Integer, byte[]> fragments = new HashMap<>();
                
                for (int i = 0; i < totalFragments; i++) {
                    // 接收片段索引和长度
                    int fragmentIndex = dis.readInt();
                    int fragmentLength = dis.readInt();
                    
                    byte[] fragment = new byte[fragmentLength];
                    dis.readFully(fragment);
                    
                    fragments.put(fragmentIndex, fragment);
                }
                
                // 重组数据
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                for (int i = 0; i < totalFragments; i++) {
                    baos.write(fragments.get(i));
                }
                
                return baos.toByteArray();
            }
        }
        
        // 流式传输
        public static class StreamTransmission {
            
            public void streamData(Socket socket, InputStream inputStream) throws IOException {
                BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                
                byte[] buffer = new byte[8192];
                int bytesRead;
                
                while ((bytesRead = bis.read(buffer)) != -1) {
                    bos.write(buffer, 0, bytesRead);
                    bos.flush();
                }
            }
            
            public void receiveStreamData(Socket socket, OutputStream outputStream) throws IOException {
                BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                BufferedOutputStream bos = new BufferedOutputStream(outputStream);
                
                byte[] buffer = new byte[8192];
                int bytesRead;
                
                while ((bytesRead = bis.read(buffer)) != -1) {
                    bos.write(buffer, 0, bytesRead);
                    bos.flush();
                }
            }
        }
        
        // 断点续传
        public static class ResumeTransmission {
            
            public void resumeFileTransfer(Socket socket, String filePath, long startPosition) throws IOException {
                try (RandomAccessFile file = new RandomAccessFile(filePath, "r")) {
                    file.seek(startPosition);
                    
                    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                    dos.writeLong(startPosition); // 发送起始位置
                    
                    byte[] buffer = new byte[8192];
                    int bytesRead;
                    
                    while ((bytesRead = file.read(buffer)) != -1) {
                        dos.write(buffer, 0, bytesRead);
                        dos.flush();
                    }
                }
            }
            
            public void receiveResumeFileTransfer(Socket socket, String filePath) throws IOException {
                try (RandomAccessFile file = new RandomAccessFile(filePath, "rw")) {
                    DataInputStream dis = new DataInputStream(socket.getInputStream());
                    
                    // 接收起始位置
                    long startPosition = dis.readLong();
                    file.seek(startPosition);
                    
                    byte[] buffer = new byte[8192];
                    int bytesRead;
                    
                    while ((bytesRead = dis.read(buffer)) != -1) {
                        file.write(buffer, 0, bytesRead);
                    }
                }
            }
        }
    }
    
    // 网络协议优化
    public static class NetworkProtocolOptimization {
        
        // HTTP/2优化
        public static class HTTP2Optimization {
            
            public void http2Request(String url) throws IOException {
                // 使用HTTP/2客户端
                HttpClient client = HttpClient.newBuilder()
                    .version(HttpClient.Version.HTTP_2)
                    .build();
                
                HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .build();
                
                CompletableFuture<HttpResponse<String>> response = client.sendAsync(
                    request, HttpResponse.BodyHandlers.ofString());
                
                response.thenAccept(resp -> {
                    log.info("HTTP/2响应: {}", resp.body());
                });
            }
        }
        
        // WebSocket使用
        public static class WebSocketOptimization {
            
            public void webSocketConnection(String url) throws IOException {
                WebSocketClient client = new WebSocketClient();
                client.connect(url);
                
                // 发送消息
                client.send("Hello WebSocket");
                
                // 接收消息
                client.onMessage(message -> {
                    log.info("收到WebSocket消息: {}", message);
                });
                
                // 关闭连接
                client.close();
            }
        }
        
        // 自定义协议
        public static class CustomProtocol {
            
            public void sendCustomMessage(Socket socket, String message) throws IOException {
                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                
                // 自定义协议格式:长度(4字节) + 消息内容
                byte[] messageBytes = message.getBytes("UTF-8");
                dos.writeInt(messageBytes.length);
                dos.write(messageBytes);
                dos.flush();
            }
            
            public String receiveCustomMessage(Socket socket) throws IOException {
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                
                // 接收消息长度
                int messageLength = dis.readInt();
                
                // 接收消息内容
                byte[] messageBytes = new byte[messageLength];
                dis.readFully(messageBytes);
                
                return new String(messageBytes, "UTF-8");
            }
        }
        
        // 协议升级
        public static class ProtocolUpgrade {
            
            public void upgradeToTLS(Socket socket) throws IOException {
                // 升级到TLS
                SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
                SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, 
                    socket.getInetAddress().getHostName(), socket.getPort(), true);
                
                // 启用TLS
                sslSocket.startHandshake();
                
                // 使用TLS socket进行通信
                useTLSSocket(sslSocket);
            }
            
            private void useTLSSocket(SSLSocket sslSocket) throws IOException {
                // 使用TLS socket
            }
        }
    }
}

🎯 I/O操作优化的实际应用

1. 高并发文件处理 🚀

@Service
public class HighConcurrencyFileProcessingService {
    
    // 异步文件处理
    @Async
    public CompletableFuture<String> processFileAsync(String filePath) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                return processFile(filePath);
            } catch (IOException e) {
                log.error("异步文件处理失败", e);
                return null;
            }
        });
    }
    
    private String processFile(String filePath) throws IOException {
        // 处理文件
        return "Processed: " + filePath;
    }
    
    // 批量文件处理
    public void processBatchFiles(List<String> filePaths) {
        filePaths.parallelStream().forEach(filePath -> {
            try {
                processFile(filePath);
            } catch (IOException e) {
                log.error("批量文件处理失败: {}", filePath, e);
            }
        });
    }
}

2. 网络服务优化 🌐

@Service
public class NetworkServiceOptimizationService {
    
    // 异步网络请求
    @Async
    public CompletableFuture<String> sendAsyncRequest(String url) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                HttpClient client = HttpClient.newHttpClient();
                HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .build();
                
                HttpResponse<String> response = client.send(request, 
                    HttpResponse.BodyHandlers.ofString());
                
                return response.body();
            } catch (Exception e) {
                log.error("异步网络请求失败", e);
                return null;
            }
        });
    }
    
    // 批量网络请求
    public void sendBatchRequests(List<String> urls) {
        urls.parallelStream().forEach(url -> {
            try {
                sendRequest(url);
            } catch (Exception e) {
                log.error("批量网络请求失败: {}", url, e);
            }
        });
    }
    
    private String sendRequest(String url) throws Exception {
        // 发送网络请求
        return "Response from: " + url;
    }
}

🛡️ I/O操作优化的注意事项

1. 资源管理 📊

@Service
public class IOResourceManagementService {
    
    public void manageIOResources() {
        // 使用try-with-resources确保资源正确关闭
        try (FileInputStream fis = new FileInputStream("test.txt");
             BufferedInputStream bis = new BufferedInputStream(fis)) {
            
            // 处理文件
            processFile(bis);
        } catch (IOException e) {
            log.error("文件处理失败", e);
        }
    }
    
    private void processFile(BufferedInputStream bis) throws IOException {
        // 处理文件
    }
    
    // 连接池管理
    public static class ConnectionPoolManager {
        private final BlockingQueue<Socket> connectionPool;
        private final AtomicInteger activeConnections;
        private final int maxConnections;
        
        public ConnectionPoolManager(int maxConnections) {
            this.maxConnections = maxConnections;
            this.connectionPool = new LinkedBlockingQueue<>();
            this.activeConnections = new AtomicInteger(0);
        }
        
        public Socket getConnection() throws InterruptedException, IOException {
            Socket connection = connectionPool.poll();
            if (connection == null && activeConnections.get() < maxConnections) {
                connection = createConnection();
                activeConnections.incrementAndGet();
            }
            return connection;
        }
        
        public void returnConnection(Socket connection) {
            if (connection != null && connection.isConnected()) {
                connectionPool.offer(connection);
            } else {
                activeConnections.decrementAndGet();
            }
        }
        
        private Socket createConnection() throws IOException {
            // 创建连接
            return new Socket();
        }
    }
}

2. 错误处理 🚨

@Service
public class IOErrorHandlingService {
    
    public void handleIOErrors() {
        try {
            // I/O操作
            performIOOperation();
        } catch (IOException e) {
            log.error("I/O操作失败", e);
            // 错误处理逻辑
            handleError(e);
        }
    }
    
    private void performIOOperation() throws IOException {
        // 执行I/O操作
    }
    
    private void handleError(IOException e) {
        // 错误处理逻辑
    }
    
    // 重试机制
    public void retryIOOperation() {
        int maxRetries = 3;
        int retryCount = 0;
        
        while (retryCount < maxRetries) {
            try {
                performIOOperation();
                break; // 成功,退出循环
            } catch (IOException e) {
                retryCount++;
                if (retryCount >= maxRetries) {
                    log.error("I/O操作重试失败,已达到最大重试次数", e);
                    throw new RuntimeException("I/O操作失败", e);
                }
                
                log.warn("I/O操作失败,进行第{}次重试", retryCount, e);
                try {
                    Thread.sleep(1000 * retryCount); // 指数退避
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException("重试被中断", ie);
                }
            }
        }
    }
}

📊 I/O操作优化监控:让性能可视化

@Component
public class IOOptimizationMonitor {
    private final MeterRegistry meterRegistry;
    private final Timer ioTimer;
    private final Counter ioCounter;
    private final Gauge ioThroughput;
    
    public IOOptimizationMonitor(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
        this.ioTimer = Timer.builder("io.operation.time")
                .register(meterRegistry);
        this.ioCounter = Counter.builder("io.operation.count")
                .register(meterRegistry);
        this.ioThroughput = Gauge.builder("io.throughput")
                .register(meterRegistry);
    }
    
    public void recordIOOperation(Duration duration, String operationType) {
        ioTimer.record(duration);
        ioCounter.increment(Tags.of("operation", operationType));
    }
    
    public void recordIOThroughput(double throughput) {
        ioThroughput.set(throughput);
    }
}

🎉 总结:I/O操作优化让程序"读写"更流畅

I/O操作优化就像生活中的各种"交通"技巧:

  • 文件I/O优化 = 优化文件柜整理 📁
  • 异步I/O = 点外卖不用等 ⚡
  • 批量处理 = 批量处理邮件 📦
  • 网络I/O优化 = 优化快递网络 🌐

通过合理使用I/O操作优化,我们可以:

  • 🚀 大幅提升程序性能
  • ⚡ 改善系统响应
  • 🎯 提高资源利用率
  • 💪 增强系统稳定性

记住:I/O优化不是万能的,但它是性能提升的关键! 合理使用I/O操作优化,让你的Java应用运行如行云流水般流畅! ✨


"I/O优化就像魔法,让程序读写更流畅,让性能更卓越!" 🪄📁