String.getBytes(),源码之下,了无秘密

 @Deprecated
    public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
        if (srcBegin < 0) {
            throw new StringIndexOutOfBoundsException(srcBegin);
        }
        if (srcEnd > value.length) {
            throw new StringIndexOutOfBoundsException(srcEnd);
        }
        if (srcBegin > srcEnd) {
            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
        }
        int j = dstBegin;
        int n = srcEnd;
        int i = srcBegin;
        char[] val = value;   /* avoid getfield opcode */

        while (i < n) {
            dst[j++] = (byte)val[i++];
        }
    }

    /**
     * Encodes this {@code String} into a sequence of bytes using the named
     * charset, storing the result into a new byte array.
     *
     * <p> The behavior of this method when this string cannot be encoded in
     * the given charset is unspecified.  The {@link
     * java.nio.charset.CharsetEncoder} class should be used when more control
     * over the encoding process is required.
     *
     * @param  charsetName
     *         The name of a supported {@linkplain java.nio.charset.Charset
     *         charset}
     *
     * @return  The resultant byte array
     *
     * @throws  UnsupportedEncodingException
     *          If the named charset is not supported
     *
     * @since  JDK1.1
     */
    public byte[] getBytes(String charsetName)
            throws UnsupportedEncodingException {
        if (charsetName == null) throw new NullPointerException();
        return StringCoding.encode(charsetName, value, 0, value.length);
    }

    /**
     * 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.
     *
     * <p> 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, 0, value.length);
    }

    /**
     * Encodes this {@code String} into a sequence of bytes using the
     * platform‘s default charset, storing the result into a new byte array.
     *
     * <p> The behavior of this method when this string cannot be encoded in
     * the default charset is unspecified.  The {@link
     * java.nio.charset.CharsetEncoder} class should be used when more control
     * over the encoding process is required.
     *
     * @return  The resultant byte array
     *
     * @since      JDK1.1
     */
    public byte[] getBytes() {
        return StringCoding.encode(value, 0, value.length);
    }

只有我一个人觉得JDK里面的代码写得特别跳吗?

时间: 2024-10-19 05:16:15

String.getBytes(),源码之下,了无秘密的相关文章

String部分源码分析

最近想处理上传文件时request包含的混合信息,得到的流可以部分解析成String部分依旧当二进制流处理 于是把BufferedInputStream和String的源码相关部分都读了一下 String是final修饰的类这个大家应该都知道,其中主要的char[] value也是final修饰的 大概看了下字符串驻留结合自己的理解,我试着讲下白话文= =# String a="abc"; 这样是在池内先搜索"abc"如果没有创建一个字符串"abc&quo

LeetCode-Reverse Words in a String[AC源码]

1 package com.lw.leet1; 2 3 import java.util.Stack; 4 5 /** 6 * @ClassName:Solution 7 * @Description: 8 * Reverse Words in a String 9 * Total Accepted: 26194 Total Submissions: 187094 My Submissions 10 * Given an input string, reverse the string word

There is no getter for property named &#39;*&#39; in &#39;class java.lang.String&#39;之源码分析

There is no getter for property named '*' in 'class java.lang.String',此错误之所以出现,是因为mybatis在对parameterType="String"的sql语句做了限制,假如你使用<when test="username != null">这样的条件判断时,就会出现该错误,不过今天我们来刨根问底一下. 一.错误再现 想要追本溯源,就需要错误再现,那么假设我们有这样一个sql查询

java.lang.String 类源码解读

String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public final class String defined String由char[]数组实现 /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the strin

翻String.Format源码发现的新东西:StringBuilderCache

起因: 记不清楚今天是为毛点想F12看String.Format的实现源码了,反正就看到了下图的鸟东西: 瞬间石化有没有,StringBuilder还能这么获取? 研究StringBuilderCache类 下面的事件也简单,果断在StringBuilderCache上面点了F12看源码(Resharpe真是好东西啊...) 首先看到的是这是一个internal的类,怪不得没见有人这么写过呢! 研究一番之后,终于弄清楚这货是干嘛的了:这个类的作用就是缓存一个StringBuilder对象,给那些

String的源码理解(未写完)

String本质上是一个char数组(jdk 9之后是byte数组),并且是一个声明为final的数组,并且String的不可变也是通过这种把数组声明为final来实现的 public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char va

java String部分源码学习记录

public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /**char数组用于字符的存储 */ private final char value[]; /** 缓存string的hash码 */ private int hash; // Default to 0 public String() {/**无参构造函数,打印值为""*/ this.

String 部分源码分析

String 无参数构造函数 /** * 底层存储字符串的目标字节数组, * Jdk 8 之前都是字符数组 private final char[] value; */ @Stable private final byte[] value; /** * 编码底层字节数组的字符集,支持 LATIN1.UTF16 */ private final byte coder; /** * 字符串的哈希码值,默认为 0 */ private int hash; // Default to 0 /** * 创

java String部分源码解析

String类型的成员变量 /** String的属性值 */ private final char value[]; /** The offset is the first index of the storage that is used. */ /**数组被使用的开始位置**/ private final int offset; /** The count is the number of characters in the String. */ /**String中元素的个数**/ pr