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 是如何实现的 ...
随机推荐
- Struts:文件上传下载
- 2017年8月28日 HTML/CSS 语法(待填坑)
今天这种节日真的是 ----------------------------------------------------------- HTML
- Ext:添加进度条
var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"正在提交..."}); myMask.show(); myMask.hide( ...
- CSS display使用
.今天我们来分析一下display的一些用法,display样式在我们整个CSS设置中,非常重要,掌握好display,才能有效的解决CSS布局的问题,在理解display之前,我们先了解两个概念:块 ...
- [转]Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp85 根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给 ...
- poj 1200字符串hash
题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...
- Project 5:替换指定字符串
这个程序主要用于替换指定字符串,较为简单. #include <stdio.h> void change(char *,char *,char *); int ju(char *,char ...
- JavaScript封装一个MyAlert弹出框
平时我们想要显示一些提示信息时会用到alert方法,alert是全局的一个方法,会短暂的中断程序,我们主要用来显示提示客户信息.但是这个方法有一定的局限性,而且本身样式也不够美观.于是我封装了一个实用 ...
- 团队作业4——第一次项目冲刺(Alpha版本)第六天and第七天
团队作业4--第一次项目冲刺(Alpha版本)第六天and第七天 第一次项目冲刺(Alpha版本)第六天 一.Daily Scrum Meeting照片 二.燃尽图 1.解释说明横纵坐标代表的含义 ...
- list后台转化为JSON的方法ajax
导入alibaba的fastJson包 后台: protected void doGet(HttpServletRequest request, HttpServletResponse respons ...