string.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,934 行 · 第 1/5 页
JAVA
1,934 行
* @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); // equivalent to: offset + count < 0 || offset + count > data.length if (data.length - offset < count) 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); // equivalent to: offset + count < 0 || offset + count > data.length if (data.length - offset < count) 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; } /** * Creates a new String containing the characters represented in the * given subarray of Unicode code points. * @param codePoints the entire array of code points * @param offset the start of the subarray * @param count the length of the subarray * * @throws IllegalArgumentException if an invalid code point is found * in the codePoints array * @throws IndexOutOfBoundsException if offset is negative or offset + count * is greater than the length of the array. */ public String(int[] codePoints, int offset, int count) { // FIXME: This implementation appears to give correct internal // representation of the String because: // - length() is correct // - getting a char[] from toCharArray() and testing // Character.codePointAt() on all the characters in that array gives // the appropriate results // however printing the String gives incorrect results. This may be // due to printing method errors (such as incorrectly looping through // the String one char at a time rather than one "character" at a time. if (offset < 0) throw new IndexOutOfBoundsException(); int end = offset + count; int pos = 0; // This creates a char array that is long enough for all of the code // points to represent supplementary characters. This is more than likely // a waste of storage, so we use it only temporarily and then copy the // used portion into the value array. char[] temp = new char[2 * codePoints.length]; for (int i = offset; i < end; i++) { pos += Character.toChars(codePoints[i], temp, pos); } this.count = pos; this.value = new char[pos]; System.arraycopy(temp, 0, value, 0, pos); this.offset = 0; } /** * 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 < 0 || index >= 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 >= 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 >= 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.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?