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

📄 string.java

📁 linux下编程用 编译软件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
   * @throws NullPointerException if data or encoding is null   * @throws UnsupportedEncodingException if encoding is not found   * @throws Error if the decoding fails   * @see #String(byte[], int, int, String)   * @since 1.1   */  public String(byte[] data, String encoding)    throws UnsupportedEncodingException  {    this(data, 0, data.length, encoding);  }  /**   * Creates a new String using the portion of the byte array starting at the   * offset and ending at offset + count. Uses the encoding of the platform's   * default charset, so the resulting string may be longer or shorter than   * the byte array. For more decoding control, use   * {@link java.nio.charset.CharsetDecoder}.  The behavior is not specified   * if the decoder encounters invalid characters; this implementation throws   * an Error.   *   * @param data byte array to copy   * @param offset the offset to start at   * @param count the number of bytes in the array to use   * @throws NullPointerException if data is null   * @throws IndexOutOfBoundsException if offset or count is incorrect   * @throws Error if the decoding fails   * @see #String(byte[], int, int, String)   * @since 1.1   */  public String(byte[] data, int offset, int count)  {    if (offset < 0)      throw new StringIndexOutOfBoundsException("offset: " + offset);    if (count < 0)      throw new StringIndexOutOfBoundsException("count: " + count);    if (offset + count < 0 || offset + count > data.length)      throw new StringIndexOutOfBoundsException("offset + count: "						+ (offset + count));    int o, c;    char[] v;    String encoding;    try 	{	  encoding = System.getProperty("file.encoding");	  CharsetDecoder csd = Charset.forName(encoding).newDecoder();	  csd.onMalformedInput(CodingErrorAction.REPLACE);	  csd.onUnmappableCharacter(CodingErrorAction.REPLACE);	  CharBuffer cbuf = csd.decode(ByteBuffer.wrap(data, offset, count));	  if(cbuf.hasArray())	    {              v = cbuf.array();	      o = cbuf.position();	      c = cbuf.remaining();	    } else {	      // Doubt this will happen. But just in case.	      v = new char[cbuf.remaining()];	      cbuf.get(v);	      o = 0;	      c = v.length;	    }	} catch(Exception ex){	    // If anything goes wrong (System property not set,	    // NIO provider not available, etc)	    // Default to the 'safe' encoding ISO8859_1	    v = new char[count];	    o = 0;	    c = count;	    for (int i=0;i<count;i++)	      v[i] = (char)data[offset+i];	}    this.value = v;    this.offset = o;    this.count = c;  }  /**   * Creates a new String using the byte array. Uses the encoding of the   * platform's default charset, so the resulting string may be longer or   * shorter than the byte array. For more decoding control, use   * {@link java.nio.charset.CharsetDecoder}.  The behavior is not specified   * if the decoder encounters invalid characters; this implementation throws   * an Error.   *   * @param data byte array to copy   * @throws NullPointerException if data is null   * @throws Error if the decoding fails   * @see #String(byte[], int, int)   * @see #String(byte[], int, int, String)   * @since 1.1   */  public String(byte[] data)  {    this(data, 0, data.length);  }  /**   * Creates a new String using the character sequence represented by   * the StringBuffer. Subsequent changes to buf do not affect the String.   *   * @param buffer StringBuffer to copy   * @throws NullPointerException if buffer is null   */  public String(StringBuffer buffer)  {    synchronized (buffer)      {        offset = 0;        count = buffer.count;        // Share unless buffer is 3/4 empty.        if ((count << 2) < buffer.value.length)          {            value = new char[count];            VMSystem.arraycopy(buffer.value, 0, value, 0, count);          }        else          {            buffer.shared = true;            value = buffer.value;          }      }  }  /**   * Creates a new String using the character sequence represented by   * the StringBuilder. Subsequent changes to buf do not affect the String.   *   * @param buffer StringBuilder to copy   * @throws NullPointerException if buffer is null   */  public String(StringBuilder buffer)  {    this(buffer.value, 0, buffer.count);  }  /**   * Special constructor which can share an array when safe to do so.   *   * @param data the characters to copy   * @param offset the location to start from   * @param count the number of characters to use   * @param dont_copy true if the array is trusted, and need not be copied   * @throws NullPointerException if chars is null   * @throws StringIndexOutOfBoundsException if bounds check fails   */  String(char[] data, int offset, int count, boolean dont_copy)  {    if (offset < 0)      throw new StringIndexOutOfBoundsException("offset: " + offset);    if (count < 0)      throw new StringIndexOutOfBoundsException("count: " + count);    if (offset + count < 0 || offset + count > data.length)      throw new StringIndexOutOfBoundsException("offset + count: "						+ (offset + count));    if (dont_copy)      {        value = data;        this.offset = offset;      }    else      {        value = new char[count];        VMSystem.arraycopy(data, offset, value, 0, count);        this.offset = 0;      }    this.count = count;  }  /**   * Returns the number of characters contained in this String.   *   * @return the length of this String   */  public int length()  {    return count;  }  /**   * Returns the character located at the specified index within this String.   *   * @param index position of character to return (base 0)   * @return character located at position index   * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= length()   *         (while unspecified, this is a StringIndexOutOfBoundsException)   */  public char charAt(int index)  {    if (index < 0 || index >= count)      throw new StringIndexOutOfBoundsException(index);    return value[offset + index];  }  /**   * Get the code point at the specified index.  This is like #charAt(int),   * but if the character is the start of a surrogate pair, and the   * following character completes the pair, then the corresponding   * supplementary code point is returned.   * @param index the index of the codepoint to get, starting at 0   * @return the codepoint at the specified index   * @throws IndexOutOfBoundsException if index is negative or &gt;= length()   * @since 1.5   */  public synchronized int codePointAt(int index)  {    // Use the CharSequence overload as we get better range checking    // this way.    return Character.codePointAt(this, index);  }  /**   * Get the code point before the specified index.  This is like   * #codePointAt(int), but checks the characters at <code>index-1</code> and   * <code>index-2</code> to see if they form a supplementary code point.   * @param index the index just past the codepoint to get, starting at 0   * @return the codepoint at the specified index   * @throws IndexOutOfBoundsException if index is negative or &gt;= length()   *         (while unspecified, this is a StringIndexOutOfBoundsException)   * @since 1.5   */  public synchronized int codePointBefore(int index)  {    // Use the CharSequence overload as we get better range checking    // this way.    return Character.codePointBefore(this, index);  }  /**   * Copies characters from this String starting at a specified start index,   * ending at a specified stop index, to a character array starting at   * a specified destination begin index.   *   * @param srcBegin index to begin copying characters from this String   * @param srcEnd index after the last character to be copied from this String   * @param dst character array which this String is copied into   * @param dstBegin index to start writing characters into dst   * @throws NullPointerException if dst is null   * @throws IndexOutOfBoundsException if any indices are out of bounds   *         (while unspecified, source problems cause a   *         StringIndexOutOfBoundsException, and dst problems cause an   *         ArrayIndexOutOfBoundsException)   */  public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)  {    if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > count)      throw new StringIndexOutOfBoundsException();    VMSystem.arraycopy(value, srcBegin + offset,                     dst, dstBegin, srcEnd - srcBegin);  }  /**   * Copies the low byte of each character from this String starting at a   * specified start index, ending at a specified stop index, to a byte array   * starting at a specified destination begin index.   *   * @param srcBegin index to being copying characters from this String   * @param srcEnd index after the last character to be copied from this String   * @param dst byte array which each low byte of this String is copied into   * @param dstBegin index to start writing characters into dst   * @throws NullPointerException if dst is null and copy length is non-zero   * @throws IndexOutOfBoundsException if any indices are out of bounds   *         (while unspecified, source problems cause a   *         StringIndexOutOfBoundsException, and dst problems cause an   *         ArrayIndexOutOfBoundsException)   * @see #getBytes()   * @see #getBytes(String)   * @deprecated use {@link #getBytes()}, which uses a char to byte encoder   */  public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin)  {    if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > count)      throw new StringIndexOutOfBoundsException();    int i = srcEnd - srcBegin;    srcBegin += offset;    while (--i >= 0)      dst[dstBegin++] = (byte) value[srcBegin++];  }  /**   * Converts the Unicode characters in this String to a byte array. Uses the   * specified encoding method, so the result may be longer or shorter than   * the String. For more encoding control, use   * {@link java.nio.charset.CharsetEncoder}, and for valid character sets,   * see {@link java.nio.charset.Charset}. Unsupported characters get   * replaced by an encoding specific byte.   *   * @param enc encoding name   * @return the resulting byte array   * @throws NullPointerException if enc is null   * @throws UnsupportedEncodingException if encoding is not supported   * @since 1.1   */  public byte[] getBytes(String enc) throws UnsupportedEncodingException  {    try       {	CharsetEncoder cse = Charset.forName(enc).newEncoder();	cse.onMalformedInput(CodingErrorAction.REPLACE);	cse.onUnmappableCharacter(CodingErrorAction.REPLACE);	ByteBuffer bbuf = cse.encode(CharBuffer.wrap(value, offset, count));	if(bbuf.hasArray())	  return bbuf.array();		// Doubt this will happen. But just in case.	byte[] bytes = new byte[bbuf.remaining()];	bbuf.get(bytes);	return bytes;      }     catch(IllegalCharsetNameException e)      {	throw new UnsupportedEncodingException("Encoding: " + enc					       + " not found.");      }     catch(UnsupportedCharsetException e)      {	throw new UnsupportedEncodingException("Encoding: " + enc					       + " not found.");      }     catch(CharacterCodingException e)      {	// This shouldn't ever happen.	throw (InternalError) new InternalError().initCause(e);      }	    }  /**   * Converts the Unicode characters in this String to a byte array. Uses the   * encoding of the platform's default charset, so the result may be longer   * or shorter than the String. For more encoding control, use   * {@link java.nio.charset.CharsetEncoder}. Unsupported characters get   * replaced by an encoding specific byte.   *   * @return the resulting byte array, or null on a problem   * @since 1.1   */  public byte[] getBytes()  {       try 	  {	      return getBytes(System.getProperty("file.encoding"));	  } catch(Exception e) {	      // XXX - Throw an error here? 	      // For now, default to the 'safe' encoding.	      byte[] bytes = new byte[count];	      for(int i=0;i<count;i++)		  bytes[i] = (byte)((value[offset+i] <= 0xFF)?				    value[offset+i]:'?');	      return bytes;      }  }  /**   * Predicate which compares anObject to this. This is true only for Strings   * with the same character sequence.   *   * @param anObject the object to compare   * @return true if anObject is semantically equal to this   * @see #compareTo(String)   * @see #equalsIgnoreCase(String)   */  public boolean equals(Object anObject)  {    if (! (anObject instanceof String))      return false;    String str2 = (String) anObject;    if (count != str2.count)      return false;    if (value == str2.value && offset == str2.offset)      return true;    int i = count;    int x = offset;    int y = str2.offset;    while (--i >= 0)      if (value[x++] != str2.value[y++])        return false;    return true;  }  /**   * Compares the given StringBuffer to this String. This is true if the   * StringBuffer has the same content as this String at this moment.   *   * @param buffer the StringBuffer to compare to   * @return true if StringBuffer has the same character sequence

⌨️ 快捷键说明

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