java中tochar_Java Charsets.toCharset方法代码示例

import org.apache.commons.io.Charsets; //导入方法依赖的package包/类

/**

* Creates a ReversedLinesFileReader with the given block size and encoding.

*

* @param file

* the file to be read

* @param blockSize

* size of the internal buffer (for ideal performance this should

* match with the block size of the underlying file system).

* @param encoding

* the encoding of the file

* @throws IOException if an I/O error occurs

* @since 2.3

*/

public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding) throws IOException {

this.blockSize = blockSize;

this.encoding = encoding;

randomAccessFile = new RandomAccessFile(file, "r");

totalByteLength = randomAccessFile.length();

int lastBlockLength = (int) (totalByteLength % blockSize);

if (lastBlockLength > 0) {

totalBlockCount = totalByteLength / blockSize + 1;

} else {

totalBlockCount = totalByteLength / blockSize;

if (totalByteLength > 0) {

lastBlockLength = blockSize;

}

}

currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);

// --- check & prepare encoding ---

Charset charset = Charsets.toCharset(encoding);

CharsetEncoder charsetEncoder = charset.newEncoder();

float maxBytesPerChar = charsetEncoder.maxBytesPerChar();

if(maxBytesPerChar==1f) {

// all one byte encodings are no problem

byteDecrement = 1;

} else if(charset == Charset.forName("UTF-8")) {

// UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte

// http://en.wikipedia.org/wiki/UTF-8

byteDecrement = 1;

} else if(charset == Charset.forName("Shift_JIS")) {

// Same as for UTF-8

// http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html

byteDecrement = 1;

} else if(charset == Charset.forName("UTF-16BE") || charset == Charset.forName("UTF-16LE")) {

// UTF-16 new line sequences are not allowed as second tuple of four byte sequences,

// however byte order has to be specified

byteDecrement = 2;

} else if(charset == Charset.forName("UTF-16")) {

throw new UnsupportedEncodingException(

"For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)");

} else {

throw new UnsupportedEncodingException(

"Encoding "+encoding+" is not supported yet (feel free to submit a patch)");

}

// NOTE: The new line sequences are matched in the order given, so it is important that \r\n is BEFORE \n

newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) };

avoidNewlineSplitBufferSize = newLineSequences[0].length;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值