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


Java CharBuffer.allocate方法代码示例

本文整理汇总了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;
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:21,代码来源:WsFrameBase.java

示例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.");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:StockName.java

示例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;
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:18,代码来源:MessageInbound.java

示例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();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:XMLBeanConvertor.java

示例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;
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:18,代码来源:MessageInbound.java

示例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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:40,代码来源:BinaryPrimitivesTest.java

示例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();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:9,代码来源:TokenDumpCheck.java

示例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);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:9,代码来源:BufferedCharSequence.java

示例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);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:21,代码来源:ReaderInputStream.java

示例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);
}
 
开发者ID:FastBootWeixin,项目名称:FastBootWeixin,代码行数:22,代码来源:ReaderInputStream.java

示例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);
}
 
开发者ID:dremio,项目名称:dremio-oss,代码行数:17,代码来源:CharSequenceWrapper.java

示例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();
    }
 
开发者ID:akhilkedia,项目名称:AllTrans,代码行数:44,代码来源:SetTextHookHandler.java

示例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)
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:7,代码来源:CharStreams.java

示例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();
    }
 
开发者ID:akhilkedia,项目名称:AllTrans,代码行数:47,代码来源:DrawTextHookHandler.java

示例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();
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:52,代码来源:TestLobFile.java


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