Package gnu.java.lang

Examples of gnu.java.lang.CPStringBuilder


   * @return the String representation of the Field
   */
  public String toString()
  {
    // 64 is a reasonable buffer initial size for field
    CPStringBuilder sb = new CPStringBuilder(64);
    Modifier.toString(getModifiers(), sb).append(' ');
    sb.append(ClassHelper.getUserName(getType())).append(' ');
    sb.append(getDeclaringClass().getName()).append('.');
    sb.append(getName());
    return sb.toString();
  }
View Full Code Here


    /**
     * Helper method for encoding an array of bytes as a Base64 String.
     */
    private static String encode64(byte[] b) {
        CPStringBuilder sb = new CPStringBuilder((b.length/3)*4);

        int i = 0;
        int remaining = b.length;
        char c[] = new char[4];
        while (remaining > 0) {
            // Three input bytes are encoded as four chars (6 bits) as
            // 00000011 11112222 22333333

            c[0] = (char) ((b[i] & 0xFC) >> 2);
            c[1] = (char) ((b[i] & 0x03) << 4);
            if (remaining >= 2) {
                c[1] += (char) ((b[i+1] & 0xF0) >> 4);
                c[2] = (char) ((b[i+1] & 0x0F) << 2);
                if (remaining >= 3) {
                    c[2] += (char) ((b[i+2] & 0xC0) >> 6);
                    c[3] = (char) (b[i+2] & 0x3F);
                } else {
                    c[3] = 64;
                }
            } else {
                c[2] = 64;
                c[3] = 64;
            }

            // Convert to base64 chars
            for(int j = 0; j < 4; j++) {
                if (c[j] < 26) {
                    c[j] += 'A';
                } else if (c[j] < 52) {
                    c[j] = (char) (c[j] - 26 + 'a');
                } else if (c[j] < 62) {
                    c[j] = (char) (c[j] - 52 + '0');
                } else if (c[j] == 62) {
                    c[j] = '+';
                } else if (c[j] == 63) {
                    c[j] = '/';
                } else {
                    c[j] = '=';
                }
            }

            sb.append(c);
            i += 3;
            remaining -= 3;
        }

        return sb.toString();
    }
View Full Code Here

    return sb.toString();
  }

  public String toGenericString()
  {
    CPStringBuilder sb = new CPStringBuilder(64);
    Modifier.toString(getModifiers(), sb).append(' ');
    sb.append(getGenericType()).append(' ');
    sb.append(getDeclaringClass().getName()).append('.');
    sb.append(getName());
    return sb.toString();
  }
View Full Code Here

    // Then go through the whole string looking for byte encoded characters
    int i;
    int start = 0;
    byte[] bytes = null;
    int length = str.length();
    CPStringBuilder result = new CPStringBuilder(length);
    while ((i = str.indexOf('%', start)) >= 0)
      {
  // Add all non-encoded characters to the result buffer
  result.append(str.substring(start, i));
  start = i;

  // Get all consecutive encoded bytes
  while ((i + 2 < length) && (str.charAt(i) == '%'))
    i += 3;

  // Decode all these bytes
  if ((bytes == null) || (bytes.length < ((i - start) / 3)))
    bytes = new byte[((i - start) / 3)];

  int index = 0;
  try
    {
      while (start < i)
        {
    String sub = str.substring(start + 1, start + 3);
    bytes[index] = (byte) Integer.parseInt(sub, 16);
    index++;
    start += 3;
        }
    }
  catch (NumberFormatException nfe)
    {
      // One of the hex encoded strings was bad
    }

  // Add the bytes as characters according to the given encoding
  result.append(new String(bytes, 0, index, encoding));

  // Make sure we skip to just after a % sign
  // There might not have been enough encoded characters after the %
  // or the hex chars were not actually hex chars (NumberFormatException)
  if (start < length && s.charAt(start) == '%')
    {
      result.append('%');
      start++;
    }
      }

    // Add any characters left
    if (start < str.length())
      result.append(str.substring(start));

    return result.toString();
  }
View Full Code Here

      return "NaN";
    if (isInfinite(f))
      return f < 0 ? "-Infinity" : "Infinity";

    int bits = floatToIntBits(f);
    CPStringBuilder result = new CPStringBuilder();
   
    if (bits < 0)
      result.append('-');
    result.append("0x");

    final int mantissaBits = 23;
    final int exponentBits = 8;
    int mantMask = (1 << mantissaBits) - 1;
    int mantissa = bits & mantMask;
    int expMask = (1 << exponentBits) - 1;
    int exponent = (bits >>> mantissaBits) & expMask;

    result.append(exponent == 0 ? '0' : '1');
    result.append('.');
    // For Float only, we have to adjust the mantissa.
    mantissa <<= 1;
    result.append(Integer.toHexString(mantissa));
    if (exponent == 0 && mantissa != 0)
      {
        // Treat denormal specially by inserting '0's to make
        // the length come out right.  The constants here are
        // to account for things like the '0x'.
        int offset = 4 + ((bits < 0) ? 1 : 0);
        // The silly +3 is here to keep the code the same between
        // the Float and Double cases.  In Float the value is
        // not a multiple of 4.
        int desiredLength = offset + (mantissaBits + 3) / 4;
        while (result.length() < desiredLength)
          result.insert(offset, '0');
      }
    result.append('p');
    if (exponent == 0 && mantissa == 0)
      {
        // Zero, so do nothing special.
      }
    else
      {
        // Apply bias.
        boolean denormal = exponent == 0;
        exponent -= (1 << (exponentBits - 1)) - 1;
        // Handle denormal.
        if (denormal)
          ++exponent;
      }

    result.append(Integer.toString(exponent));
    return result.toString();
  }
View Full Code Here

  {
    // Validate some arguments
    if ((begin < 0) || (end < begin) || end > aci.getEndIndex())
      throw new IllegalArgumentException("Bad index values");

    CPStringBuilder sb = new CPStringBuilder("");

    // Get the valid attribute list
    Set allAttribs = aci.getAllAttributeKeys();
    if (attributes != null)
      allAttribs.retainAll(Arrays.asList(attributes));

    // Loop through and extract the attributes
    char c = aci.setIndex(begin);

    ArrayList accum = new ArrayList();
    do
      {
        sb.append(c);

        Iterator iter = allAttribs.iterator();
        while(iter.hasNext())
          {
            Object obj = iter.next();

            // What should we do if this is not true?
            if (!(obj instanceof AttributedCharacterIterator.Attribute))
              continue;

            AttributedCharacterIterator.Attribute attrib =
              (AttributedCharacterIterator.Attribute)obj;

            // Make sure the attribute is defined.
            Object attribObj = aci.getAttribute(attrib);
            if (attribObj == null)
              continue;
            int rl = aci.getRunLimit(attrib);
            if (rl > end)
              rl = end;
            rl -= begin;

            // Check to see if we already processed this one
            int rs = aci.getRunStart(attrib);
            if ((rs < aci.getIndex()) && (aci.getIndex() != begin))
              continue;

            // If the attribute run starts before the beginning index, we
            // need to junk it if it is an Annotation.
            rs -= begin;
            if (rs < 0)
              {
                if (attribObj instanceof Annotation)
                   continue;

                rs = 0;
              }

            // Create a map object.  Yes this will only contain one attribute
            Map newMap = new Hashtable();
            newMap.put(attrib, attribObj);

            // Add it to the attribute list.
            accum.add(new AttributeRange(newMap, rs, rl));
          }

        c = aci.next();
      }
    while( aci.getIndex() < end );

    attribs = new AttributeRange[accum.size()];
    attribs = (AttributeRange[]) accum.toArray(attribs);

    sci = new StringCharacterIterator(sb.toString());
  }
View Full Code Here

  {
    int length = s.length();
    int start = 0;
    int i = 0;

    CPStringBuilder result = new CPStringBuilder(length);
    while (true)
      {
  while (i < length && isSafe(s.charAt(i)))
    i++;

  // Safe character can just be added
  result.append(s.substring(start, i));

  // Are we done?
  if (i >= length)
    return result.toString();
  else if (s.charAt(i) == ' ')
    {
      result.append('+'); // Replace space char with plus symbol.
      i++;
    }
  else
    {
      // Get all unsafe characters
      start = i;
      char c;
      while (i < length && (c = s.charAt(i)) != ' ' && ! isSafe(c))
        i++;

      // Convert them to %XY encoded strings
      String unsafe = s.substring(start, i);
      byte[] bytes = unsafe.getBytes(encoding);
      for (int j = 0; j < bytes.length; j++)
        {
    result.append('%');
    int val = bytes[j];
    result.append(hex.charAt((val & 0xf0) >> 4));
    result.append(hex.charAt(val & 0x0f));
        }
    }
  start = i;
      }
  }
View Full Code Here

    if (words == null)
      return Integer.toString(ival, radix);
    if (ival <= 2)
      return Long.toString(longValue(), radix);
    int buf_size = ival * (MPN.chars_per_word(radix) + 1);
    CPStringBuilder buffer = new CPStringBuilder(buf_size);
    format(radix, buffer);
    return buffer.toString();
  }
View Full Code Here

        // The characters up to the next Whitespace, ':', or '='
        // describe the key.  But look for escape sequences.
  // Try to short-circuit when there is no escape char.
  int start = pos;
  boolean needsEscape = line.indexOf('\\', pos) != -1;
        CPStringBuilder key = needsEscape ? new CPStringBuilder() : null;
        while (pos < line.length()
               && ! Character.isWhitespace(c = line.charAt(pos++))
               && c != '=' && c != ':')
          {
            if (needsEscape && c == '\\')
              {
                if (pos == line.length())
                  {
                    // The line continues on the next line.  If there
                    // is no next line, just treat it as a key with an
                    // empty value.
                    line = reader.readLine();
        if (line == null)
          line = "";
                    pos = 0;
                    while (pos < line.length()
                           && Character.isWhitespace(c = line.charAt(pos)))
                      pos++;
                  }
                else
                  {
                    c = line.charAt(pos++);
                    switch (c)
                      {
                      case 'n':
                        key.append('\n');
                        break;
                      case 't':
                        key.append('\t');
                        break;
                      case 'r':
                        key.append('\r');
                        break;
                      case 'u':
                        if (pos + 4 <= line.length())
                          {
                            char uni = (char) Integer.parseInt
                              (line.substring(pos, pos + 4), 16);
                            key.append(uni);
                            pos += 4;
                          }        // else throw exception?
                        break;
                      default:
                        key.append(c);
                        break;
                      }
                  }
              }
            else if (needsEscape)
              key.append(c);
          }

        boolean isDelim = (c == ':' || c == '=');

  String keyString;
  if (needsEscape)
    keyString = key.toString();
  else if (isDelim || Character.isWhitespace(c))
    keyString = line.substring(start, pos - 1);
  else
    keyString = line.substring(start, pos);
View Full Code Here

      writer.println("#" + header);
    writer.println ("#" + Calendar.getInstance ().getTime ());
   
    Iterator iter = entrySet ().iterator ();
    int i = size ();
    CPStringBuilder s = new CPStringBuilder (); // Reuse the same buffer.
    while (--i >= 0)
      {
        Map.Entry entry = (Map.Entry) iter.next ();
        formatForOutput ((String) entry.getKey (), s, true);
        s.append ('=');
        formatForOutput ((String) entry.getValue (), s, false);
        writer.println (s);
      }

    writer.flush ();
View Full Code Here

TOP

Related Classes of gnu.java.lang.CPStringBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.