burlapinput.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,810 行 · 第 1/3 页

JAVA
1,810
字号
    long value = 0;    for (; ch >= '0' && ch <= '9'; ch = read())      value = 10 * value + ch - '0';    _peek = ch;    return sign * value;  }    /**   * Parses a 64-bit double value from the stream.   *   * <pre>   * b64 b56 b48 b40 b32 b24 b16 b8   * </pre>   */  private double parseDouble()    throws IOException  {    int ch = skipWhitespace();    _sbuf.setLength(0);        for (; ! isWhitespace(ch) && ch != '<'; ch = read())      _sbuf.append((char) ch);    _peek = ch;        return new Double(_sbuf.toString()).doubleValue();  }  /**   * Parses a date value from the stream.   */  protected long parseDate()    throws IOException  {    if (_utcCalendar == null)      _utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));    return parseDate(_utcCalendar);  }  /**   * Parses a date value from the stream.   */  protected long parseDate(Calendar calendar)    throws IOException  {    int ch = skipWhitespace();        int year = 0;    for (int i = 0; i < 4; i++) {      if (ch >= '0' && ch <= '9')        year = 10 * year + ch - '0';      else        throw expectedChar("year", ch);      ch = read();    }    int month = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        month = 10 * month + ch - '0';      else        throw expectedChar("month", ch);      ch = read();    }    int day = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        day = 10 * day + ch - '0';      else        throw expectedChar("day", ch);      ch = read();    }    if (ch != 'T')      throw expectedChar("`T'", ch);    ch = read();    int hour = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        hour = 10 * hour + ch - '0';      else        throw expectedChar("hour", ch);      ch = read();    }    int minute = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        minute = 10 * minute + ch - '0';      else        throw expectedChar("minute", ch);      ch = read();    }    int second = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        second = 10 * second + ch - '0';      else        throw expectedChar("second", ch);      ch = read();    }    int ms = 0;    if (ch == '.') {      ch = read();      while (ch >= '0' && ch <= '9') {        ms = 10 * ms + ch - '0';	ch = read();      }    }    for (; ch > 0 && ch != '<'; ch = read()) {    }    _peek = ch;    calendar.set(Calendar.YEAR, year);    calendar.set(Calendar.MONTH, month - 1);    calendar.set(Calendar.DAY_OF_MONTH, day);    calendar.set(Calendar.HOUR_OF_DAY, hour);    calendar.set(Calendar.MINUTE, minute);    calendar.set(Calendar.SECOND, second);    calendar.set(Calendar.MILLISECOND, ms);    return calendar.getTime().getTime();  }    protected String parseString()    throws IOException  {    _sbuf.setLength(0);    return parseString(_sbuf).toString();  }    /**   * Parses a string value from the stream.  The burlap object's   * string buffer is used for the result.   */  protected StringBuffer parseString(StringBuffer sbuf)    throws IOException  {    int ch;    while ((ch = readChar()) >= 0)      sbuf.append((char) ch);    return sbuf;  }  org.w3c.dom.Node parseXML()    throws IOException  {    throw error("help!");  }    /**   * Reads a character from the underlying stream.   */  int readChar()    throws IOException  {    int ch = read();    if (ch == '<' || ch < 0) {      _peek = ch;      return -1;    }        if (ch == '&') {      ch = read();      if (ch == '#') {        ch = read();        if (ch >= '0' && ch <= '9') {          int v = 0;          for (; ch >= '0' && ch <= '9'; ch = read()) {            v = 10 * v + ch - '0';          }          if (ch != ';')            throw error("expected ';' at " + (char) ch);          return (char) v;        }        else          throw error("expected digit at " + (char) ch);      }      else {        _entityBuffer.setLength(0);        for (; ch >= 'a' && ch <= 'z'; ch = read())          _entityBuffer.append((char) ch);        String entity = _entityBuffer.toString();        if (ch != ';')          throw expectedChar("';'", ch);                if (entity.equals("amp"))          return '&';        else if (entity.equals("apos"))          return '\'';        else if (entity.equals("quot"))          return '"';        else if (entity.equals("lt"))          return '<';        else if (entity.equals("gt"))          return '>';        else          throw new BurlapProtocolException("unknown XML entity &" + entity + "; at `" + (char) ch + "'");      }    }    else if (ch < 0x80)      return (char) ch;    else if ((ch & 0xe0) == 0xc0) {      int ch1 = read();      int v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);      return (char) v;    }    else if ((ch & 0xf0) == 0xe0) {      int ch1 = read();      int ch2 = read();      int v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);      return (char) v;    }    else      throw new BurlapProtocolException("bad utf-8 encoding");  }    /**   * Parses a byte array.   */  protected byte []parseBytes()    throws IOException  {    ByteArrayOutputStream bos = new ByteArrayOutputStream();    parseBytes(bos);    return bos.toByteArray();  }    /**   * Parses a byte array.   */  protected ByteArrayOutputStream parseBytes(ByteArrayOutputStream bos)    throws IOException  {    int ch;    for (ch = skipWhitespace(); ch >= 0 && ch != '<'; ch = skipWhitespace()) {      int b1 = ch;      int b2 = read();      int b3 = read();      int b4 = read();      if (b4 != '=') {        int chunk = ((base64Decode[b1] << 18) +                     (base64Decode[b2] << 12) +                     (base64Decode[b3] << 6) +                     (base64Decode[b4]));        bos.write(chunk >> 16);        bos.write(chunk >> 8);        bos.write(chunk);      }      else if (b3 != '=') {        int chunk = ((base64Decode[b1] << 10) +                     (base64Decode[b2] << 4) +                     (base64Decode[b3] >> 2));        bos.write(chunk >> 8);        bos.write(chunk);      }      else {        int chunk = ((base64Decode[b1] << 2) +                     (base64Decode[b2] >> 4));        bos.write(chunk);      }    }    if (ch == '<')      _peek = ch;        return bos;  }    public void expectTag(int expectTag)    throws IOException  {    int tag = parseTag();    if (tag != expectTag)      throw error("expected " + tagName(expectTag) + " at " + tagName(tag));  }  /**   * Parses a tag.  Returns true if it's a start tag.   */  protected int parseTag()    throws IOException  {    if (_peekTag >= 0) {      int tag = _peekTag;      _peekTag = -1;      return tag;    }        int ch = skipWhitespace();    int endTagDelta = 0;    if (ch != '<')      throw expectedChar("'<'", ch);    ch = read();    if (ch == '/') {      endTagDelta = 100;      ch = _is.read();    }        if (! isTagChar(ch))      throw expectedChar("tag", ch);          _sbuf.setLength(0);    for (; isTagChar(ch); ch = read())      _sbuf.append((char) ch);    if (ch != '>')      throw expectedChar("'>'", ch);    Integer value = (Integer) _tagMap.get(_sbuf.toString());    if (value == null)      throw error("Unknown tag <" + _sbuf + ">");    return value.intValue() + endTagDelta;  }  /**   * Returns true if the character is a valid tag character.   */  private boolean isTagChar(int ch)  {    return (ch >= 'a' && ch <= 'z' ||            ch >= 'A' && ch <= 'Z' ||            ch >= '0' && ch <= '9' ||            ch == ':' || ch == '-');  }  protected int skipWhitespace()    throws IOException  {    int ch = read();    for (;         ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';         ch = read()) {    }    return ch;  }  protected boolean isWhitespace(int ch)    throws IOException  {    return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';  }    /**   * Reads bytes from the underlying stream.   */  int read(byte []buffer, int offset, int length)    throws IOException  {    throw new UnsupportedOperationException();  }  int read()    throws IOException  {    if (_peek >= 0) {      int value = _peek;      _peek = -1;      return value;    }    int ch = _is.read();    return ch;  }  public Reader getReader()  {    return null;  }  public InputStream readInputStream()  {    return null;  }  public InputStream getInputStream()  {    return null;  }  protected IOException expectBeginTag(String expect, String tag)  {    return new BurlapProtocolException("expected <" + expect + "> at <" + tag + ">");  }    protected IOException expectedChar(String expect, int ch)  {    if (ch < 0)      return error("expected " + expect + " at end of file");    else      return error("expected " + expect + " at " + (char) ch);  }    protected IOException expectedTag(String expect, int tag)  {    return error("expected " + expect + " at " + tagName(tag));  }    protected IOException error(String message)  {    return new BurlapProtocolException(message);  }  protected static String tagName(int tag)  {    switch (tag) {    case TAG_NULL:      return "<null>";    case TAG_NULL_END:      return "</null>";          case TAG_BOOLEAN:      return "<boolean>";    case TAG_BOOLEAN_END:      return "</boolean>";          case TAG_INT:      return "<int>";    case TAG_INT_END:      return "</int>";          case TAG_LONG:      return "<long>";    case TAG_LONG_END:      return "</long>";          case TAG_DOUBLE:      return "<double>";    case TAG_DOUBLE_END:      return "</double>";          case TAG_STRING:      return "<string>";    case TAG_STRING_END:      return "</string>";          case TAG_XML:      return "<xml>";    case TAG_XML_END:      return "</xml>";          case TAG_BASE64:      return "<base64>";    case TAG_BASE64_END:      return "</base64>";          case TAG_MAP:      return "<map>";    case TAG_MAP_END:      return "</map>";          case TAG_LIST:      return "<list>";    case TAG_LIST_END:      return "</list>";          case TAG_TYPE:      return "<type>";    case TAG_TYPE_END:      return "</type>";          case TAG_LENGTH:      return "<length>";    case TAG_LENGTH_END:      return "</length>";          case TAG_REF:      return "<ref>";    case TAG_REF_END:      return "</ref>";          case TAG_REMOTE:      return "<remote>";    case TAG_REMOTE_END:      return "</remote>";          case TAG_CALL:      return "<burlap:call>";    case TAG_CALL_END:      return "</burlap:call>";          case TAG_REPLY:      return "<burlap:reply>";    case TAG_REPLY_END:      return "</burlap:reply>";          case TAG_HEADER:      return "<header>";    case TAG_HEADER_END:      return "</header>";          case TAG_FAULT:      return "<fault>";    case TAG_FAULT_END:      return "</fault>";    case -1:      return "end of file";    default:      return "unknown " + tag;    }  }        static {    _tagMap = new HashMap();    _tagMap.put("null", new Integer(TAG_NULL));        _tagMap.put("boolean", new Integer(TAG_BOOLEAN));    _tagMap.put("int", new Integer(TAG_INT));    _tagMap.put("long", new Integer(TAG_LONG));    _tagMap.put("double", new Integer(TAG_DOUBLE));        _tagMap.put("date", new Integer(TAG_DATE));        _tagMap.put("string", new Integer(TAG_STRING));    _tagMap.put("xml", new Integer(TAG_XML));    _tagMap.put("base64", new Integer(TAG_BASE64));        _tagMap.put("map", new Integer(TAG_MAP));    _tagMap.put("list", new Integer(TAG_LIST));        _tagMap.put("type", new Integer(TAG_TYPE));    _tagMap.put("length", new Integer(TAG_LENGTH));        _tagMap.put("ref", new Integer(TAG_REF));    _tagMap.put("remote", new Integer(TAG_REMOTE));        _tagMap.put("burlap:call", new Integer(TAG_CALL));    _tagMap.put("burlap:reply", new Integer(TAG_REPLY));    _tagMap.put("fault", new Integer(TAG_FAULT));    _tagMap.put("method", new Integer(TAG_METHOD));    _tagMap.put("header", new Integer(TAG_HEADER));  }    static {    base64Decode = new int[256];    for (int i = 'A'; i <= 'Z'; i++)      base64Decode[i] = i - 'A';    for (int i = 'a'; i <= 'z'; i++)      base64Decode[i] = i - 'a' + 26;    for (int i = '0'; i <= '9'; i++)      base64Decode[i] = i - '0' + 52;    base64Decode['+'] = 62;    base64Decode['/'] = 63;  }  static {    try {      _detailMessageField = Throwable.class.getDeclaredField("detailMessage");      _detailMessageField.setAccessible(true);    } catch (Throwable e) {    }  }}

⌨️ 快捷键说明

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