Exemplo n.º 1
0
  public boolean equals(MessageBytes mb) {
    switch (type) {
      case T_STR:
        return mb.equals(strValue);
    }

    if (mb.type != T_CHARS && mb.type != T_BYTES) {
      // it's a string or int/date string value
      return equals(mb.toString());
    }

    // mb is either CHARS or BYTES.
    // this is either CHARS or BYTES
    // Deal with the 4 cases ( in fact 3, one is simetric)

    if (mb.type == T_CHARS && type == T_CHARS) {
      return charC.equals(mb.charC);
    }
    if (mb.type == T_BYTES && type == T_BYTES) {
      return byteC.equals(mb.byteC);
    }
    if (mb.type == T_CHARS && type == T_BYTES) {
      return byteC.equals(mb.charC);
    }
    if (mb.type == T_BYTES && type == T_CHARS) {
      return mb.byteC.equals(charC);
    }
    // can't happen
    return true;
  }
Exemplo n.º 2
0
 /**
  * Set the encoding. If the object was constructed from bytes[]. any previous conversion is reset.
  * If no encoding is set, we'll use 8859-1.
  */
 public void setEncoding(String enc) {
   if (!byteC.isNull()) {
     // if the encoding changes we need to reset the converion results
     charC.recycle();
     hasStrValue = false;
   }
   byteC.setEncoding(enc);
 }
Exemplo n.º 3
0
 /**
  * Set the encoding. If the object was constructed from bytes[]. any previous conversion is reset.
  * If no encoding is set, we'll use 8859-1.
  */
 public void setCharset(Charset charset) {
   if (!byteC.isNull()) {
     // if the encoding changes we need to reset the conversion results
     charC.recycle();
     hasStrValue = false;
   }
   byteC.setCharset(charset);
 }
Exemplo n.º 4
0
 /** Unimplemented yet. Do a char->byte conversion. */
 public void toBytes() {
   if (!byteC.isNull()) {
     type = T_BYTES;
     return;
   }
   toString();
   type = T_BYTES;
   byte bb[] = strValue.getBytes();
   byteC.setBytes(bb, 0, bb.length);
 }
Exemplo n.º 5
0
 /**
  * Sets the content to the specified subarray of bytes.
  *
  * @param b the bytes
  * @param off the start offset of the bytes
  * @param len the length of the bytes
  */
 public void setBytes(byte[] b, int off, int len) {
   byteC.setBytes(b, off, len);
   type = T_BYTES;
   hasStrValue = false;
   hasHashCode = false;
   hasIntValue = false;
   hasLongValue = false;
   hasDateValue = false;
 }
Exemplo n.º 6
0
 /**
  * Returns the length of the original buffer. Note that the length in bytes may be different from
  * the length in chars.
  */
 public int getLength() {
   if (type == T_BYTES) return byteC.getLength();
   if (type == T_CHARS) {
     return charC.getLength();
   }
   if (type == T_STR) return strValue.length();
   toString();
   if (strValue == null) return 0;
   return strValue.length();
 }
Exemplo n.º 7
0
 /** Copy the src into this MessageBytes, allocating more space if needed */
 public void duplicate(MessageBytes src) throws IOException {
   switch (src.getType()) {
     case MessageBytes.T_BYTES:
       type = T_BYTES;
       ByteChunk bc = src.getByteChunk();
       byteC.allocate(2 * bc.getLength(), -1);
       byteC.append(bc);
       break;
     case MessageBytes.T_CHARS:
       type = T_CHARS;
       CharChunk cc = src.getCharChunk();
       charC.allocate(2 * cc.getLength(), -1);
       charC.append(cc);
       break;
     case MessageBytes.T_STR:
       type = T_STR;
       String sc = src.getString();
       this.setString(sc);
       break;
   }
 }
Exemplo n.º 8
0
 /**
  * Returns true if the message bytes starts with the specified string.
  *
  * @param s the string
  */
 public boolean startsWith(String s) {
   switch (type) {
     case T_STR:
       return strValue.startsWith(s);
     case T_CHARS:
       return charC.startsWith(s);
     case T_BYTES:
       return byteC.startsWith(s);
     default:
       return false;
   }
 }
Exemplo n.º 9
0
 /**
  * Returns true if the message bytes starts with the specified string.
  *
  * @param c the character
  * @param starting The start position
  */
 public int indexOf(char c, int starting) {
   switch (type) {
     case T_STR:
       return strValue.indexOf(c, starting);
     case T_CHARS:
       return charC.indexOf(c, starting);
     case T_BYTES:
       return byteC.indexOf(c, starting);
     default:
       return -1;
   }
 }
Exemplo n.º 10
0
 /** Set the buffer to the representation of an long */
 public void setLong(long l) {
   byteC.allocate(32, 64);
   long current = l;
   byte[] buf = byteC.getBuffer();
   int start = 0;
   int end = 0;
   if (l == 0) {
     buf[end++] = (byte) '0';
   }
   if (l < 0) {
     current = -l;
     buf[end++] = (byte) '-';
   }
   while (current > 0) {
     int digit = (int) (current % 10);
     current = current / 10;
     buf[end++] = HexUtils.HEX[digit];
   }
   byteC.setOffset(0);
   byteC.setEnd(end);
   // Inverting buffer
   end--;
   if (l < 0) {
     start++;
   }
   while (end > start) {
     byte temp = buf[start];
     buf[start] = buf[end];
     buf[end] = temp;
     start++;
     end--;
   }
   longValue = l;
   hasStrValue = false;
   hasHashCode = false;
   hasIntValue = false;
   hasLongValue = true;
   hasDateValue = false;
   type = T_BYTES;
 }
Exemplo n.º 11
0
 /**
  * Compares the message bytes to the specified String object.
  *
  * @param s the String to compare
  * @return true if the comparison succeeded, false otherwise
  */
 public boolean equalsIgnoreCase(String s) {
   switch (type) {
     case T_STR:
       if (strValue == null && s != null) return false;
       return strValue.equalsIgnoreCase(s);
     case T_CHARS:
       return charC.equalsIgnoreCase(s);
     case T_BYTES:
       return byteC.equalsIgnoreCase(s);
     default:
       return false;
   }
 }
Exemplo n.º 12
0
  /** Convert the buffer to an int, cache the value */
  public int getInt() {
    if (hasIntValue) return intValue;

    switch (type) {
      case T_BYTES:
        intValue = byteC.getInt();
        break;
      default:
        intValue = Integer.parseInt(toString());
    }
    hasIntValue = true;
    return intValue;
  }
Exemplo n.º 13
0
  /** Resets the message bytes to an uninitialized (NULL) state. */
  public void recycle() {
    type = T_NULL;
    byteC.recycle();
    charC.recycle();

    strValue = null;
    caseSensitive = true;

    hasStrValue = false;
    hasHashCode = false;
    hasIntValue = false;
    hasLongValue = false;
    hasDateValue = false;
  }
Exemplo n.º 14
0
  /** Convert the buffer to an long, cache the value */
  public long getLong() {
    if (hasLongValue) return longValue;

    switch (type) {
      case T_BYTES:
        longValue = byteC.getLong();
        break;
      default:
        longValue = Long.parseLong(toString());
    }

    hasLongValue = true;
    return longValue;
  }
Exemplo n.º 15
0
  /** Compute the string value */
  public String toString() {
    if (hasStrValue) return strValue;

    switch (type) {
      case T_CHARS:
        strValue = charC.toString();
        hasStrValue = true;
        return strValue;
      case T_BYTES:
        strValue = byteC.toString();
        hasStrValue = true;
        return strValue;
    }
    return null;
  }
Exemplo n.º 16
0
 // hash ignoring case
 private int hashIgnoreCase() {
   int code = 0;
   switch (type) {
     case T_STR:
       for (int i = 0; i < strValue.length(); i++) {
         code = code * 37 + Ascii.toLower(strValue.charAt(i));
       }
       return code;
     case T_CHARS:
       return charC.hashIgnoreCase();
     case T_BYTES:
       return byteC.hashIgnoreCase();
     default:
       return 0;
   }
 }
Exemplo n.º 17
0
 // normal hash.
 private int hash() {
   int code = 0;
   switch (type) {
     case T_STR:
       // We need to use the same hash function
       for (int i = 0; i < strValue.length(); i++) {
         code = code * 37 + strValue.charAt(i);
       }
       return code;
     case T_CHARS:
       return charC.hash();
     case T_BYTES:
       return byteC.hash();
     default:
       return 0;
   }
 }
Exemplo n.º 18
0
  /**
   * Returns true if the message bytes starts with the specified string.
   *
   * @param s the string
   * @param pos The start position
   */
  public boolean startsWithIgnoreCase(String s, int pos) {
    switch (type) {
      case T_STR:
        if (strValue == null) return false;
        if (strValue.length() < pos + s.length()) return false;

        for (int i = 0; i < s.length(); i++) {
          if (Ascii.toLower(s.charAt(i)) != Ascii.toLower(strValue.charAt(pos + i))) {
            return false;
          }
        }
        return true;
      case T_CHARS:
        return charC.startsWithIgnoreCase(s, pos);
      case T_BYTES:
        return byteC.startsWithIgnoreCase(s, pos);
      default:
        return false;
    }
  }
Exemplo n.º 19
0
 public boolean isNull() {
   //		should we check also hasStrValue ???
   return byteC.isNull() && charC.isNull() && !hasStrValue;
   // bytes==null && strValue==null;
 }