StreamUtil

public class StreamUtil {
    private static final String TAG = "StreamUtil";

    private StreamUtil() {
    }

    public static void sendInt(OutputStream os, int intValue) throws IOException {
        os.write(ByteUtil.intToBytes(intValue));
        os.flush();
    }

    public static void sendString(OutputStream os, String stringValue) throws IOException {
        byte[] bytes = stringValue.getBytes("UTF-8");
        // 发送字符串字节数组长度
        sendInt(os, bytes.length);
        // 发送字符串内容
        os.write(bytes);
        os.flush();
    }

    public static int receiveInt(InputStream is) throws IOException {
        byte[] bytes = new byte[4];
        is.read(bytes);
        return ByteUtil.bytesToInt(bytes);
    }

    public static String receiveString(InputStream is) throws IOException {
        // 接收字符串长度
        int length = receiveInt(is);
        // 接收字符串内
        byte[] results = new byte[length];
        is.read(results);
        return new String(results, "UTF-8");
    }

    public static int readInt(ByteArrayInputStream bais) throws IOException {
        byte[] ints = new byte[4];
        bais.read(ints);
        return ByteUtil.bytesToInt(ints);
    }

    public static String readString(ByteArrayInputStream bais) throws IOException {
        byte[] lens = new byte[2];
        bais.read(lens);
        byte[] strs = new byte[ByteUtil.bytesToShort(lens)];
        bais.read(strs);
        return new String(strs, "UTF-8");
    }

    public static void writeInt(ByteArrayOutputStream baos, int intValue) throws IOException {
        baos.write(ByteUtil.intToBytes(intValue));
    }

    public static void writeString(ByteArrayOutputStream baos, String stringValue) throws IOException {
        byte[] strs = null;
        try {
            strs = stringValue.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // can not reachable
        }
        baos.write(ByteUtil.shortToBytes((short) strs.length));
        baos.write(strs);
    }

    public static void close(Closeable stream, String errMsg) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                Logger.e(TAG, errMsg);
            }
        }
    }
}

 

posted @ 2014-03-07 18:05  bingoogolapple  阅读(519)  评论(0)    收藏  举报