⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 streamio.java

📁 一个MMORPG手机游戏的服务器端程序源代码
💻 JAVA
字号:
package zsw_mmorpg.log;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Digital Online</p>
 * @author byron.peng
 * @version 1.0
 */ 

import java.io.*;

public class StreamIO {
  public StreamIO() {
  }

  public static void main(String[] args) {
    StreamIO streamIO1 = new StreamIO();
  }

  /**
   * 读取指定长的整数
   */
  public static final int readInt(InputStream in, int len) throws java.io.
      IOException {
    byte[] b = new byte[len];
    int p = 0;
    int x = 0;

    for (int loop = 0; loop < (len - p); loop++) {
      int r = in.read(b, p, (len - p));
      if (r == -1) {
        break;
      }

      p += r;
    }

    return (bytesToInt(b, 0, len));
  }

  /**
   */
  public static final String readString(InputStream in) throws java.io.
      IOException {
    StringBuffer s = new StringBuffer();

    int b = in.read();

    while ( (b == 0) || (b == '\r') || (b == '\n') || (b == 0x01)) {
      b = in.read();
    }

    while (true) {
      if (b == -1) {
        throw new IOException("End of Input Stream before NULL byte");
      }

      if ( (b == 0) || (b == '\r') || (b == '\n')) {
        break;
      }

      s.append( (char) b);
      b = in.read();
    }

    if (s.length() == 0) {
      return (null);
    }
    else {
      return (s.toString());
    }
  }

  /**
   * 读取一个以'\0'结束的字符串
   */
  public static synchronized final String readCString(InputStream in) throws
      java.io.IOException {
    StringBuffer s = new StringBuffer();

    int b = in.read();
    while (b != 0) {
      if (b == -1) {
        throw new IOException("End of Input Stream before NULL byte");
      }

      s.append( (char) b);
      b = in.read();
    }

    if (s.length() == 0) {
      return (null);
    }
    else {
      return (s.toString());
    }
  }

  /**
   * 读取指定长度的字符串,只取'\0'前的数据
   */
  public static final String readString(InputStream in, int len) throws java.io.
      IOException {
    if (len < 1) {
      return (null);
    }

    byte[] b = new byte[len];
    int l = 0;
    StringBuffer s = new StringBuffer();

    while (l < len) {
      int r = in.read(b, 0, (len - l));
      if (r == -1) {
        throw new IOException("EOS before NUL byte read.");
      }

      l += r;
      int i = 0;
      //取到\0为止
      for (; i < r; i++) {
        if (b[i] == '\0') {
          break;
        }
      }
      s.append(new String(b, 0, i));
      //s.append(new String(b, 0, r));
    }

    if (s.length() == 0) {
      return (null);
    }
    else {
      return (s.toString());
    }
  }

  /**
   * 将整数转成字节
   */
  public static final byte[] intToBytes(int num, int len) {
    byte[] b = new byte[len];
    int sw = ( (len - 1) * 8);
    int mask = (0xff << sw);

    for (int l = 0; l < len; l++) {
      b[l] = (byte) ( (num & mask) >>> sw);
      sw -= 8;
      mask >>>= 8;
    }

    return (b);
  }

  /**
   * 将字节转成整数
   */
  public static final int bytesToInt(byte[] b, int offset, int size) {
    int num = 0;
    int sw = 8 * (size - 1);

    for (int loop = 0; loop < size; loop++) {
      num |= ( (int) b[offset + loop] & 0x00ff) << sw;
      sw -= 8;
    }

    return (num);
  }

  /**
   * 写整数
   */
  public static void writeInt(int x, int len, OutputStream out) throws java.io.
      IOException {
    out.write(intToBytes(x, len));
  }

  /**
   * 写以'\0'为结束的字符串
   */
  public static void writeCString(String s, OutputStream out) throws java.io.
      IOException {
    writeString(s, out);
    out.write( (byte) 0);
  }

  public static void writeHeartbeat(OutputStream out) throws java.io.
      IOException {
    out.write( (byte) 0x1);
  }

  /**
   * 写指定长度的字符串
   */
  public static void writeString(String s, int len, OutputStream out) throws
      java.io.IOException {
    if (s == null) {
      return;
    }

    if (len > s.length()) {
      writeString(s, out);
    }
    else {
      writeString(s.substring(0, len), out);
    }
  }

  /**
   * 写字符串
   */
  public static void writeString(String s, OutputStream out) throws java.io.
      IOException {
    if (s == null) {
      return;
    }
    out.write(s.getBytes());
  }

  /**
   * 写指定长度的字符串,不够补'\0'
   */
  public static void writeOString(String s, int len, OutputStream out) throws
      java.io.IOException {
    byte[] temp = new byte[len];
    if (s != null) {
      BytesCopy(s.getBytes(), temp, 0, len - 1, 0);
    }
    out.write(temp);
  }

  /**
   * 两个字节的拷贝
   */
  public static void BytesCopy(byte source[], byte dest[], int sourcebegin,
                               int sourceend, int destbegin) {
    int j = 0;
    for (int i = sourcebegin; i <= sourceend && i < source.length; i++) {
      dest[destbegin + j] = source[i];
      j++;
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -