本文整理匯總了Java中java.nio.CharBuffer.allocate方法的典型用法代碼示例。如果您正苦於以下問題:Java CharBuffer.allocate方法的具體用法?Java CharBuffer.allocate怎麽用?Java CharBuffer.allocate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.CharBuffer
的用法示例。
在下文中一共展示了CharBuffer.allocate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: WsFrameBase
import java.nio.CharBuffer; //導入方法依賴的package包/類
public WsFrameBase(WsSession wsSession, Transformation transformation) {
inputBuffer = new byte[Constants.DEFAULT_BUFFER_SIZE];
messageBufferBinary =
ByteBuffer.allocate(wsSession.getMaxBinaryMessageBufferSize());
messageBufferText =
CharBuffer.allocate(wsSession.getMaxTextMessageBufferSize());
this.wsSession = wsSession;
Transformation finalTransformation;
if (isMasked()) {
finalTransformation = new UnmaskTransformation();
} else {
finalTransformation = new NoopTransformation();
}
if (transformation == null) {
this.transformation = finalTransformation;
} else {
transformation.setNext(finalTransformation);
this.transformation = transformation;
}
}
示例2: main
import java.nio.CharBuffer; //導入方法依賴的package包/類
public static void main(String [] args) {
StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
"Fruit Titanesque, Inc.");
CharBuffer cb = CharBuffer.allocate(128);
Formatter fmt = new Formatter(cb);
fmt.format("%s", sn); // -> "Huge Fruit, Inc."
test(cb, "Huge Fruit, Inc.");
fmt.format("%s", sn.toString()); // -> "HUGE - Huge Fruit, Inc."
test(cb, "HUGE - Huge Fruit, Inc.");
fmt.format("%#s", sn); // -> "HUGE"
test(cb, "HUGE");
fmt.format("%-10.8s", sn); // -> "HUGE "
test(cb, "HUGE ");
fmt.format("%.12s", sn); // -> "Huge Fruit,*"
test(cb, "Huge Fruit,*");
fmt.format(Locale.FRANCE, "%25s", sn);
// -> " Fruit Titanesque, Inc."
test(cb, " Fruit Titanesque, Inc.");
}
示例3: resizeCharBuffer
import java.nio.CharBuffer; //導入方法依賴的package包/類
private void resizeCharBuffer() throws IOException {
int maxSize = getCharBufferMaxSize();
if (cb.limit() >= maxSize) {
throw new IOException(sm.getString("message.bufferTooSmall"));
}
long newSize = cb.limit() * 2;
if (newSize > maxSize) {
newSize = maxSize;
}
// Cast is safe. newSize < maxSize and maxSize is an int
CharBuffer newBuffer = CharBuffer.allocate((int) newSize);
cb.rewind();
newBuffer.put(cb);
cb = newBuffer;
}
示例4: 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();
}
示例5: resizeCharBuffer
import java.nio.CharBuffer; //導入方法依賴的package包/類
private void resizeCharBuffer() throws IOException {
int maxSize = getCharBufferMaxSize();
if (cb.limit() >= maxSize) {
throw new IOException(sm.getString("message.bufferTooSmall"));
}
long newSize = cb.limit() * 2;
if (newSize > maxSize) {
newSize = maxSize;
}
// Cast is safe. newSize < maxSize and maxSize is an int
CharBuffer newBuffer = CharBuffer.allocate((int) newSize);
cb.rewind();
newBuffer.put(cb);
cb = newBuffer;
}
示例6: stringReadChunked
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Test
public void stringReadChunked() {
final int MAX_STRING_LENGTH = 16;
final ByteBuffer bytes = ByteBuffer.allocate(MAX_STRING_LENGTH + 6);
final CharBuffer chars = CharBuffer.allocate(MAX_STRING_LENGTH);
final StringReader reader = new StringReader();
final StringWriter writer = new StringWriter();
for (int len = 0; len <= MAX_STRING_LENGTH; len++) {
byte[] b = new byte[len];
rnd.nextBytes(b);
String expected = new String(b, StandardCharsets.ISO_8859_1); // reference string
boolean written = writer
.configure(CharBuffer.wrap(expected), 0, expected.length(), false)
.write(bytes);
writer.reset();
if (!written) {
fail("please increase 'bytes' size");
}
bytes.flip();
forEachSplit(bytes, (buffers) -> {
for (ByteBuffer buf : buffers) {
int p0 = buf.position();
reader.read(buf, chars);
buf.position(p0);
}
chars.flip();
assertEquals(chars.toString(), expected);
reader.reset();
chars.clear();
});
bytes.clear();
}
}
示例7: readFile
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static String readFile(NbTestCase test, File f) throws Exception {
FileReader r = new FileReader(f);
int fileLen = (int)f.length();
CharBuffer cb = CharBuffer.allocate(fileLen);
r.read(cb);
cb.rewind();
return cb.toString();
}
示例8: allocate
import java.nio.CharBuffer; //導入方法依賴的package包/類
private void allocate(int sourceCapacity) {
if (sourceCapacity == 0) {
charBuffer = CharBuffer.allocate(0);
return;
}
int bufferSize = bufferSize(sourceCapacity);
charBuffer = CharBuffer.allocate(bufferSize);
}
示例9: ReaderInputStream
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Creates a new input stream that will encode the characters from {@code reader} into bytes using
* the given character set encoder.
*
* @param reader input source
* @param encoder character set encoder used for encoding chars to bytes
* @param bufferSize size of internal input and output buffers
* @throws IllegalArgumentException if bufferSize is non-positive
*/
ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) {
this.reader = checkNotNull(reader);
this.encoder = checkNotNull(encoder);
checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize);
encoder.reset();
charBuffer = CharBuffer.allocate(bufferSize);
charBuffer.flip();
byteBuffer = ByteBuffer.allocate(bufferSize);
}
示例10: ReaderInputStream
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Creates a new input stream that will encode the characters from {@code reader} into bytes using
* the given character set encoder.
*
* @param reader input source
* @param encoder character set encoder used for encoding chars to bytes
* @param bufferSize size of internal input and output buffers
* @throws IllegalArgumentException if bufferSize is non-positive
*/
ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) {
Assert.notNull(reader, "不能為空");
Assert.notNull(encoder, "不能為空");
this.reader = reader;
this.encoder = encoder;
encoder.reset();
charBuffer = CharBuffer.allocate(bufferSize);
charBuffer.flip();
byteBuffer = ByteBuffer.allocate(bufferSize);
}
示例11: growCharBuffer
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Grow the charbuffer making sure not to overflow size integer. Note
* this grows in the same manner as the ArrayList that is it adds 50%
* to the current size.
*/
private void growCharBuffer() {
// overflow-conscious code
int oldCapacity = charBuffer.capacity();
//System.out.println("old capacity " + oldCapacity);
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
}
//System.out.println("new capacity " + newCapacity);
charBuffer = CharBuffer.allocate(newCapacity);
}
示例12: callOriginalMethod
import java.nio.CharBuffer; //導入方法依賴的package包/類
public void callOriginalMethod(CharSequence translatedString, Object userData) {
MethodHookParam methodHookParam = (MethodHookParam) userData;
Method myMethod = (Method) methodHookParam.method;
myMethod.setAccessible(true);
Object[] myArgs = methodHookParam.args;
if (myMethod.getName().equals("setText")) {
//if((myMethod.getName()=="setText")) {
if (myArgs[0].getClass().equals(AlteredCharSequence.class)) {
myArgs[0] = AlteredCharSequence.make(translatedString, null, 0, 0);
} else if (myArgs[0].getClass().equals(CharBuffer.class)) {
CharBuffer charBuffer = CharBuffer.allocate(translatedString.length() + 1);
charBuffer.append(translatedString);
myArgs[0] = charBuffer;
} else if (myArgs[0].getClass().equals(SpannableString.class)) {
myArgs[0] = new SpannableString(translatedString);
} else if (myArgs[0].getClass().equals(SpannedString.class)) {
myArgs[0] = new SpannedString(translatedString);
} else if (myArgs[0].getClass().equals(String.class)) {
myArgs[0] = translatedString.toString();
} else if (myArgs[0].getClass().equals(StringBuffer.class)) {
myArgs[0] = new StringBuffer(translatedString);
} else if (myArgs[0].getClass().equals(StringBuilder.class)) {
myArgs[0] = new StringBuilder(translatedString);
} else {
myArgs[0] = new SpannableStringBuilder(translatedString);
}
} else {
myArgs[0] = TextUtils.stringOrSpannedString(translatedString);
}
alltrans.hookAccess.acquireUninterruptibly();
unhookMethod(methodHookParam.method, alltrans.setTextHook);
try {
utils.debugLog("In Thread " + Thread.currentThread().getId() + " Invoking original function " + methodHookParam.method.getName() + " and setting text to " + myArgs[0].toString());
myMethod.invoke(methodHookParam.thisObject, myArgs);
} catch (Exception e) {
Log.e("AllTrans", "AllTrans: Got error in invoking method as : " + Log.getStackTraceString(e));
}
hookMethod(methodHookParam.method, alltrans.setTextHook);
alltrans.hookAccess.release();
}
示例13: createBuffer
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Creates a new {@code CharBuffer} for buffering reads or writes.
*/
static CharBuffer createBuffer() {
return CharBuffer.allocate(0x800); // 2K chars (4K bytes)
}
示例14: callOriginalMethod
import java.nio.CharBuffer; //導入方法依賴的package包/類
public void callOriginalMethod(CharSequence translatedString, Object userData) {
MethodHookParam methodHookParam = (MethodHookParam) userData;
Method myMethod = (Method) methodHookParam.method;
myMethod.setAccessible(true);
Object[] myArgs = methodHookParam.args;
if (myArgs[0].getClass().equals(AlteredCharSequence.class)) {
myArgs[0] = AlteredCharSequence.make(translatedString, null, 0, 0);
} else if (myArgs[0].getClass().equals(CharBuffer.class)) {
CharBuffer charBuffer = CharBuffer.allocate(translatedString.length() + 1);
charBuffer.append(translatedString);
myArgs[0] = charBuffer;
} else if (myArgs[0].getClass().equals(SpannableString.class)) {
myArgs[0] = new SpannableString(translatedString);
} else if (myArgs[0].getClass().equals(SpannedString.class)) {
myArgs[0] = new SpannedString(translatedString);
} else if (myArgs[0].getClass().equals(String.class)) {
myArgs[0] = translatedString.toString();
} else if (myArgs[0].getClass().equals(StringBuffer.class)) {
myArgs[0] = new StringBuffer(translatedString);
} else if (myArgs[0].getClass().equals(StringBuilder.class)) {
myArgs[0] = new StringBuilder(translatedString);
} else {
myArgs[0] = new SpannableStringBuilder(translatedString);
}
Paint tempPaint = (Paint) myArgs[myArgs.length - 1];
Canvas tempCanvas = (Canvas) methodHookParam.thisObject;
myArgs[myArgs.length - 1] = copyPaint(tempPaint, tempCanvas, myArgs[0].toString());
if (myArgs[1].getClass().equals(int.class)) {
myArgs[1] = 0;
myArgs[2] = translatedString.length();
}
alltrans.hookAccess.acquireUninterruptibly();
unhookMethod(methodHookParam.method, alltrans.setTextHook);
try {
utils.debugLog("In Thread " + Thread.currentThread().getId() + " Invoking original function " + methodHookParam.method.getName() + " and setting text to " + myArgs[0].toString());
myMethod.invoke(methodHookParam.thisObject, myArgs);
} catch (Exception e) {
Log.e("AllTrans", "AllTrans: Got error in invoking method as : " + Log.getStackTraceString(e));
}
hookMethod(methodHookParam.method, alltrans.setTextHook);
alltrans.hookAccess.release();
}
示例15: verifyClobFile
import java.nio.CharBuffer; //導入方法依賴的package包/類
private void verifyClobFile(Path p, String... expectedRecords)
throws Exception {
LobFile.Reader reader = LobFile.open(p, conf);
int recNum = 0;
while (reader.next()) {
// We should have a record of the same length as the expected one.
String expected = expectedRecords[recNum];
assertTrue(reader.isRecordAvailable());
assertEquals(expected.length(), reader.getRecordLen());
Reader r = reader.readClobRecord();
// Read in the record and assert that we got enough characters out.
CharBuffer buf = CharBuffer.allocate(expected.length());
int bytesRead = 0;
while (bytesRead < expected.length()) {
int thisRead = r.read(buf);
LOG.info("performed read of " + thisRead + " chars");
if (-1 == thisRead) {
break;
}
bytesRead += thisRead;
}
LOG.info("Got record of " + bytesRead + " chars");
assertEquals(expected.length(), bytesRead);
char [] charData = buf.array();
String finalRecord = new String(charData);
assertEquals(expected, finalRecord);
recNum++;
}
// Check that we got everything.
assertEquals(expectedRecords.length, recNum);
reader.close();
try {
reader.next();
fail("Expected IOException calling next after close");
} catch (IOException ioe) {
// expected this.
}
// A second close shouldn't hurt anything. This should be a no-op.
reader.close();
}