本文整理匯總了Java中java.nio.CharBuffer.flip方法的典型用法代碼示例。如果您正苦於以下問題:Java CharBuffer.flip方法的具體用法?Java CharBuffer.flip怎麽用?Java CharBuffer.flip使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.CharBuffer
的用法示例。
在下文中一共展示了CharBuffer.flip方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: read
import java.nio.CharBuffer; //導入方法依賴的package包/類
public Object read(java.io.Reader r) throws IOException, ClassNotFoundException {
java.io.
BufferedReader buf = new BufferedReader(r, 4096);
CharBuffer arr = CharBuffer.allocate(2048);
buf.mark(arr.capacity());
buf.read(arr);
arr.flip();
Matcher m = Pattern.compile("<java").matcher(arr);
if (m.find()) {
buf.reset();
buf.skip(m.start());
} else {
buf.reset();
}
XMLDecoder d = new XMLDecoder(new ReaderInputStream(buf, "UTF-8"));
return d.readObject();
}
示例2: makeSpace
import java.nio.CharBuffer; //導入方法依賴的package包/類
private void makeSpace() {
int offset = position;
buf.position(offset);
// Gain space by compacting buffer
if (offset > 0) {
buf.compact();
position -= offset;
buf.flip();
return;
}
// Gain space by growing buffer
int newSize = buf.capacity() * 2;
CharBuffer newBuf = CharBuffer.allocate(newSize);
newBuf.put(buf);
newBuf.flip();
position -= offset;
buf = newBuf;
matcher.reset(buf);
}
示例3: byCharsetEncoder_US_ASCII
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Benchmark
public byte[] byCharsetEncoder_US_ASCII() {
try {
CharsetEncoder encoder = asciiencode.get();
CharBuffer buffer = charbuffergenerator.get();
buffer.clear();
buffer.append(STR);
buffer.flip();
ByteBuffer outbuffer = bytebuffergenerator.get();
outbuffer.clear();
CoderResult result = encoder.encode(buffer, outbuffer, false);
if (result.isError()) {
result.throwException();
}
byte[] b = new byte[STR.length()];
outbuffer.flip();
outbuffer.get(b);
return b;
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
示例4: tmpFileWriteRead
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Test
public void tmpFileWriteRead() throws IOException {
PrintWriterOf<File> tmpFileOut = IoStream.tempFile().printWriter("UTF-8");
try {
tmpFileOut.append("tmpé");
} finally {
tmpFileOut.close();
}
try (BufferedReaderOf<File> tmpFileIn = IoStream.file(tmpFileOut.get()).bufferedReader(UTF_8)) {
CharBuffer sb = CharBuffer.allocate(5);
assertThat(tmpFileIn.read(sb)).isEqualTo(4);
sb.flip();
assertThat(sb.toString()).isEqualTo("tmpé");
}
}
示例5: getBytes
import java.nio.CharBuffer; //導入方法依賴的package包/類
public byte[] getBytes(char[] charArr, String charset) {// 將字符轉為字節(編碼)
if (charset == null || "".equals(charset.trim())) {
charset = "UTF-8";
}
CharBuffer charBuffer = CharBuffer.allocate(charArr.length);
charBuffer.put(charArr);
charBuffer.flip();
ByteBuffer byteBuffer = Charset.forName(charset).encode(charBuffer);
return byteBuffer.array();
}
示例6: test
import java.nio.CharBuffer; //導入方法依賴的package包/類
static void test(String expectedCharset, byte[] input) throws Exception {
Charset cs = Charset.forName("x-JISAutoDetect");
CharsetDecoder autoDetect = cs.newDecoder();
Charset cs2 = Charset.forName(expectedCharset);
CharsetDecoder decoder = cs2.newDecoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CharBuffer charOutput = CharBuffer.allocate(128);
CharBuffer charExpected = CharBuffer.allocate(128);
bb.put(input);
bb.flip();
bb.mark();
CoderResult result = autoDetect.decode(bb, charOutput, true);
checkCoderResult(result);
charOutput.flip();
String actual = charOutput.toString();
bb.reset();
result = decoder.decode(bb, charExpected, true);
checkCoderResult(result);
charExpected.flip();
String expected = charExpected.toString();
check(actual.equals(expected),
String.format("actual=%s expected=%s", actual, expected));
}
示例7: assertRoundTrips
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static void assertRoundTrips(String str, int index, int size, byte[] bytes, Memory mem,
WritableMemory writeMem) throws IOException {
StringBuilder sb = new StringBuilder();
mem.getCharsFromUtf8(index, size, sb);
checkStrings(sb.toString(), new String(bytes, index, size, UTF_8));
CharBuffer cb = CharBuffer.allocate(bytes.length + 1);
cb.position(1);
// Make CharBuffer 1-based, to check correct offset handling
cb = cb.slice();
mem.getCharsFromUtf8(index, size, cb);
cb.flip();
checkStrings(cb.toString(), new String(bytes, index, size, UTF_8));
assertEquals(writeMem.putCharsToUtf8(0, str), bytes.length);
assertEquals(0, writeMem.compareTo(0, bytes.length, mem, 0, bytes.length));
// Test write overflow
WritableMemory writeMem2 = WritableMemory.allocate(bytes.length - 1);
try {
writeMem2.putCharsToUtf8(0, str);
fail();
} catch (Utf8CodingException e) {
// Expected.
}
}
示例8: byte2char
import java.nio.CharBuffer; //導入方法依賴的package包/類
private void byte2char(ByteBuffer bytes, CharBuffer chars) throws IOException {
decoder.reset();
chars.clear();
CoderResult result = decoder.decode(bytes, chars, true);
if (result.isError() || result.isOverflow()) {
throw new IOException(result.toString());
} else if (result.isUnderflow()) {
chars.flip();
}
}
示例9: getSinkContents
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Override
public String getSinkContents() throws IOException {
File file = getFile();
Reader reader = new InputStreamReader(new FileInputStream(file), Charsets.UTF_8);
StringBuilder builder = new StringBuilder();
CharBuffer buffer = CharBuffer.allocate(100);
while (reader.read(buffer) != -1) {
buffer.flip();
builder.append(buffer);
buffer.clear();
}
return builder.toString();
}
示例10: readByteArray
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Read a 'n' bytes from buffer into a String where n is the frameSize - offset
* so therefore cannot use this if there are other objects after it because it has no
* delimiter.
* <p/>
* Must take into account the text encoding defined in the Encoding Object
* ID3 Text Frames often allow multiple strings separated by the null char
* appropriate for the encoding.
*
* @param arr this is the buffer for the frame
* @param offset this is where to start reading in the buffer for this field
* @throws NullPointerException
* @throws IndexOutOfBoundsException
*/
public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException {
logger.finest("Reading from array from offset:" + offset);
//Get the Specified Decoder
String charSetName = getTextEncodingCharSet();
CharsetDecoder decoder = Charset.forName(charSetName).newDecoder();
//Decode sliced inBuffer
ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
decoder.reset();
CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
if (coderResult.isError()) {
logger.warning("Decoding error:" + coderResult.toString());
}
decoder.flush(outBuffer);
outBuffer.flip();
//Store value
String stringValue = outBuffer.toString();
value = new PartOfSetValue(stringValue);
//SetSize, important this is correct for finding the next datatype
setSize(arr.length - offset);
logger.config("Read SizeTerminatedString:" + value + " size:" + size);
}
示例11: decode
import java.nio.CharBuffer; //導入方法依賴的package包/類
private char decode(byte byte1, byte byte2, byte shiftFlag)
{
byte1 |= MSB;
byte2 |= MSB;
byte[] tmpByte = { byte1,byte2 };
char[] tmpChar = new char[1];
int i = 0,
tmpIndex = 0;
switch(shiftFlag) {
case SOFlag:
tmpIndex = curSODes;
tmpDecoder = SODecoder;
break;
case SS2Flag:
tmpIndex = curSS2Des;
tmpDecoder = SS2Decoder;
break;
case SS3Flag:
tmpIndex = curSS3Des;
tmpDecoder = SS3Decoder;
break;
}
if (tmpDecoder != null) {
for(i = 0; i < tmpDecoder.length; i++) {
if(tmpIndex == i) {
try {
ByteBuffer bb = ByteBuffer.wrap(tmpByte,0,2);
CharBuffer cc = CharBuffer.wrap(tmpChar,0,1);
tmpDecoder[i].decode(bb, cc, true);
cc.flip();
return cc.get();
} catch (Exception e) {}
}
}
}
return REPLACE_CHAR;
}
示例12: toStringBuilder
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static StringBuilder toStringBuilder(final Readable input)
throws IOException {
final StringBuilder text = new StringBuilder();
final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
while (true) {
final int n = input.read(buffer);
if (n == -1) {
break;
}
buffer.flip();
text.append(buffer, 0, n);
}
return text;
}
示例13: getBytes
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static byte[] getBytes(char[] chars) {
Charset cs = Charset.forName("UTF-8");
CharBuffer cb = CharBuffer.allocate(chars.length);
cb.put(chars);
cb.flip();
ByteBuffer bb = cs.encode(cb);
return bb.array();
}
示例14: main
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
*
* @param args
*/
public static void main(String args[]) {
CharBuffer charBuffer = CharBuffer.allocate(BUF_SIZE);
File file = new File("temp.txt");
file.deleteOnExit();
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
char arr[] = new char[BUF_SIZE];
charBuffer = CharBuffer.wrap(arr);
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
char secArr[] = new char[BUF_SIZE];
charBuffer = CharBuffer.wrap(secArr);
charBuffer.put(str);
charBuffer.position(5);
charBuffer.limit(str.length() - 7);
charBuffer = charBuffer.slice();
checkFileContent(charBuffer, file, str.substring(5, (str.length() - 7)));
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(15, (str.length() - 7)));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
charBuffer = ByteBuffer.allocate(BUF_SIZE).asCharBuffer();
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
charBuffer = ByteBuffer.allocateDirect(1024).asCharBuffer();
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
}
示例15: tryDecode
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Override
public String
tryDecode(
byte[] array,
boolean lax )
{
try{
ByteBuffer bb = ByteBuffer.wrap(array);
CharBuffer cb = CharBuffer.allocate(array.length);
CoderResult cr;
this_mon.enter();
try {
cr = decoder.decode(bb, cb, true);
} finally {
this_mon.exit();
}
if ( !cr.isError() ){
cb.flip();
String str = cb.toString();
// lax means that as long as the conversion works we consider it usable
// as opposed to strict which requires reverse-conversion equivalence
if ( lax ){
return( str );
}
byte[] b2 = str.getBytes( getName() );
// make sure the conversion is symetric (there are cases where it appears
// to work but in fact converting back to bytes leads to a different
// result
/*
for (int k=0;k<str.length();k++){
System.out.print( Integer.toHexString(str.charAt(k)));
}
System.out.println("");
*/
if ( Arrays.equals( array, b2 )){
return( str );
}
}
return( null );
}catch( Throwable e ){
// Throwable here as we can get "classdefnotfound" + others if the decoder
// isn't available
return( null );
}
}