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里面的代码写得特别跳吗?
搜索 java 源码,只能找到 4 个文件中包含 file.encoding
的文件,
也就是说,只有四个文件调用了 file.encoding
这个属性。
在 java.nio.charset
包中的 Charset.java
中,这段话的意思说的很明确了。
简单说就是默认字符集是在 java 虚拟机启动时决定的,
依赖于 java 虚拟机所在的操作系统的区域以及字符集。
代码中可以看到,默认字符集就是从 file.encoding
这个属性中获取的。
String.getBytes(),源码之下,了无秘密的更多相关文章
- 【原创】http请求中加号被替换为空格?源码背后的秘密
这是why技术的第**20**篇原创文章
String本质上是一个char数组(jdk 9之后是byte数组),并且是一个声明为final的数组,并且String的不可变也是通过这种把数组声明为final来实现的 public final c ...
- String.trim()源码解析
trim()这个方法一般用来消除字符串两边的空格,但是内部是如何实现的呢? 附上源码: public String trim() { int len = value.length; int st = ...
- Java String类源码
String类的签名(JDK 8): public final class String implements java.io.Serializable, Comparable<String&g ...
- 从String 聊源码解读
@ 目录 源码实现 构造方法 equals 其他方法 常见面试题 你真的了解String吗?之前一篇博客写jvm时,就觉得String可以单独拎出来写一篇博客,毕竟几乎所有的面试都是以String开始 ...
- String -- 从源码剖析String类
几乎所有的 Java 面试都是以 String 开始的,String 源码属于所有源码中最基础.最简单的一个,对 String 源码的理解也反应了你的 Java 基础功底. String 是如何实现的 ...
随机推荐
- php之数组
数组分类: 1.索引数组. 索引值从0开始,依次递增. 2.使用array()函数声明数组 <?php // 1.直接为数组元素赋值即可声明数组 $contact_index[0] = 1; $ ...
- Intelli IDEA学习系列之快捷键篇
Intelli IDEA学习系列之快捷键篇 IDEA简介: IDEA 全称IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能 ...
- [2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)
声明 本文对Lambda表达式的扩展,示例代码来源于网络. 场景描述 web开发查询功能的时候,如果查询条件比较多,就会遇到动态组合查询条件的情况.在手写sql的情况下,我们一般会根据传入的参数,针对 ...
- Day4 - Linux分区规划与xshell使用排错
1.1 没有重要数据 /boot 200M 存放系统的引导信息 内核 swap 交换分区 防止内存用光了 临时的一个内存 如果你的内存小于8G swap是内存的1.5倍 如果你的 ...
- Mysql中使用聚合函数对null值的处理
平时因为对于数据库研习的不深,所以在面试的时候问了一些平常遇到过的问题居然没法很肯定地回答出来,实在让自己很恼怒! 这次让我记忆深刻的一个问题是: 在mysql中使用聚合函数的时候比如avg(t),t ...
- Tomcat会话保持之session server
author:JevonWei 版权声明:原创作品 Tomcat会话保持之session server 拓扑图 环境 tomcatA 172.16.253.108 tomcatB 172.16.253 ...
- 【译】怎样处理 Safari 移动端对图片资源的限制
原文作者:Thijs van der Vossen 本文翻译自<How to work around the Mobile Safari image resource limit>,原文写 ...
- CSS中的路径裁剪样式clip-path
前面的话 CSS借鉴了SVG裁剪的概念,设置了clip-path样式,本文将详细介绍路径裁剪clip-path 概述 clip-path属性可以防止部分元素通过定义的剪切区域来显示,仅通过显示的特殊区 ...
- Day-11: IO编程
由于CUP的运行速度远高于其他外设,IO操作有两种方式: 同步IO:CUP登着,程序暂停直到执行完后续代码 异步IO:CUP不等待,去做其他的事情,磁盘做完该做的事情后,告诉CUP,CUP再进行后续代 ...
- JAVA中String = null 与 String = "" 的区别
JAVA中String = null 与 String = ""的区别 笔者今天在Debug的时候发现的NPE(NullPointerException),辛辛苦苦地调试了半天,终 ...