microburlapoutput.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 695 行 · 第 1/2 页

JAVA
695
字号
    print("</ref>");  }  /**   * Writes a generic object.  writeObject understands the following types:   *   * <ul>   * <li>null   * <li>java.lang.String   * <li>java.lang.Boolean   * <li>java.lang.Integer   * <li>java.lang.Long   * <li>java.util.Date   * <li>byte[]   * <li>java.util.Vector   * <li>java.util.Hashtable   * </ul>   *   * Unknown objects will call <code>writeCustomObject</code>.   */  public void writeObject(Object object)    throws IOException  {    if (object == null)      writeNull();    else if (object instanceof String)      writeString((String) object);    else if (object instanceof Boolean)      writeBoolean(((Boolean) object).booleanValue());    else if (object instanceof Integer)      writeInt(((Integer) object).intValue());    else if (object instanceof Long)      writeLong(((Long) object).longValue());    else if (object instanceof Date)      writeUTCDate(((Date) object).getTime());    else if (object instanceof byte[]) {      byte []data = (byte []) object;      writeBytes(data, 0, data.length);    }    else if (object instanceof Vector) {      Vector vector = (Vector) object;      int size = vector.size();      writeListBegin(size, null);      for (int i = 0; i < size; i++)        writeObject(vector.elementAt(i));            writeListEnd();    }    else if (object instanceof Hashtable) {      Hashtable hashtable = (Hashtable) object;      writeMapBegin(null);      Enumeration e = hashtable.keys();      while (e.hasMoreElements()) {        Object key = e.nextElement();        Object value = hashtable.get(key);        writeObject(key);        writeObject(value);      }      writeMapEnd();    }    else      writeCustomObject(object);  }    /**   * Applications which override this can do custom serialization.   *   * @param object the object to write.   */  public void writeCustomObject(Object object)    throws IOException  {    throw new IOException("unexpected object: " + object);  }  /**   * Writes the list header to the stream.  List writers will call   * <code>writeListBegin</code> followed by the list contents and then   * call <code>writeListEnd</code>.   *   * <code><pre>   * &lt;list>   *   &lt;type>java.util.ArrayList&lt;/type>   *   &lt;length>3&lt;/length>   *   &lt;int>1&lt;/int>   *   &lt;int>2&lt;/int>   *   &lt;int>3&lt;/int>   * &lt;/list>   * </pre></code>   */  public void writeListBegin(int length, String type)    throws IOException  {    print("<list><type>");    if (type != null)      print(type);    print("</type><length>");    printInt(length);    print("</length>");  }  /**   * Writes the tail of the list to the stream.   */  public void writeListEnd()    throws IOException  {    print("</list>");  }  /**   * Writes the map header to the stream.  Map writers will call   * <code>writeMapBegin</code> followed by the map contents and then   * call <code>writeMapEnd</code>.   *   * <code><pre>   * &lt;map>   *   &lt;type>java.util.Hashtable&lt;/type>   *   &lt;string>a&lt;/string;&lt;int>1&lt;/int>   *   &lt;string>b&lt;/string;&lt;int>2&lt;/int>   *   &lt;string>c&lt;/string;&lt;int>3&lt;/int>   * &lt;/map>   * </pre></code>   */  public void writeMapBegin(String type)    throws IOException  {    print("<map><type>");    if (type != null)      print(type);    print("</type>");  }  /**   * Writes the tail of the map to the stream.   */  public void writeMapEnd()    throws IOException  {    print("</map>");  }  /**   * Writes a remote object reference to the stream.  The type is the   * type of the remote interface.   *   * <code><pre>   * &lt;remote>   *   &lt;type>test.account.Account&lt;/type>   *   &lt;string>http://caucho.com/foo;ejbid=bar&lt;/string>   * &lt;/remote>   * </pre></code>   */  public void writeRemote(String type, String url)    throws IOException  {    print("<remote><type>");    if (type != null)      print(type);    print("</type><string>");    print(url);    print("</string></remote>");  }    /**   * Prints an integer to the stream.   *   * @param v the integer to print.   */  public void printInt(int v)    throws IOException  {    print(String.valueOf(v));  }  /**   * Prints a long to the stream.   *   * @param v the long to print.   */  public void printLong(long v)    throws IOException  {    print(String.valueOf(v));  }  /**   * Prints a string to the stream, properly encoded.   *   * @param v the string to print.   */  public void printString(String v)    throws IOException  {    int len = v.length();        for (int i = 0; i < len; i++) {      char ch = v.charAt(i);            switch (ch) {      case '<':        print("&lt;");        break;              case '&':        print("&amp;");        break;              case '\r':        print("&#13;");        break;              default:        if (ch < 0x80)          os.write(ch);        else if (ch < 0x800) {          os.write(0xc0 + ((ch >> 6) & 0x1f));          os.write(0x80 + (ch & 0x3f));        }        else {          os.write(0xe0 + ((ch >> 12) & 0xf));          os.write(0x80 + ((ch >> 6) & 0x3f));          os.write(0x80 + (ch & 0x3f));        }        break;      }    }  }      /**   * Prints a byte array to the stream, properly encoded  in base64.   *   * @param data the bytes to print.   */  public void printBytes(byte []data, int offset, int length)    throws IOException  {    int i;        for (; length >= 3; length -= 3) {      int chunk = (((data[offset] & 0xff) << 16) +                   ((data[offset + 1] & 0xff) << 8) +                   (data[offset + 2] & 0xff));      os.write(base64encode(chunk >> 18));      os.write(base64encode(chunk >> 12));      os.write(base64encode(chunk >> 6));      os.write(base64encode(chunk));      offset += 3;    }    if (length == 2) {      int chunk = ((data[offset] & 0xff) << 8) + (data[offset + 1] & 0xff);      os.write(base64encode(chunk >> 12));      os.write(base64encode(chunk >> 6));      os.write(base64encode(chunk));      os.write('=');    } else if (length == 1) {      int chunk = data[offset] & 0xff;      os.write(base64encode(chunk >> 6));      os.write(base64encode(chunk));      os.write('=');      os.write('=');    }  }  /**   * Converts the digit to its base64 encoding.   */  public static char base64encode(int d)  {    d &= 0x3f;    if (d < 26)      return (char) (d + 'A');    else if (d < 52)      return (char) (d + 'a' - 26);    else if (d < 62)      return (char) (d + '0' - 52);    else if (d == 62)      return '+';    else      return '/';  }    /**   * Prints a date.   *   * @param date the date to print.   */  public void printDate(Calendar calendar)    throws IOException  {    int year = calendar.get(Calendar.YEAR);    os.write((char) ('0' + (year / 1000 % 10)));    os.write((char) ('0' + (year / 100 % 10)));    os.write((char) ('0' + (year / 10 % 10)));    os.write((char) ('0' + (year % 10)));    int month = calendar.get(Calendar.MONTH) + 1;    os.write((char) ('0' + (month / 10 % 10)));    os.write((char) ('0' + (month % 10)));    int day = calendar.get(Calendar.DAY_OF_MONTH);    os.write((char) ('0' + (day / 10 % 10)));    os.write((char) ('0' + (day % 10)));    os.write('T');    int hour = calendar.get(Calendar.HOUR_OF_DAY);    os.write((char) ('0' + (hour / 10 % 10)));    os.write((char) ('0' + (hour % 10)));    int minute = calendar.get(Calendar.MINUTE);    os.write((char) ('0' + (minute / 10 % 10)));    os.write((char) ('0' + (minute % 10)));    int second = calendar.get(Calendar.SECOND);    os.write((char) ('0' + (second / 10 % 10)));    os.write((char) ('0' + (second % 10)));    os.write('Z');  }  /**   * Prints a string as ascii to the stream.  Used for tags, etc.   * that are known to the ascii.   *   * @param s the ascii string to print.   */  public void print(String s)    throws IOException  {    int len = s.length();    for (int i = 0; i < len; i++) {      int ch = s.charAt(i);      os.write(ch);    }  }}

⌨️ 快捷键说明

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