List of usage examples for java.nio CharBuffer allocate
public static CharBuffer allocate(int capacity)
From source file:com.dclab.preparation.ReadTest.java
public String scan(Reader r) { try {//from ww w.j a v a2 s . c o m CharBuffer cb = CharBuffer.allocate(MAX_CAPACITY); int i = r.read(cb); Logger.getAnonymousLogger().log(Level.INFO, "read {0} bytes", i); String content = cb.toString(); return extract(content); } catch (IOException ex) { Logger.getLogger(ReadTest.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:com.compomics.colims.core.util.AccessionConverter.java
/** * fetches the html page at the passed url as a string * * @param aUrl the url to fetch the html page from * @return the page at the url in textual form * @throws IOException//from w w w .ja va 2s.c om */ public static String getPage(String aUrl) throws IOException { StringBuilder input = new StringBuilder(); String htmlPage; try (Reader r = openReader(aUrl)) { CharBuffer buffer = CharBuffer.allocate(256); while ((r.read(buffer)) != -1) { input.append(buffer.flip()); buffer.clear(); } } catch (IOException e) { throw new IOException("EMBL-EBI server is not available at the moment, please try again later."); } htmlPage = input.toString(); return htmlPage; }
From source file:ape.NetworkDisconnectCommand.java
/** * This method writes to the Stand output *//*from w ww.j av a 2 s. c om*/ private boolean writeSTDOut(Process p) { BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); int count; CharBuffer cbuf = CharBuffer.allocate(99999); try { count = stdInput.read(cbuf); if (count != -1) cbuf.array()[count] = '\0'; else if (cbuf.array()[0] != '\0') count = cbuf.array().length; else count = 0; for (int i = 0; i < count; i++) System.out.print(cbuf.get(i)); } catch (IOException e) { System.err.println( "Writing Stdout in NetworkDisconnectCommand catches an exception, Turn on the VERBOSE flag to see the stack trace"); e.printStackTrace(); return false; } try { stdInput.close(); } catch (IOException e) { System.err.println("Unable to close the IOStream"); e.printStackTrace(); return false; } return true; }
From source file:com.examples.with.different.packagename.idnaming.ReaderInputStream.java
/** * Construct a new {@link ReaderInputStream}. * //from www . j ava 2s.com * @param reader the target {@link Reader} * @param encoder the charset encoder * @param bufferSize the size of the input buffer in number of characters * @since 2.1 */ public ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) { this.reader = reader; this.encoder = encoder; this.encoderIn = CharBuffer.allocate(bufferSize); this.encoderIn.flip(); this.encoderOut = ByteBuffer.allocate(128); this.encoderOut.flip(); }
From source file:com.hypersocket.ui.ReaderInputStream.java
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param encoder the charset encoder//from w w w . j ava 2 s . c o m * @param bufferSize the size of the input buffer in number of characters * @since 2.1 */ public ReaderInputStream(final Reader reader, final CharsetEncoder encoder, final int bufferSize) { this.reader = reader; this.encoder = encoder; this.encoderIn = CharBuffer.allocate(bufferSize); this.encoderIn.flip(); this.encoderOut = ByteBuffer.allocate(128); this.encoderOut.flip(); }
From source file:it.tidalwave.northernwind.frontend.ui.component.htmltemplate.TextHolder.java
private void loadTemplate() throws IOException { // FIXME: this should be done only once... Resource resource = null;/* w w w.j ava 2s. co m*/ for (Class<?> clazz = getClass(); clazz.getSuperclass() != null; clazz = clazz.getSuperclass()) { final String templateName = clazz.getSimpleName() + ".txt"; resource = new ClassPathResource(templateName, clazz); if (resource.exists()) { break; } } try { if (resource == null) { throw new FileNotFoundException(); } final @Cleanup Reader r = new InputStreamReader(resource.getInputStream()); final CharBuffer charBuffer = CharBuffer.allocate((int) resource.contentLength()); final int length = r.read(charBuffer); r.close(); template = new String(charBuffer.array(), 0, length); } catch (FileNotFoundException e) // no specific template, fallback { log.warn("No template for {}, using default", getClass().getSimpleName()); template = "$content$\n"; } }
From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java
/** * Construct double array trie which is equivalent to input trie * * @param trie normal trie, which contains all dictionary words */// w w w. j av a2 s . c o m public void build(Trie trie) { ProgressLog.begin("building " + (compact ? "compact" : "sparse") + " trie"); baseBuffer = IntBuffer.allocate(BASE_CHECK_INITIAL_SIZE); baseBuffer.put(0, 1); checkBuffer = IntBuffer.allocate(BASE_CHECK_INITIAL_SIZE); tailBuffer = CharBuffer.allocate(TAIL_INITIAL_SIZE); add(-1, 0, trie.getRoot()); reportUtilizationRate(); ProgressLog.end(); }
From source file:com.gargoylesoftware.htmlunit.NoHttpResponseTest.java
@Override public void run() { try {/* www.j a va 2s .co m*/ serverSocket_ = new ServerSocket(port_); started_.set(true); LOG.info("Starting listening on port " + port_); while (!shutdown_) { final Socket s = serverSocket_.accept(); final BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); final CharBuffer cb = CharBuffer.allocate(5000); br.read(cb); cb.flip(); final String in = cb.toString(); cb.rewind(); final RawResponseData responseData = getResponseData(in); if (responseData == null || responseData.getStringContent() == DROP_CONNECTION) { LOG.info("Closing impolitely in & output streams"); s.getOutputStream().close(); } else { final PrintWriter pw = new PrintWriter(s.getOutputStream()); pw.println("HTTP/1.0 " + responseData.getStatusCode() + " " + responseData.getStatusMessage()); for (final NameValuePair header : responseData.getHeaders()) { pw.println(header.getName() + ": " + header.getValue()); } pw.println(); pw.println(responseData.getStringContent()); pw.println(); pw.flush(); pw.close(); } br.close(); s.close(); } } catch (final SocketException e) { if (!shutdown_) { LOG.error(e); } } catch (final IOException e) { LOG.error(e); } finally { LOG.info("Finished listening on port " + port_); } }
From source file:net.sf.smbt.touchosc.utils.TouchOSCUtils.java
public String loadTouchOscXML(String zipTouchoscFilePath) { List<String> touchoscFilePathList = new ArrayList<String>(); IPath path = new Path(zipTouchoscFilePath); String xml = ""; try {//ww w . j a v a2s. co m FileInputStream touchoscFile = new FileInputStream(zipTouchoscFilePath); ZipInputStream fileIS = new ZipInputStream(touchoscFile); ZipEntry zEntry = null; while ((zEntry = fileIS.getNextEntry()) != null) { if (zEntry.getName().endsWith(".xml")) { touchoscFilePathList.add(path.removeLastSegments(1) + "/_" + path.lastSegment()); } BufferedReader reader = new BufferedReader(new InputStreamReader(fileIS, Charset.forName("UTF-8"))); CharBuffer charBuffer = CharBuffer.allocate(65535); while (reader.read(charBuffer) != -1) charBuffer.flip(); xml = charBuffer.toString(); } fileIS.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } return xml; }
From source file:com.twentyn.patentScorer.PatentScorer.java
@Override public void processPatentText(File patentFile, Reader patentTextReader, int patentTextLength) throws IOException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException, XPathExpressionException { System.out.println("Patent text length: " + patentTextLength); CharBuffer buff = CharBuffer.allocate(patentTextLength); int read = patentTextReader.read(buff); System.out.println("Read bytes: " + read); patentTextReader.reset();/*from w ww .j a va 2s . c o m*/ String fullContent = new String(buff.array()); PatentDocument patentDocument = PatentDocument .patentDocumentFromXMLStream(new ReaderInputStream(patentTextReader, Charset.forName("utf-8"))); if (patentDocument == null) { LOGGER.info("Found non-patent type document, skipping."); return; } double pr = this.patentModel.ProbabilityOf(fullContent); this.outputWriter .write(objectMapper.writeValueAsString(new ClassificationResult(patentDocument.getFileId(), pr))); this.outputWriter.write(LINE_SEPARATOR); System.out.println("Doc " + patentDocument.getFileId() + " has score " + pr); }