本文整理匯總了Java中java.nio.charset.CharsetEncoder.flush方法的典型用法代碼示例。如果您正苦於以下問題:Java CharsetEncoder.flush方法的具體用法?Java CharsetEncoder.flush怎麽用?Java CharsetEncoder.flush使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.charset.CharsetEncoder
的用法示例。
在下文中一共展示了CharsetEncoder.flush方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setSerialNumber
import java.nio.charset.CharsetEncoder; //導入方法依賴的package包/類
/**
* Writes the serial number string to the device EEPROM
*
* @param name
* the string. This string has very limited length, e.g. 4 bytes.
* @throws net.sf.jaer.hardwareinterface.HardwareInterfaceException
*/
public void setSerialNumber(final String name) throws HardwareInterfaceException {
if (!isOpen()) {
open();
}
final CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();
final ByteBuffer buffer = BufferUtils.allocateByteBuffer(name.length());
encoder.encode(CharBuffer.wrap(name), buffer, true);
encoder.flush(buffer);
// sendVendorRequest(CypressFX3.VR_SET_DEVICE_NAME, (short) 0, (short)
// 0, buffer);
stringDescriptor3 = LibUsb.getStringDescriptor(deviceHandle, (byte) 3);
if (stringDescriptor3 == null) {
CypressFX3.log.warning("Could not get new device name!");
}
else {
CypressFX3.log.info("New Devicename set, close and reopen the device to see the change");
}
}
示例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: encode
import java.nio.charset.CharsetEncoder; //導入方法依賴的package包/類
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
ByteBuffer out = ByteBuffer.allocate(length);
encoder.reset();
CoderResult flushResult = null;
while (flushResult != CoderResult.UNDERFLOW) {
CoderResult encodeResult = encoder.encode(in, out, true);
if (encodeResult == CoderResult.OVERFLOW) {
out = allocateMore(out);
continue;
}
flushResult = encoder.flush(out);
if (flushResult == CoderResult.OVERFLOW) {
out = allocateMore(out);
}
}
out.flip();
return out;
}
示例4: encode
import java.nio.charset.CharsetEncoder; //導入方法依賴的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);
}
}
示例5: 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());
}
示例6: 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();
}
示例7: putString
import java.nio.charset.CharsetEncoder; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public IoBuffer putString(CharSequence val, CharsetEncoder encoder) throws CharacterCodingException {
if (val.length() == 0) {
return this;
}
CharBuffer in = CharBuffer.wrap(val);
encoder.reset();
int expandedState = 0;
for (;;) {
CoderResult cr;
if (in.hasRemaining()) {
cr = encoder.encode(in, buf(), true);
} else {
cr = encoder.flush(buf());
}
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
if (isAutoExpand()) {
switch (expandedState) {
case 0:
autoExpand((int) Math.ceil(in.remaining() * encoder.averageBytesPerChar()));
expandedState++;
break;
case 1:
autoExpand((int) Math.ceil(in.remaining() * encoder.maxBytesPerChar()));
expandedState++;
break;
default:
throw new RuntimeException("Expanded by "
+ (int) Math.ceil(in.remaining() * encoder.maxBytesPerChar())
+ " but that wasn't enough for '" + val + "'");
}
continue;
}
} else {
expandedState = 0;
}
cr.throwException();
}
return this;
}
示例8: encode
import java.nio.charset.CharsetEncoder; //導入方法依賴的package包/類
static byte[] encode(Charset cs, byte coder, byte[] val) {
if (cs == UTF_8) {
return encodeUTF8(coder, val);
} else if (cs == ISO_8859_1) {
return encode8859_1(coder, val);
} else if (cs == US_ASCII) {
return encodeASCII(coder, val);
}
CharsetEncoder ce = cs.newEncoder();
// fastpath for ascii compatible
if (coder == LATIN1 && (((ce instanceof ArrayEncoder) &&
((ArrayEncoder)ce).isASCIICompatible() &&
!hasNegatives(val, 0, val.length)))) {
return Arrays.copyOf(val, val.length);
}
int len = val.length >> coder; // assume LATIN1=0/UTF16=1;
int en = scale(len, ce.maxBytesPerChar());
byte[] ba = new byte[en];
if (len == 0) {
return ba;
}
boolean isTrusted = cs.getClass().getClassLoader0() == null ||
System.getSecurityManager() == null;
ce.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.reset();
if (ce instanceof ArrayEncoder) {
if (!isTrusted) {
val = Arrays.copyOf(val, val.length);
}
int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba)
: ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba);
if (blen != -1) {
return safeTrim(ba, blen, isTrusted);
}
}
char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val)
: StringUTF16.toChars(val);
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca, 0, 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(), isTrusted);
}