<commit_before>package org.nutz.lang.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;
import org.nutz.lang.Streams;
import org.nutz.lang.born.Borning;
import org.nutz.lang.util.Context;
import org.nutz.log.Log;
import org.nutz.log.Logs;

public abstract class Sockets {

    private static final Log log = Logs.get();
    
    public static void send(String host, int port, InputStream ins, OutputStream ops) {
    	send(host, port, ins, ops, 0);
    }

    /**
     * 向某主机发送一些字节内容，并将返回写入输出流
     * 
     * @param host
     *            主机
     * @param port
     *            端口
     * @param ins
     *            发送的内容
     * @param ops
     *            主机返回的输入流
     */
    public static void send(String host, int port, InputStream ins, OutputStream ops, int timeout) {
        Socket socket = null;
        try {
            socket = new Socket();
            socket.connect(InetSocketAddress.createUnresolved(host, port), timeout);
            // 发送关闭命令
            OutputStream sOut = socket.getOutputStream();
            Streams.write(sOut, ins);
            sOut.flush();

            // 接收服务器的反馈
            if (!socket.isClosed()) {
                InputStream sReturn = socket.getInputStream();
                Streams.write(ops, sReturn);
            }
        }
        catch (IOException e) {
            throw Lang.wrapThrow(e);
        }
        finally {
            Streams.safeClose(ins);
            Streams.safeClose(ops);
            safeClose(socket);
        }
    }

    /**<commit_msg>fix: Sockets连接主机的方法改成传统方式.<commit_after>package org.nutz.lang.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;
import org.nutz.lang.Streams;
import org.nutz.lang.born.Borning;
import org.nutz.lang.util.Context;
import org.nutz.log.Log;
import org.nutz.log.Logs;

public abstract class Sockets {

    private static final Log log = Logs.get();
    
    public static void send(String host, int port, InputStream ins, OutputStream ops) {
    	send(host, port, ins, ops, 0);
    }

    /**
     * 向某主机发送一些字节内容，并将返回写入输出流
     * 
     * @param host
     *            主机
     * @param port
     *            端口
     * @param ins
     *            发送的内容
     * @param ops
     *            主机返回的输入流
     */
    public static void send(String host, int port, InputStream ins, OutputStream ops, int timeout) {
        Socket socket = null;
        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress(host, port), timeout);
            // 发送关闭命令
            OutputStream sOut = socket.getOutputStream();
            Streams.write(sOut, ins);
            sOut.flush();

            // 接收服务器的反馈
            if (!socket.isClosed()) {
                InputStream sReturn = socket.getInputStream();
                Streams.write(ops, sReturn);
            }
        }
        catch (IOException e) {
            throw Lang.wrapThrow(e);
        }
        finally {
            Streams.safeClose(ins);
            Streams.safeClose(ops);
            safeClose(socket);
        }
    }

    /**
{'fraction_non_alphanumeric': 0.10581023454157783, 'fraction_numerical': 0.0005330490405117271, 'mean_word_length': 2.0437956204379564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5548194', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1210, 'n_words': 250}