/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

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