PageRenderTime 35ms CodeModel.GetById 8ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ictclas4j/src/com/gftech/util/GFNet.java

http://ictclas4j.googlecode.com/
Java | 175 lines | 108 code | 28 blank | 39 comment | 25 complexity | d9e04232af68ee363ccffcb0134ce552 MD5 | raw file
  1package com.gftech.util;
  2
  3import java.io.DataInputStream;
  4import java.io.EOFException;
  5import java.io.IOException;
  6import java.io.InputStream;
  7import java.io.OutputStream;
  8import java.net.InetAddress;
  9import java.net.UnknownHostException;
 10import java.util.ArrayList;
 11
 12/**
 13 * ????????
 14 * 
 15 * @author sinboy
 16 * @version 1.0 2005-9-27
 17 * 
 18 */
 19final public class GFNet {
 20
 21	/**
 22	 * ???????IP??
 23	 * 
 24	 * @return
 25	 */
 26	public static String getLocalHost() {
 27		String result = null;
 28
 29		try {
 30			result = InetAddress.getLocalHost().getHostName() + "/" + InetAddress.getLocalHost().getHostAddress();
 31		} catch (UnknownHostException e) {
 32			e.printStackTrace();
 33		}
 34		return result;
 35	}
 36
 37	/**
 38	 * ?????????Buffer????
 39	 * 
 40	 * @param out
 41	 *            ???
 42	 * @param b
 43	 *            ???????
 44	 * @throws IOException
 45	 */
 46	public static void send(OutputStream out, byte[] b) throws IOException {
 47		if (out == null)
 48			throw new IOException();
 49		if (b != null) {
 50			try {
 51				if (b != null) {
 52					out.write(b);
 53					out.flush();
 54				}
 55			} catch (IOException e) {
 56				throw e;
 57			}
 58		}
 59	}
 60
 61	/**
 62	 * ??????????????
 63	 * 
 64	 * @param in
 65	 *            ???????Buffer????
 66	 * @param len
 67	 *            ?????
 68	 * @return
 69	 * @throws IOException
 70	 */
 71	public static byte[] receive(InputStream in, int len) throws IOException {
 72		byte[] result = null;
 73
 74		if (in == null)
 75			throw new IOException();
 76		if (len > 0) {
 77			result = new byte[len];
 78			in.read(result);
 79		}
 80		return result;
 81	}
 82
 83	/**
 84	 * ????????????????
 85	 * 
 86	 * @param in
 87	 *            ???
 88	 * @return
 89	 * @throws IOException
 90	 */
 91	public static byte[] receive(InputStream in) throws IOException {
 92		byte[] result = null;
 93
 94		if (in == null)
 95			throw new IOException();
 96		int b;
 97		ArrayList<Byte> temp = new ArrayList<Byte>();
 98		while ((b = in.read()) != -1) {
 99			temp.add((byte) b);
100		}
101
102		if (temp.size() > 0) {
103			result = new byte[temp.size()];
104			for (int i = 0; i < temp.size(); i++)
105				result[i] = temp.get(i);
106		}
107
108		return result;
109	}
110
111	 
112	public static final short readUInt8(InputStream in) throws IOException {
113		int ch = in.read();
114		if (ch < 0) {
115			throw new EOFException();
116		}
117		return (short) (ch & 0xff);
118	}
119
120	public static final int readUInt16(InputStream in) throws IOException {
121		return (readUInt8(in) + (readUInt8(in) << 8)) & 0xffff;
122	}
123
124	public static final int readInt32(InputStream in) throws IOException {
125		return readInt32(in, false);
126	}
127
128	public static final int readInt32(InputStream in, boolean isHighFirst) throws IOException {
129		if (isHighFirst)
130			return (readUInt8(in) << 24) + (readUInt8(in) << 16) + (readUInt8(in) << 8) + readUInt8(in);
131		else
132			return readUInt8(in) + (readUInt8(in) << 8) + (readUInt8(in) << 16) + (readUInt8(in) << 24);
133	}
134
135	public static byte[] readBytes(DataInputStream in, int len) throws IOException {
136		if (in != null && len > 0) {
137			byte[] b = new byte[len];
138
139			for (int i = 0; i < len; i++)
140				b[i] = in.readByte();
141
142			return b;
143		}
144
145		return null;
146	}
147	public static final void writeInt8(OutputStream out, int value) throws IOException { 
148		  out.write(value);
149	}
150	
151	
152	public static final boolean writeInt32(OutputStream out, int value) throws IOException {
153
154		return writeInt32(out, value, false);
155	}
156
157	public static final boolean writeInt32(OutputStream out, int value, boolean isHighFirst) throws IOException {
158		boolean result = false;
159
160		if (out != null) {
161			byte[] b = GFCommon.int2bytes(value, isHighFirst);
162			out.write(b);
163		}
164		return result;
165	}
166
167	public static int readInt(DataInputStream in, boolean isBig) throws IOException {
168		if (in != null) {
169			return in.readInt();
170		} else
171			throw new IOException();
172
173	}
174
175}