当前位置: 首页>>代码示例>>Java>>正文


Java Charsets.toCharset方法代码示例

本文整理汇总了Java中org.apache.commons.io.Charsets.toCharset方法的典型用法代码示例。如果您正苦于以下问题:Java Charsets.toCharset方法的具体用法?Java Charsets.toCharset怎么用?Java Charsets.toCharset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.io.Charsets的用法示例。


在下文中一共展示了Charsets.toCharset方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: ReversedLinesFileReader

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;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:62,代码来源:ReversedLinesFileReader.java

示例2: LockableFileWriter

import org.apache.commons.io.Charsets; //导入方法依赖的package包/类
/**
 * Constructs a LockableFileWriter with a file encoding.
 *
 * @param file  the file to write to, not null
 * @param encoding  the encoding to use, null means platform default
 * @param append  true if content should be appended, false to overwrite
 * @param lockDir  the directory in which the lock file should be held
 * @throws NullPointerException if the file is null
 * @throws IOException in case of an I/O error
 * @throws UnsupportedCharsetException
 *             thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *             supported.
 */
public LockableFileWriter(File file, String encoding, boolean append,
        String lockDir) throws IOException {
    this(file, Charsets.toCharset(encoding), append, lockDir);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:18,代码来源:LockableFileWriter.java


注:本文中的org.apache.commons.io.Charsets.toCharset方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。