java HTP协议之HttpConnUtil

package com.catstudio.user.client.interstellar;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpConnUtil {

	public static HttpURLConnection connect(String address) throws IOException {
		URL url = new URL(address);
		HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
		httpUrlConnection.setRequestMethod("POST");
		httpUrlConnection.setDoOutput(true);
		httpUrlConnection.setDoInput(true);
		httpUrlConnection.connect();
		return httpUrlConnection;
	}

	public static void close(InputStream din) {
		if (din != null) {
			try {
				din.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			din = null;
		}
	}

	public static void close(OutputStream dou) {
		if (dou != null) {
			try {
				dou.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			dou = null;
		}
	}

	public static void close(HttpURLConnection conn) {
		if (conn != null) {
			conn.disconnect();
			conn = null;
		}
	}

	/**
	 * 获取DataInputStream,当in 数据中没有数据,或者数据长度小于4个字节(无效请求)的时候返回null
	 * 
	 * @param conn
	 * @return
	 */
	public static DataInputStream getDataInputStream(InputStream in) {
		if (in == null) {
			return null;
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		// 读取数据到缓冲流
		try {
			while (true) {
				int b = in.read();
				if (b == -1) {
					break;
				}
				dos.write(b);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		byte[] bytes = baos.toByteArray();
		// 关闭暂时的流
		HttpConnUtil.close(dos);
		HttpConnUtil.close(baos);
		// 无效数据长度
		if (bytes.length < 4) {
			return null;
		}
		// 返回新的流
		return new DataInputStream(new ByteArrayInputStream(bytes));
	}

	/**
	 * 获取DataInputStream,当in 数据中没有数据,或者数据长度小于4个字节(无效请求)的时候返回null
	 * 
	 * @param conn
	 * @return
	 */
	public static DataInputStream getGzipDataInputStream(InputStream in) {
		if (in == null) {
			return null;
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		// 读取数据到缓冲流
		try {
			while (true) {
				int b = in.read();
				if (b == -1) {
					break;
				}
				dos.write(b);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		byte[] bytes = null;
		try {
			bytes = baos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// System.out.println("字节长度:" + bytes.length);
		// 关闭暂时的流
		byte[] gbipByte = GZipUtil.uncompress(bytes);
		// System.out.println("解压后长度:" + gbipByte.length);
		HttpConnUtil.close(dos);
		HttpConnUtil.close(baos);
		// 无效数据长度
		if (bytes.length < 4) {
			return null;
		}
		// 返回新的流
		return new DataInputStream(new ByteArrayInputStream(gbipByte));
	}

}

在getDataInputStream方法中,之所以会重新write一遍,是为了防止数据流被截断。第二个有趣的地方是:ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); 这样写之后,操作dos就会操作baos,因为他俩操作的是同一个数据流,持有的是同一个引用。相当于c++中传递指针那样,最后还是会对同一个对象进行操作。




你可能感兴趣的:(java HTP协议之HttpConnUtil)