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


Java Charset.newEncoder方法代码示例

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


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

示例1: JavaCTBConverter

import java.nio.charset.Charset; //导入方法依赖的package包/类
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset,
                        int alignmentForEncoding) {

    try {
        ctb = cache.getCharToByteConverter(codeset.getName());
        if (ctb == null) {
            Charset tmpCharset = Charset.forName(codeset.getName());
            ctb = tmpCharset.newEncoder();
            cache.setConverter(codeset.getName(), ctb);
        }
    } catch(IllegalCharsetNameException icne) {

        // This can only happen if one of our Entries has
        // an invalid name.
        throw wrapper.invalidCtbConverterName(icne,codeset.getName());
    } catch(UnsupportedCharsetException ucne) {

        // This can only happen if one of our Entries has
        // an unsupported name.
        throw wrapper.invalidCtbConverterName(ucne,codeset.getName());
    }

    this.codeset = codeset;
    alignment = alignmentForEncoding;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:CodeSetConversion.java

示例2: Encoder

import java.nio.charset.Charset; //导入方法依赖的package包/类
public Encoder(Charset cs)
{
    super(cs);
    SODesig = "$)A";

    try {
        Charset cset = Charset.forName("EUC_CN"); // GB2312
        ISOEncoder = cset.newEncoder();
    } catch (Exception e) { }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:ISO2022_CN_GB.java

示例3: test_error_1

import java.nio.charset.Charset; //导入方法依赖的package包/类
public void test_error_1() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder realEncoder = charset.newEncoder();
    
    CharsetEncoder charsetEncoder = new MockCharsetEncoder(charset, realEncoder);

    Exception error = null;
    char[] chars = "abc".toCharArray();
    try {
        encode(charsetEncoder, chars, 0, chars.length);
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:SerialWriterStringEncoderTest2.java

示例4: handleHead

import java.nio.charset.Charset; //导入方法依赖的package包/类
private CoderResult handleHead (CharBuffer in, ByteBuffer out) {
    String encoding = getEncoding ();
    if (encoding == null) {          
        buffer = null;
        throwUnknownEncoding();
        return null;
    }
    else {
        Charset c = Charset.forName(encoding);
        encoder = c.newEncoder();
        return flushHead(in, out);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:14,代码来源:FileEncodingQueryTest.java

示例5: saveAs

import java.nio.charset.Charset; //导入方法依赖的package包/类
public void saveAs(String path) throws IOException {
    Storage storage = getStorage();
    if (storage == null) {
        throw new IOException ("Data has already been disposed"); //NOI18N
    }
    try {
        String encoding = System.getProperty ("file.encoding"); //NOI18N
        if (encoding == null) {
            encoding = "UTF-8"; //NOI18N
        }
        Charset charset = Charset.forName (encoding); //NOI18N
        CharsetEncoder encoder = charset.newEncoder ();
        String ls = System.getProperty("line.separator");
        Path filePath = Paths.get(path);
        try (FileChannel ch = FileChannel.open(filePath, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
            ByteBuffer lsbb = encoder.encode(CharBuffer.wrap(ls));
            for (int i = 0; i < getLineCount(); i++) {
                int lineStart = getCharLineStart(i);
                int lineLength = length(i);
                BufferResource<CharBuffer> br = getCharBuffer(lineStart,
                        lineLength);
                try {
                    CharBuffer cb = br.getBuffer();
                    ByteBuffer bb = encoder.encode(cb);
                    ch.write(bb);
                    if (i != getLineCount() - 1) {
                        lsbb.rewind();
                        ch.write(lsbb);
                    }
                } finally {
                    if (br != null) {
                        br.releaseBuffer();
                    }
                }
            }
        }
    } finally {
        FileUtil.refreshFor(new java.io.File(path));
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:41,代码来源:AbstractLines.java

示例6: determineConsecutiveBinaryCount

import java.nio.charset.Charset; //导入方法依赖的package包/类
/**
 * Determines the number of consecutive characters that are encodable using binary compaction.
 *
 * @param msg      the message
 * @param startpos the start position within the message
 * @param encoding the charset used to convert the message to a byte array
 * @return the requested character count
 */
private static int determineConsecutiveBinaryCount(String msg, int startpos, Charset encoding)
    throws WriterException {
  final CharsetEncoder encoder = encoding.newEncoder();
  int len = msg.length();
  int idx = startpos;
  while (idx < len) {
    char ch = msg.charAt(idx);
    int numericCount = 0;

    while (numericCount < 13 && isDigit(ch)) {
      numericCount++;
      //textCount++;
      int i = idx + numericCount;
      if (i >= len) {
        break;
      }
      ch = msg.charAt(i);
    }
    if (numericCount >= 13) {
      return idx - startpos;
    }
    ch = msg.charAt(idx);

    if (!encoder.canEncode(ch)) {
      throw new WriterException("Non-encodable character detected: " + ch + " (Unicode: " + (int) ch + ')');
    }
    idx++;
  }
  return idx - startpos;
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:39,代码来源:PDF417HighLevelEncoder.java

示例7: encode

import java.nio.charset.Charset; //导入方法依赖的package包/类
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:36,代码来源:StringCoding.java

示例8: determineConsecutiveBinaryCount

import java.nio.charset.Charset; //导入方法依赖的package包/类
private static int determineConsecutiveBinaryCount(String msg, int startpos, Charset
        encoding) throws WriterException {
    CharsetEncoder encoder = encoding.newEncoder();
    int len = msg.length();
    int idx = startpos;
    while (idx < len) {
        char ch = msg.charAt(idx);
        int numericCount = 0;
        while (numericCount < 13 && isDigit(ch)) {
            numericCount++;
            int i = idx + numericCount;
            if (i >= len) {
                break;
            }
            ch = msg.charAt(i);
        }
        if (numericCount >= 13) {
            return idx - startpos;
        }
        ch = msg.charAt(idx);
        if (encoder.canEncode(ch)) {
            idx++;
        } else {
            throw new WriterException("Non-encodable character detected: " + ch + " (Unicode:" +
                    " " + ch + ')');
        }
    }
    return idx - startpos;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:30,代码来源:PDF417HighLevelEncoder.java

示例9: XMLWriter

import java.nio.charset.Charset; //导入方法依赖的package包/类
public XMLWriter(OutputStream os, String encoding, Charset cs) throws XMLStreamException {
    _encoder = cs.newEncoder();
    try {
        _writer = getWriter(os, encoding, cs);
    } catch (UnsupportedEncodingException ex) {
        throw new XMLStreamException(ex);
    }

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:XMLWriter.java

示例10: initializeConverter

import java.nio.charset.Charset; //导入方法依赖的package包/类
private static void initializeConverter() throws UnsupportedEncodingException {
    Charset cs = null;

    try {
        cs = (encodingString == null) ?
            lookupCharset(defaultEncoding):
            lookupCharset(encodingString);

        encoder =  (cs != null) ?
            cs.newEncoder() :
            null;
    } catch (IllegalCharsetNameException e) {
        throw new Error(e);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:16,代码来源:Main.java

示例11: testCheckFileLines

import java.nio.charset.Charset; //导入方法依赖的package包/类
public void testCheckFileLines() throws IOException {

        FileSystem fs = FileUtil.createMemoryFileSystem();
        Charset chs = Charset.forName("UTF-8");
        OutputStream os = fs.getRoot().createAndOpen("find.txt");
        try {
            OutputStreamWriter osw = new OutputStreamWriter(os,
                    chs.newEncoder());
            try {
                osw.write("Text on line 1.");
                osw.write("Line 1 has some more text included!\r\n");
                osw.write("Not matching line.\r\n");
                osw.write("Line 3 contains text\n");
                osw.write("\n");
                osw.write("Line 5 contains text\n");
            } finally {
                osw.flush();
                osw.close();
            }
        } finally {
            os.flush();
            os.close();
        }
        BasicSearchCriteria bsc = new BasicSearchCriteria();
        bsc.setFileNamePattern("*.*");
        bsc.setTextPattern("text");
        bsc.onOk();

        FileObject fo = fs.getRoot().getFileObject("find.txt");
        //bsc.matches(fo, new SearchListener() {});
        AbstractMatcher fileMatcher =
                new DefaultMatcher(bsc.getSearchPattern());
        Def resultDef = fileMatcher.check(fo, new SearchListener() {
        });
        List<TextDetail> matches = resultDef.getTextDetails();
        assertEquals(4, matches.size());

        assertEquals(1, matches.get(0).getLine());
        assertEquals(1, matches.get(0).getColumn());
        assertEquals(4, matches.get(0).getMarkLength());

        assertEquals(1, matches.get(1).getLine());
        assertEquals(37, matches.get(1).getColumn());

        assertEquals(3, matches.get(2).getLine());
        assertEquals(17, matches.get(2).getColumn());

        assertEquals(5, matches.get(3).getLine());
    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:50,代码来源:MatchingObjectTest.java

示例12: ReversedLinesFileReader

import java.nio.charset.Charset; //导入方法依赖的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
 */
@SuppressWarnings("deprecation") // unavoidable until Java 7
public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding) throws IOException {
    this.blockSize = blockSize;
    this.encoding = encoding;

    // --- check & prepare encoding ---
    final Charset charset = Charsets.toCharset(encoding);
    final CharsetEncoder charsetEncoder = charset.newEncoder();
    final float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
    if (maxBytesPerChar == 1f) {
        // all one byte encodings are no problem
        byteDecrement = 1;
    } else if (charset == Charsets.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
            charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
            charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
            charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
            charset == Charset.forName("x-windows-950")) { // Windows code page 950 (Traditional Chinese)
        byteDecrement = 1;
    } else if (charset == Charsets.UTF_16BE || charset == Charsets.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 == Charsets.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;

    // Open file
    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);

}
 
开发者ID:kolbasa,项目名称:cordova-logcat-filelogger,代码行数:69,代码来源:ReversedLinesFileReader.java

示例13: createEncoder

import java.nio.charset.Charset; //导入方法依赖的package包/类
public static CharsetEncoder createEncoder( String encodin ) {
    Charset cs = Charset.forName(System.getProperty("file.encoding"));
    CharsetEncoder encoder = cs.newEncoder();
    return encoder;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:EncoderFactory.java

示例14: write

import java.nio.charset.Charset; //导入方法依赖的package包/类
/**
 * Write lines of text to a file. Each line is a char sequence and is
 * written to the file in sequence with each line terminated by the
 * platform's line separator, as defined by the system property {@code
 * line.separator}. Characters are encoded into bytes using the specified
 * charset.
 *
 * <p> The {@code options} parameter specifies how the file is created
 * or opened. If no options are present then this method works as if the
 * {@link StandardOpenOption#CREATE CREATE}, {@link
 * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
 * StandardOpenOption#WRITE WRITE} options are present. In other words, it
 * opens the file for writing, creating the file if it doesn't exist, or
 * initially truncating an existing {@link #isRegularFile regular-file} to
 * a size of {@code 0}. The method ensures that the file is closed when all
 * lines have been written (or an I/O error or other runtime exception is
 * thrown). If an I/O error occurs then it may do so after the file has
 * been created or truncated, or after some bytes have been written to the
 * file.
 *
 * @param   path
 *          the path to the file
 * @param   lines
 *          an object to iterate over the char sequences
 * @param   cs
 *          the charset to use for encoding
 * @param   options
 *          options specifying how the file is opened
 *
 * @return  the path
 *
 * @throws  IllegalArgumentException
 *          if {@code options} contains an invalid combination of options
 * @throws  IOException
 *          if an I/O error occurs writing to or creating the file, or the
 *          text cannot be encoded using the specified charset
 * @throws  UnsupportedOperationException
 *          if an unsupported option is specified
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
 *          method is invoked to check write access to the file. The {@link
 *          SecurityManager#checkDelete(String) checkDelete} method is
 *          invoked to check delete access if the file is opened with the
 *          {@code DELETE_ON_CLOSE} option.
 */
public static Path write(Path path, Iterable<? extends CharSequence> lines,
                         Charset cs, OpenOption... options)
    throws IOException
{
    // ensure lines is not null before opening file
    Objects.requireNonNull(lines);
    CharsetEncoder encoder = cs.newEncoder();
    OutputStream out = newOutputStream(path, options);
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
        for (CharSequence line: lines) {
            writer.append(line);
            writer.newLine();
        }
    }
    return path;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:63,代码来源:Files.java

示例15: ReversedLinesFileReader

import java.nio.charset.Charset; //导入方法依赖的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


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