Java Code Examples for java.nio.CharBuffer#allocate()
The following examples show how to use
java.nio.CharBuffer#allocate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java From feign with Apache License 2.0 | 6 votes |
/** * 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); } }
Example 2
Source File: XMLBeanConvertor.java From netbeans with Apache License 2.0 | 6 votes |
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(); }
Example 3
Source File: MessageBuilderFactory.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
/** * 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; } }
Example 4
Source File: LoggerReaderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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); }
Example 5
Source File: MappeableRunContainer.java From RoaringBitmap with Apache License 2.0 | 5 votes |
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; }
Example 6
Source File: CharSequenceWrapper.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * 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(); } }
Example 7
Source File: ProcFileReaderTest.java From Battery-Metrics with MIT License | 5 votes |
@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(); }
Example 8
Source File: ResourceUtil.java From metafacture-core with Apache License 2.0 | 5 votes |
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(); } }
Example 9
Source File: UTF7CharsetTest.java From ph-commons with Apache License 2.0 | 5 votes |
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 ()); }
Example 10
Source File: NIOJISAutoDetectTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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)); }
Example 11
Source File: WriterOutputStream.java From netbeans with Apache License 2.0 | 5 votes |
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); }
Example 12
Source File: DatEventDeserializer.java From kieker with Apache License 2.0 | 4 votes |
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); }
Example 13
Source File: ConvertCharacterSet.java From localization_nifi with Apache License 2.0 | 4 votes |
@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); } }
Example 14
Source File: JISAutoDetect.java From hottub with GNU General Public License v2.0 | 4 votes |
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); }
Example 15
Source File: JISAutoDetect.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
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); }
Example 16
Source File: JMapHProfLargeHeapTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
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(); } }
Example 17
Source File: JMapHProfLargeHeapTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
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(); } }
Example 18
Source File: WriterOutputStream.java From java-tool with Apache License 2.0 | 3 votes |
/** * 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); }
Example 19
Source File: CharsetDecoder.java From jdk-1.7-annotated with Apache License 2.0 | 3 votes |
/** * 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; }
Example 20
Source File: MappeableArrayContainer.java From RoaringBitmap with Apache License 2.0 | 2 votes |
/** * Create an array container with specified capacity * * @param capacity The capacity of the container */ public MappeableArrayContainer(final int capacity) { content = CharBuffer.allocate(capacity); }