---
title: "String补漏"
author: "dyingcow"
date: "2007-06-05T07:32:36Z"
tags: ["CSS"]
slug: "string"
draft: false
lastmod: "2007-06-05T07:32:36Z"
id: 102986
url: "https://dyingcow.iteye.com/blog/102986"
---

&lt;link rel="stylesheet" type="text/css" href="http://liushi.hitidea.org/plugins/plogeshi/styles/plogeshi.css"&gt;

补漏语：

    String内部用char\[]来储存数据，数组操作。获取byte时通过StringCoding类进行转换。

源码：

    **String**

 **/\*\***  
     *** Encodes this {@code String} into a sequence of bytes using the given**  
     *** {@linkplain java.nio.charset.Charset charset}, storing the result into a**  
     *** new byte array.**  
     **\***  
     *** &lt;p&gt; This method always replaces malformed-input and unmappable-character**  
     *** sequences with this charset's default replacement byte array.  The**  
     *** {@link java.nio.charset.CharsetEncoder} class should be used when more**  
     *** control over the encoding process is required.**  
     **\***  
     *** @param  charset**  
     ***         The {@linkplain java.nio.charset.Charset} to be used to encode**  
     ***         the {@code String}**  
     **\***  
     *** @return  The resultant byte array**  
     **\***  
     *** @since  1.6**  
     **\*/**  
    public byte\[] getBytes(Charset charset) {  
    if (charset == null) throw new NullPointerException();  
    return StringCoding.encode(charset, value, offset, count);  
    }

 

 **/\*\***  
     *** Copies characters from this string into the destination character**  
     *** array.**  
     *** &lt;p&gt;**  
     *** The first character to be copied is at index &lt;code&gt;srcBegin&lt;/code&gt;;**  
     *** the last character to be copied is at index &lt;code&gt;srcEnd-1&lt;/code&gt;**  
     *** (thus the total number of characters to be copied is**  
     *** &lt;code&gt;srcEnd-srcBegin&lt;/code&gt;). The characters are copied into the**  
     *** subarray of &lt;code&gt;dst&lt;/code&gt; starting at index &lt;code&gt;dstBegin&lt;/code&gt;**  
     *** and ending at index:**  
     *** &lt;p&gt;&lt;blockquote&gt;&lt;pre&gt;**  
     ***     dstbegin + (srcEnd-srcBegin) - 1**  
     *** &lt;/pre&gt;&lt;/blockquote&gt;**  
     **\***  
     *** @param      srcBegin   index of the first character in the string**  
     ***                        to copy.**  
     *** @param      srcEnd     index after the last character in the string**  
     ***                        to copy.**  
     *** @param      dst        the destination array.**  
     *** @param      dstBegin   the start offset in the destination array.**  
     *** @exception IndexOutOfBoundsException If any of the following**  
     ***            is true:**  
     ***            &lt;ul&gt;&lt;li&gt;&lt;code&gt;srcBegin&lt;/code&gt; is negative.**  
     ***            &lt;li&gt;&lt;code&gt;srcBegin&lt;/code&gt; is greater than &lt;code&gt;srcEnd&lt;/code&gt;**  
     ***            &lt;li&gt;&lt;code&gt;srcEnd&lt;/code&gt; is greater than the length of this**  
     ***                string**  
     ***            &lt;li&gt;&lt;code&gt;dstBegin&lt;/code&gt; is negative**  
     ***            &lt;li&gt;&lt;code&gt;dstBegin+(srcEnd-srcBegin)&lt;/code&gt; is larger than**  
     ***                &lt;code&gt;dst.length&lt;/code&gt;&lt;/ul&gt;**  
     **\*/**  
    public void getChars(int srcBegin, int srcEnd, char dst\[], int dstBegin) {  
        if (srcBegin &lt; 0) {  
            throw new StringIndexOutOfBoundsException(srcBegin);  
        }  
        if (srcEnd &gt; count) {  
            throw new StringIndexOutOfBoundsException(srcEnd);  
        }  
        if (srcBegin &gt; srcEnd) {  
            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);  
        }  
        System.arraycopy(value, offset + srcBegin, dst, dstBegin,  
             srcEnd - srcBegin);  
    }

 

    **StringCoding**

    char\[] decode(byte\[] ba, int off, int len) {  
        int en = scale(len, cd.maxCharsPerByte());  
        char\[] ca = new char\[en];  
        if (len == 0)  
        return ca;  
        cd.reset();  
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);  
        CharBuffer cb = CharBuffer.wrap(ca);  
        try {  
        CoderResult cr = cd.decode(bb, cb, true);  
        if (!cr.isUnderflow())  
            cr.throwException();  
        cr = cd.flush(cb);  
        if (!cr.isUnderflow())  
            cr.throwException();  
        } catch (CharacterCodingException x) {  
        // Substitution is always enabled,  
        // so this shouldn't happen  
        throw new Error(x);  
        }  
        return safeTrim(ca, cb.position(), cs);  
    }

    }