下面列出了java.nio.CharBuffer#allocate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
/**
* Adapted from {@code com.google.common.io.CharStreams.toString()}.
*/
public static String toString(Reader reader) throws IOException {
if (reader == null) {
return null;
}
try {
StringBuilder to = new StringBuilder();
CharBuffer charBuf = CharBuffer.allocate(BUF_SIZE);
// must cast to super class Buffer otherwise break when running with java 11
Buffer buf = charBuf;
while (reader.read(charBuf) != -1) {
buf.flip();
to.append(charBuf);
buf.clear();
}
return to.toString();
} finally {
ensureClosed(reader);
}
}
public WriterOutputStream(Writer out) {
this.writer = out;
this.decoder = Charset.forName("UTF-8"). //NOI18N
newDecoder().
onMalformedInput(CodingErrorAction.REPLACE).
onUnmappableCharacter(CodingErrorAction.REPLACE).
replaceWith("?"); //NOI18N
this.decoderOut = CharBuffer.allocate(2048);
}
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));
}
private void _verifyMalformed (final String string) throws UnsupportedEncodingException
{
final ByteBuffer in = CharsetTestHelper.wrap (string);
final CharBuffer out = CharBuffer.allocate (1024);
final CoderResult result = tested.newDecoder ().decode (in, out, false); // "€#"
assertTrue (result.isMalformed ());
}
/**
* Left in for reference. Less efficient, but potentially catches more errors. Behavior is
* largely dependent on how strict the JVM's utf8 decoder is. It is possible on some JVMs to
* decode a string that then throws an error when attempting to return it to bytes.
*
* @param bytes Bytes representing a utf8 string
* @return The decoded string
*/
private String decodeStringStreaming(byte[] bytes) {
try {
ByteBuffer input = getBuffer(bytes);
int bufSize = (int) (input.remaining() * localDecoder.get().averageCharsPerByte());
CharBuffer output = CharBuffer.allocate(bufSize);
for (; ; ) {
CoderResult result = localDecoder.get().decode(input, output, false);
if (result.isError()) {
return null;
}
if (result.isUnderflow()) {
break;
}
if (result.isOverflow()) {
// We need more room in our output buffer
bufSize = 2 * bufSize + 1;
CharBuffer o = CharBuffer.allocate(bufSize);
output.flip();
o.put(output);
output = o;
}
}
if (input.remaining() > 0) {
carryOver = input;
}
// Re-encode to work around bugs in UTF-8 decoder
CharBuffer test = CharBuffer.wrap(output);
localEncoder.get().encode(test);
output.flip();
String text = output.toString();
return text;
} catch (CharacterCodingException e) {
return null;
}
}
public static String readAll(Reader reader) throws IOException {
final StringBuilder loadedText = new StringBuilder();
try (Reader bufferedReader = new BufferedReader(reader)) {
final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
while (bufferedReader.read(buffer) > -1) {
buffer.flip();
loadedText.append(buffer);
buffer.clear();
}
return loadedText.toString();
}
}
@Test
public void testWord() throws Exception {
String contents = "some";
String testPath = createFile(contents);
ProcFileReader reader = new ProcFileReader(testPath).start();
CharBuffer buffer = CharBuffer.allocate(20);
assertThat(reader.readWord(buffer).toString()).isEqualTo("some");
assertThat(reader.hasReachedEOF()).isTrue();
}
/**
* Initialize the charbuffer and decoder if they are not yet initialized.
*/
private void initCharBuffer() {
if (charBuffer == null) {
charBuffer = CharBuffer.allocate(INITIAL_CHAR_BUF);
}
if (decoder == null) {
decoder = Charset.forName("UTF-8").newDecoder();
}
}
private void increaseCapacity() {
int newCapacity = (valueslength.capacity() == 0) ? DEFAULT_INIT_SIZE
: valueslength.capacity() < 64 ? valueslength.capacity() * 2
: valueslength.capacity() < 1024 ? valueslength.capacity() * 3 / 2
: valueslength.capacity() * 5 / 4;
final CharBuffer nv = CharBuffer.allocate(newCapacity);
valueslength.rewind();
nv.put(valueslength);
valueslength = nv;
}
@Test
public void testRead_CharBuffer() throws Exception {
final CharBuffer chars = CharBuffer.allocate(1024);
assertEquals("len", FIRST.length() + LAST.length() + 2, this.reader.read(chars));
this.reader.close();
assertMessages(FIRST, LAST);
}
private static void testHProfFileFormat(String vmArgs, long heapSize,
String expectedFormat) throws Exception, IOException,
InterruptedException, FileNotFoundException {
ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(
vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize));
procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
Process largeHeapProc = procBuilder.start();
try (Scanner largeHeapScanner = new Scanner(
largeHeapProc.getInputStream());) {
String pidstring = null;
while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) {
Thread.sleep(500);
}
int pid = Integer.parseInt(pidstring.substring(4,
pidstring.length() - 1));
System.out.println("Extracted pid: " + pid);
JDKToolLauncher jMapLauncher = JDKToolLauncher
.createUsingTestJDK("jmap");
jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-"
+ HEAP_DUMP_FILE_NAME);
jMapLauncher.addToolArg(String.valueOf(pid));
ProcessBuilder jMapProcessBuilder = new ProcessBuilder(
jMapLauncher.getCommand());
System.out.println("jmap command: "
+ Arrays.toString(jMapLauncher.getCommand()));
Process jMapProcess = jMapProcessBuilder.start();
OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess);
analyzer.shouldHaveExitValue(0);
analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME);
analyzer.shouldContain("Heap dump file created");
largeHeapProc.getOutputStream().write('\n');
File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME);
Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found.");
try (Reader reader = new BufferedReader(new FileReader(dumpFile))) {
CharBuffer buf = CharBuffer.allocate(expectedFormat.length());
reader.read(buf);
buf.clear();
Asserts.assertEQ(buf.toString(), expectedFormat,
"Wrong file format. Expected '" + expectedFormat
+ "', but found '" + buf.toString() + "'");
}
System.out.println("Success!");
} finally {
largeHeapProc.destroyForcibly();
}
}
private static void testHProfFileFormat(String vmArgs, long heapSize,
String expectedFormat) throws Exception, IOException,
InterruptedException, FileNotFoundException {
ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(
vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize));
procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
Process largeHeapProc = procBuilder.start();
try (Scanner largeHeapScanner = new Scanner(
largeHeapProc.getInputStream());) {
String pidstring = null;
while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) {
Thread.sleep(500);
}
int pid = Integer.parseInt(pidstring.substring(4,
pidstring.length() - 1));
System.out.println("Extracted pid: " + pid);
JDKToolLauncher jMapLauncher = JDKToolLauncher
.createUsingTestJDK("jmap");
jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-"
+ HEAP_DUMP_FILE_NAME);
jMapLauncher.addToolArg(String.valueOf(pid));
ProcessBuilder jMapProcessBuilder = new ProcessBuilder(
jMapLauncher.getCommand());
System.out.println("jmap command: "
+ Arrays.toString(jMapLauncher.getCommand()));
Process jMapProcess = jMapProcessBuilder.start();
OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess);
analyzer.shouldHaveExitValue(0);
analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME);
analyzer.shouldContain("Heap dump file created");
largeHeapProc.getOutputStream().write('\n');
File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME);
Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found.");
try (Reader reader = new BufferedReader(new FileReader(dumpFile))) {
CharBuffer buf = CharBuffer.allocate(expectedFormat.length());
reader.read(buf);
buf.clear();
Asserts.assertEQ(buf.toString(), expectedFormat,
"Wrong file format. Expected '" + expectedFormat
+ "', but found '" + buf.toString() + "'");
}
System.out.println("Success!");
} finally {
largeHeapProc.destroyForcibly();
}
}
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
if (detectedDecoder == null) {
copyLeadingASCII(src, dst);
// All ASCII?
if (! src.hasRemaining())
return CoderResult.UNDERFLOW;
if (! dst.hasRemaining())
return CoderResult.OVERFLOW;
// We need to perform double, not float, arithmetic; otherwise
// we lose low order bits when src is larger than 2**24.
int cbufsiz = (int)(src.limit() * (double)maxCharsPerByte());
CharBuffer sandbox = CharBuffer.allocate(cbufsiz);
// First try ISO-2022-JP, since there is no ambiguity
Charset cs2022 = Charset.forName("ISO-2022-JP");
DelegatableDecoder dd2022
= (DelegatableDecoder) cs2022.newDecoder();
ByteBuffer src2022 = src.asReadOnlyBuffer();
CoderResult res2022 = dd2022.decodeLoop(src2022, sandbox);
if (! res2022.isError())
return decodeLoop(cs2022, src, dst);
// We must choose between EUC and SJIS
Charset csEUCJ = Charset.forName(EUCJPName);
Charset csSJIS = Charset.forName(SJISName);
DelegatableDecoder ddEUCJ
= (DelegatableDecoder) csEUCJ.newDecoder();
ByteBuffer srcEUCJ = src.asReadOnlyBuffer();
sandbox.clear();
CoderResult resEUCJ = ddEUCJ.decodeLoop(srcEUCJ, sandbox);
// If EUC decoding fails, must be SJIS
if (resEUCJ.isError())
return decodeLoop(csSJIS, src, dst);
DelegatableDecoder ddSJIS
= (DelegatableDecoder) csSJIS.newDecoder();
ByteBuffer srcSJIS = src.asReadOnlyBuffer();
CharBuffer sandboxSJIS = CharBuffer.allocate(cbufsiz);
CoderResult resSJIS = ddSJIS.decodeLoop(srcSJIS, sandboxSJIS);
// If SJIS decoding fails, must be EUC
if (resSJIS.isError())
return decodeLoop(csEUCJ, src, dst);
// From here on, we have some ambiguity, and must guess.
// We prefer input that does not appear to end mid-character.
if (srcEUCJ.position() > srcSJIS.position())
return decodeLoop(csEUCJ, src, dst);
if (srcEUCJ.position() < srcSJIS.position())
return decodeLoop(csSJIS, src, dst);
// end-of-input is after the first byte of the first char?
if (src.position() == srcEUCJ.position())
return CoderResult.UNDERFLOW;
// Use heuristic knowledge of typical Japanese text
sandbox.flip();
Charset guess = looksLikeJapanese(sandbox) ? csEUCJ : csSJIS;
return decodeLoop(guess, src, dst);
}
return detectedDecoder.decodeLoop(src, dst);
}
public DatEventDeserializer(final Configuration configuration, final ReaderRegistry<String> registry) {
super(configuration, registry);
final int bufferSize = configuration.getIntProperty(BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
this.charBuffer = CharBuffer.allocate(bufferSize);
this.charset = configuration.getStringProperty(CHARSET, DEFAULT_CHARSET);
}
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final ComponentLog logger = getLogger();
final Charset inputCharset = Charset.forName(context.getProperty(INPUT_CHARSET).evaluateAttributeExpressions(flowFile).getValue());
final Charset outputCharset = Charset.forName(context.getProperty(OUTPUT_CHARSET).evaluateAttributeExpressions(flowFile).getValue());
final CharBuffer charBuffer = CharBuffer.allocate(MAX_BUFFER_SIZE);
final CharsetDecoder decoder = inputCharset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
decoder.replaceWith("?");
final CharsetEncoder encoder = outputCharset.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
encoder.replaceWith("?".getBytes(outputCharset));
try {
final StopWatch stopWatch = new StopWatch(true);
flowFile = session.write(flowFile, new StreamCallback() {
@Override
public void process(final InputStream rawIn, final OutputStream rawOut) throws IOException {
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(rawIn, decoder), MAX_BUFFER_SIZE);
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(rawOut, encoder), MAX_BUFFER_SIZE)) {
int charsRead;
while ((charsRead = reader.read(charBuffer)) != -1) {
charBuffer.flip();
writer.write(charBuffer.array(), 0, charsRead);
}
writer.flush();
}
}
});
session.getProvenanceReporter().modifyContent(flowFile, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
logger.info("successfully converted characters from {} to {} for {}",
new Object[]{inputCharset, outputCharset, flowFile});
session.transfer(flowFile, REL_SUCCESS);
} catch (final Exception e) {
throw new ProcessException(e);
}
}
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
if (detectedDecoder == null) {
copyLeadingASCII(src, dst);
// All ASCII?
if (! src.hasRemaining())
return CoderResult.UNDERFLOW;
if (! dst.hasRemaining())
return CoderResult.OVERFLOW;
// We need to perform double, not float, arithmetic; otherwise
// we lose low order bits when src is larger than 2**24.
int cbufsiz = (int)(src.limit() * (double)maxCharsPerByte());
CharBuffer sandbox = CharBuffer.allocate(cbufsiz);
// First try ISO-2022-JP, since there is no ambiguity
Charset cs2022 = Charset.forName("ISO-2022-JP");
DelegatableDecoder dd2022
= (DelegatableDecoder) cs2022.newDecoder();
ByteBuffer src2022 = src.asReadOnlyBuffer();
CoderResult res2022 = dd2022.decodeLoop(src2022, sandbox);
if (! res2022.isError())
return decodeLoop(cs2022, src, dst);
// We must choose between EUC and SJIS
Charset csEUCJ = Charset.forName(EUCJPName);
Charset csSJIS = Charset.forName(SJISName);
DelegatableDecoder ddEUCJ
= (DelegatableDecoder) csEUCJ.newDecoder();
ByteBuffer srcEUCJ = src.asReadOnlyBuffer();
sandbox.clear();
CoderResult resEUCJ = ddEUCJ.decodeLoop(srcEUCJ, sandbox);
// If EUC decoding fails, must be SJIS
if (resEUCJ.isError())
return decodeLoop(csSJIS, src, dst);
DelegatableDecoder ddSJIS
= (DelegatableDecoder) csSJIS.newDecoder();
ByteBuffer srcSJIS = src.asReadOnlyBuffer();
CharBuffer sandboxSJIS = CharBuffer.allocate(cbufsiz);
CoderResult resSJIS = ddSJIS.decodeLoop(srcSJIS, sandboxSJIS);
// If SJIS decoding fails, must be EUC
if (resSJIS.isError())
return decodeLoop(csEUCJ, src, dst);
// From here on, we have some ambiguity, and must guess.
// We prefer input that does not appear to end mid-character.
if (srcEUCJ.position() > srcSJIS.position())
return decodeLoop(csEUCJ, src, dst);
if (srcEUCJ.position() < srcSJIS.position())
return decodeLoop(csSJIS, src, dst);
// end-of-input is after the first byte of the first char?
if (src.position() == srcEUCJ.position())
return CoderResult.UNDERFLOW;
// Use heuristic knowledge of typical Japanese text
sandbox.flip();
Charset guess = looksLikeJapanese(sandbox) ? csEUCJ : csSJIS;
return decodeLoop(guess, src, dst);
}
return detectedDecoder.decodeLoop(src, dst);
}
/**
* Constructs a new {@link WriterOutputStream}.
*
* @param writer the target {@link Writer}
* @param decoder the charset decoder
* @param bufferSize the size of the output buffer in number of characters
* @param writeImmediately If <tt>true</tt> the output buffer will be flushed after each
* write operation, i.e. all available data will be written to the
* underlying {@link Writer} immediately. If <tt>false</tt>, the
* output buffer will only be flushed when it overflows or when
* {@link #flush()} or {@link #close()} is called.
* @since 2.1
*/
public WriterOutputStream(Writer writer, CharsetDecoder decoder, int bufferSize, boolean writeImmediately) {
this.writer = writer;
this.decoder = decoder;
this.writeImmediately = writeImmediately;
decoderOut = CharBuffer.allocate(bufferSize);
}
/**
* Convenience method that decodes the remaining content of a single input
* byte buffer into a newly-allocated character buffer.
*
* <p> This method implements an entire <a href="#steps">decoding
* operation</a>; that is, it resets this decoder, then it decodes the
* bytes in the given byte buffer, and finally it flushes this
* decoder. This method should therefore not be invoked if a decoding
* operation is already in progress. </p>
*
* @param in
* The input byte buffer
*
* @return A newly-allocated character buffer containing the result of the
* decoding operation. The buffer's position will be zero and its
* limit will follow the last character written.
*
* @throws IllegalStateException
* If a decoding operation is already in progress
*
* @throws MalformedInputException
* If the byte sequence starting at the input buffer's current
* position is not legal for this charset and the current malformed-input action
* is {@link CodingErrorAction#REPORT}
*
* @throws UnmappableCharacterException
* If the byte sequence starting at the input buffer's current
* position cannot be mapped to an equivalent character sequence and
* the current unmappable-character action is {@link
* CodingErrorAction#REPORT}
*/
public final CharBuffer decode(ByteBuffer in)
throws CharacterCodingException
{
int n = (int)(in.remaining() * averageCharsPerByte());
CharBuffer out = CharBuffer.allocate(n);
if ((n == 0) && (in.remaining() == 0))
return out;
reset();
for (;;) {
CoderResult cr = in.hasRemaining() ?
decode(in, out, true) : CoderResult.UNDERFLOW;
if (cr.isUnderflow())
cr = flush(out);
if (cr.isUnderflow())
break;
if (cr.isOverflow()) {
n = 2*n + 1; // Ensure progress; n might be 0!
CharBuffer o = CharBuffer.allocate(n);
out.flip();
o.put(out);
out = o;
continue;
}
cr.throwException();
}
out.flip();
return out;
}
/**
* Create an array container with specified capacity
*
* @param capacity The capacity of the container
*/
public MappeableArrayContainer(final int capacity) {
content = CharBuffer.allocate(capacity);
}