- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import org.apache.log4j.Logger;
- /**
- * <p>
- * 类名称: [解析InputStream工具类]
- * </p>
- * <p>
- * 类描述: [提供对InputStream解析方法]
- * </p>
- * <p>
- * 创建时间 8/3/11 4:24 PM
- * </p>
- *
- */
- public class InputStreamParseTool {
- /**
- * <p>
- * 属性描述: [日志]
- * </p>
- */
- private static final Logger T_LOG = Logger
- .getLogger(InputStreamParseTool.class);
- /**
- * <p>
- * 属性描述: [需要解析的InputStream]
- * </p>
- */
- private InputStream in;
- /**
- * <p>
- * 属性描述: [记录请求内容]
- * </p>
- */
- private StringBuffer strBuff;
- /**
- * <p>
- * 属性描述: [byte数组(解析时使用)]
- * </p>
- */
- private byte b;
- /**
- * <p>
- * 属性描述: []
- * </p>
- */
- private int bit = 7;
- /**
- * <p>
- * 方法描述: [解析工具构造方法]
- * </p>
- *
- * @param in
- * 解析工具构造方法
- */
- public InputStreamParseTool(InputStream in) {
- this.in = in;
- strBuff = new StringBuffer();
- }
- /**
- * <p>
- * 方法描述: [解析成字符串读取]
- * </p>
- *
- * @param len
- * 需要读取的长度
- *
- * @return 读取len长度后转换成的字符串
- *
- * @throws IOException
- * 抛出异常的原因
- */
- public String readString(int len) throws IOException {
- byte[] b = new byte[len];
- bit = 7;
- if (in.read(b, 0, len) == -1) {
- strBuff.append(new String(b, "gbk"));
- throw new IOException();
- }
- strBuff.append(new String(b, "gbk"));
- return new String(b, "gbk");
- }
- /**
- *
- * <p>
- * Discription:[获取无符号数的8个bit位]
- * </p>
- *
- * @param b
- * @return
- * @author:[创建者中文名字]
- * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
- */
- public static String getBit(byte b) {
- StringBuffer sb = new StringBuffer();
- int[] bit = new int[8];
- int len = bit.length;
- for (int i = 0; i < len; i++) {
- bit[8 - i - 1] = (b >> i) & 1;
- }
- for (int i : bit) {
- sb.append(i);
- }
- return sb.toString();
- }
- /**
- * <p>
- * 方法描述: [从InputStream下一个byte开始读取len长度的byte转换成bit]
- * </p>
- *
- * @param len
- * 需要转换成比特的字节长度
- *
- * @return 二进制字符串
- *
- * @throws IOException
- * 抛出异常的原因
- */
- public String readBit(int len) throws IOException {
- StringBuffer sb = new StringBuffer();
- byte ba;
- int j = len;
- while (j > 0) {
- ba = readByte();
- sb.append(getBit(ba));
- j--;
- }
- return sb.toString();
- }
- /**
- * <p>
- * 方法描述: [读取len长度的bit位]
- * </p>
- *
- * @return 返回结果的说明
- *
- * @throws IOException
- * 抛出异常的原因
- */
- public String getBit(int len) throws IOException {
- StringBuffer sb = new StringBuffer();
- for(int i=0; i<len; i++){
- if ((bit < 0) || (bit == 7)) {
- b = readByte();
- bit = 7;
- }
- bit--;
- sb.append(readBit(b, bit));
- }
- return sb.toString();
- }
- /**
- * <p>
- * 方法描述: [读取InputStream的下一个byte]
- * </p>
- *
- * @return byte
- *
- * @throws IOException
- * 抛出异常的原因
- */
- public byte readByte() throws IOException {
- byte[] by = new byte[1];
- if (in.read(by, 0, 1) == -1) {
- throw new IOException();
- }
- strBuff.append((char) by[0]);
- return (byte) (by[0] & 0xff);
- }
- /**
- * <p>
- * 方法描述: [跳过len长度的bit位]
- * </p>
- *
- * @param len
- * 需要跳过的长度
- *
- * @throws IOException
- * 抛出异常
- */
- public void skipBit(int len) throws IOException {
- getBit(len);
- }
- /**
- * <p>
- * 方法描述: [将流读取成字符串]
- * </p>
- *
- * @return 读取后的字符串
- *
- * @throws Exception
- * 抛出异常
- */
- public String readToString() throws Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String line = null;
- StringBuffer str = new StringBuffer();
- while ((line = reader.readLine()) != null) {
- str.append(line);
- }
- strBuff.append(str);
- return str.toString();
- }
- /**
- * <p>
- * 方法描述: [读取source字节的第off位的bit值]
- * </p>
- *
- * @param source
- * 需要读取的字节
- * @param off
- * 读取第off位置的bit值
- *
- * @return 返回读取到的bit值
- */
- private String readBit(byte source, int off) {
- int c = (source >> off) & 1;
- return String.valueOf(c);
- }
- /**
- * <p>
- * 方法描述: [流关闭]
- * </p>
- */
- public void closeStream() {
- try {
- if (in != null) {
- in.close();
- }
- } catch (IOException e) {
- T_LOG.info("-->CANBUS服务-->TBox协议解析工具:请求流关闭失败!");
- }
- }
- /**
- * <p>
- * 方法描述: [方法功能中文描述]
- * </p>
- *
- * @return 返回结果的说明
- */
- public StringBuffer getStrBuff() {
- return strBuff;
- }
- /**
- * <p>
- * 方法描述: [方法功能中文描述]
- * </p>
- *
- * @param strBuff
- * 参数说明
- */
- public void setStrBuff(StringBuffer strBuff) {
- this.strBuff = strBuff;
- }
- }