本文整理汇总了Java中java.nio.charset.CharsetEncoder.maxBytesPerChar方法的典型用法代码示例。如果您正苦于以下问题:Java CharsetEncoder.maxBytesPerChar方法的具体用法?Java CharsetEncoder.maxBytesPerChar怎么用?Java CharsetEncoder.maxBytesPerChar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.charset.CharsetEncoder
的用法示例。
在下文中一共展示了CharsetEncoder.maxBytesPerChar方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setEncoding
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
protected final void setEncoding(final String encoding)
throws UnsupportedEncodingException {
final Charset charSet = charsetForName(encoding);
final CharsetEncoder encoder = charSet.newEncoder().onMalformedInput(
CodingErrorAction.REPLACE).onUnmappableCharacter(
CodingErrorAction.REPLACE);
final float maxBytesPerChar = encoder.maxBytesPerChar();
final float averageBytesPerChar = encoder.averageBytesPerChar();
final boolean fixedWidthCharset =
(maxBytesPerChar == Math.round(maxBytesPerChar))
&& (maxBytesPerChar == averageBytesPerChar);
//
m_fixedWidthCharset = fixedWidthCharset;
m_maxCharWidth = Math.round(maxBytesPerChar);
m_charset = charSet;
m_encoder = encoder;
m_encoding = m_charset.name();
}
示例2: getBytes
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
byte[] getBytes(String s) {
CharsetEncoder ce = encoder().reset();
char[] ca = s.toCharArray();
int len = (int)(ca.length * ce.maxBytesPerChar());
byte[] ba = new byte[len];
if (len == 0)
return ba;
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca);
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
cr = ce.flush(bb);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
if (bb.position() == ba.length) // defensive copy?
return ba;
else
return Arrays.copyOf(ba, bb.position());
}
示例3: getBytes
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
byte[] getBytes(String s) {
CharsetEncoder ce = encoder().reset();
char[] ca = s.toCharArray();
int len = (int)(ca.length * ce.maxBytesPerChar());
byte[] ba = new byte[len];
if (len == 0)
return ba;
// UTF-8 only for now. Other ArrayDeocder only handles
// CodingErrorAction.REPLACE mode.
if (isUTF8 && ce instanceof ArrayEncoder) {
int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba);
if (blen == -1) // malformed
throw new IllegalArgumentException("MALFORMED");
return Arrays.copyOf(ba, blen);
}
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca);
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
cr = ce.flush(bb);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
if (bb.position() == ba.length) // defensive copy?
return ba;
else
return Arrays.copyOf(ba, bb.position());
}
示例4: getSubstringByte
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
/**
* 指定したバイト数で文字列をカットする
*
* @param obj 対象オブジェクト
* @param capacity カットするバイト数
* @return String
* @throws CharacterCodingException
* @throws UnsupportedEncodingException
*/
private String getSubstringByte(final Object obj, final int capacity) throws CharacterCodingException,
UnsupportedEncodingException {
String str = obj == null ? "null" : obj.toString();
if (capacity < 1) {
return str;
}
CharsetEncoder ce = Charset.forName(ENCODING_SHIFT_JIS).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
if (capacity >= ce.maxBytesPerChar() * str.length()) {
return str;
}
CharBuffer cb = CharBuffer.wrap(new char[Math.min(str.length(), capacity)]);
str.getChars(0, Math.min(str.length(), cb.length()), cb.array(), 0);
if (capacity >= ce.maxBytesPerChar() * cb.limit()) {
return cb.toString();
}
ByteBuffer out = ByteBuffer.allocate(capacity);
ce.reset();
CoderResult cr = null;
if (cb.hasRemaining()) {
cr = ce.encode(cb, out, true);
} else {
cr = CoderResult.UNDERFLOW;
}
if (cr.isUnderflow()) {
cr = ce.flush(out);
}
return cb.flip().toString();
}
示例5: MockCharsetEncoder
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
protected MockCharsetEncoder(Charset cs, CharsetEncoder raw){
super(cs, raw.averageBytesPerChar(), raw.maxBytesPerChar());
this.raw = raw;
}
示例6: ProxyEncoder
import java.nio.charset.CharsetEncoder; //导入方法依赖的package包/类
private ProxyEncoder (final CharsetEncoder defaultEncoder) {
super (ProxyCharset.this, defaultEncoder.averageBytesPerChar(), defaultEncoder.maxBytesPerChar(), defaultEncoder.replacement());
this.currentEncoder = defaultEncoder;
this.initialized = true;
}
示例7: ReversedLinesFileReader
import java.nio.charset.CharsetEncoder; //导入方法依赖的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;
}
示例8: ReversedLinesFileReader
import java.nio.charset.CharsetEncoder; //导入方法依赖的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);
}