List of usage examples for java.io BufferedReader mark
public void mark(int readAheadLimit) throws IOException
From source file:com.norconex.importer.parser.impl.xfdl.XFDLParser.java
private void parse(BufferedReader reader, Writer out, ImporterMetadata metadata) throws IOException, ParserConfigurationException, SAXException { reader.mark(MAGIC_BASE64.length); char[] signature = new char[MAGIC_BASE64.length]; int num = reader.read(signature); reader.reset();/*from w ww . j ava 2s. c om*/ if (num == -1) { return; } //--- Create XML DOM --- DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document dom = null; if (Arrays.equals(signature, MAGIC_BASE64)) { // skip first line reader.readLine(); // un-encode first byte[] compressedContent = Base64.decodeBase64(IOUtils.toString(reader)); // deal with compression InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressedContent)); dom = docBuilder.parse(is); IOUtils.closeQuietly(is); } else { dom = docBuilder.parse(new InputSource(reader)); } parseXML(dom, out, metadata); }
From source file:com.microsoft.tfs.util.NewlineUtils.java
/** * <p>/* w w w. j a v a2 s . com*/ * Replaces newlines read from the given {@link Reader} with the * <code>replacement</code> argument and writes the results to the given * {@link Writer}. * </p> * <p> * This method internally wraps the given reader in a {@link BufferedReader} * (so it can mark and rewind the stream to read multi-character EOL * sequences), so the caller may omit its own buffering layer. The * {@link Writer} is not automatically buffered. * </p> * * @see #replaceNewlines(String, String, boolean) * * @param reader * The input {@link Reader} to read characters from. If the input is * <code>null</code>, the method returns immediately (no characters * are written to the {@link Writer}). * @param writer * The output {@link Writer} where characters and converted newlines * are written (must not be <code>null</code>) * @param replacement * The replacement <code>String</code> for newlines (must not be * <code>null</code>) * @param groupNewlines * controls the behavior of this method when there are consecutive * newlines in the input (see the Javadoc for the method that takes * String input for details) * @throws IOException * if an error occurred reading from the {@link Reader}, writing to * the {@link Writer}. These errors may include the discovery of * invalid character sequences in the input. */ public static void replaceNewlines(final Reader reader, final Writer writer, final String replacement, final boolean groupNewlines) throws IOException { if (reader == null) { return; } Check.notNull(writer, "output"); //$NON-NLS-1$ Check.notNull(replacement, "replacement"); //$NON-NLS-1$ final BufferedReader bufferedReader = new BufferedReader(reader); boolean inNewlineGroup = false; int currentCharacter; while ((currentCharacter = bufferedReader.read()) != -1) { final boolean isNewlineCharacter = isNewlineCharacter((char) currentCharacter); /* * Move the index past the second character of the Windows newline * sequence (CRLF), if applicable. */ if (currentCharacter == CARRIAGE_RETURN) { /* * Set a mark so we can rewind after peeking. */ bufferedReader.mark(BUFFERED_READER_MARK_READ_AHEAD_CHARACTERS); final int nextCharacter = bufferedReader.read(); if (nextCharacter == LINE_FEED) { /* * Is a line feed, set this as the current character for * evaluation this iteration. */ currentCharacter = nextCharacter; } else { /* * Not a line feed or end of stream. */ bufferedReader.reset(); } } if (isNewlineCharacter) { if (groupNewlines) { /* * Just record that we've entered a newline group - we'll * apply the replacement string to the result after we've * exited the group. */ inNewlineGroup = true; } else { /* * Not in grouping mode - each distinct newline character * gets its own replacement. */ writer.write(replacement); } } else { if (groupNewlines && inNewlineGroup) { /* * Exiting a newline group - apply the replacement to the * result. */ writer.write(replacement); inNewlineGroup = false; } writer.write(currentCharacter); } } if (inNewlineGroup) { /* * The input string terminated while we were in a newline group, so * be sure to add in the replacement character for this group * (without this check, newline groups at the end of strings would * be ignored). */ writer.write(replacement); inNewlineGroup = false; } }
From source file:massbank.CallCgi.java
public void run() { String progName = "CallCgi"; String msg = ""; HttpClient client = new HttpClient(); // ^CAEgl(msec)Zbg client.setTimeout(m_timeout * 1000); PostMethod method = new PostMethod(this.m_url); String strParam = ""; if (m_params != null && m_params.size() > 0) { for (Enumeration keys = m_params.keys(); keys.hasMoreElements();) { String key = (String) keys.nextElement(); if (!key.equals("inst_grp") && !key.equals("inst") && !key.equals("ms") && !key.equals("inst_grp_adv") && !key.equals("inst_adv") && !key.equals("ms_adv")) { // L?[InstrumentType,MSTypeO??Stringp??[^ String val = (String) m_params.get(key); strParam += key + "=" + val + "&"; method.addParameter(key, val); } else { // L?[InstrumentType,MSType??Stringzp??[^ String[] vals = (String[]) m_params.get(key); for (int i = 0; i < vals.length; i++) { strParam += key + "=" + vals[i] + "&"; method.addParameter(key, vals[i]); }/* ww w. j a v a 2 s.co m*/ } } strParam = strParam.substring(0, strParam.length() - 1); } try { // ?s int statusCode = client.executeMethod(method); // Xe?[^XR?[h`FbN if (statusCode != HttpStatus.SC_OK) { // G?[ msg = method.getStatusLine().toString() + "\n" + "URL : " + this.m_url; msg += "\nPARAM : " + strParam; MassBankLog.ErrorLog(progName, msg, m_context); return; } // X|X // this.result = method.getResponseBodyAsString(); /** * modification start * Use method.getResponseBodyAsStream() rather * than method.getResponseBodyAsString() (marked as deprecated) for updated HttpClient library. * Prevents logging of message: * "Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended." */ String charset = method.getResponseCharSet(); InputStream is = method.getResponseBodyAsStream(); StringBuilder sb = new StringBuilder(); String line = ""; if (is != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset)); while ((line = reader.readLine()) != null) { reader.mark(2000); String forward = reader.readLine(); if ((line.equals("") || line.equals("\n") || line.equals("OK")) && forward == null) sb.append(line); // append last line to StringBuilder else if (forward != null) sb.append(line).append("\n"); // append current line with explicit line break else sb.append(line); reader.reset(); } reader.close(); is.close(); // this.result = sb.toString().trim(); this.result = sb.toString(); // trim() deleted. because the last [\t] and [\n] are removed. if (this.result.endsWith("\n")) // remove trailing line break { int pos = this.result.lastIndexOf("\n"); this.result = this.result.substring(0, pos); } } else { this.result = ""; } /** * modification end */ } catch (Exception e) { // G?[ msg = e.toString() + "\n" + "URL : " + this.m_url; msg += "\nPARAM : " + strParam; MassBankLog.ErrorLog(progName, msg, m_context); } finally { // RlNV method.releaseConnection(); } }
From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVAutoDetect.java
private List<LineStats> generateStats(BufferedReader br, Character separatorChar) throws IOException { List<LineStats> lineStats = new Vector<>(); String line;//from ww w.j ava 2s. c o m int rows = 0; br.mark(32765); while ((line = br.readLine()) != null && rows < 100) { LineStats stats = new LineStats(line, separatorChar); rows++; lineStats.add(stats); } br.reset(); return lineStats; }
From source file:com.vuze.android.remote.rpc.RestJsonClient.java
public static Map<?, ?> connect(String id, String url, Map<?, ?> jsonPost, Header[] headers, UsernamePasswordCredentials creds, boolean sendGzip) throws RPCException { long readTime = 0; long connSetupTime = 0; long connTime = 0; int bytesRead = 0; if (DEBUG_DETAILED) { Log.d(TAG, id + "] Execute " + url); }//from ww w .ja va2 s .c om long now = System.currentTimeMillis(); long then; Map<?, ?> json = Collections.EMPTY_MAP; try { URI uri = new URI(url); int port = uri.getPort(); BasicHttpParams basicHttpParams = new BasicHttpParams(); HttpProtocolParams.setUserAgent(basicHttpParams, "Vuze Android Remote"); DefaultHttpClient httpclient; if ("https".equals(uri.getScheme())) { httpclient = MySSLSocketFactory.getNewHttpClient(port); } else { httpclient = new DefaultHttpClient(basicHttpParams); } //AndroidHttpClient.newInstance("Vuze Android Remote"); // This doesn't set the "Authorization" header!? httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), creds); // Prepare a request object HttpRequestBase httpRequest = jsonPost == null ? new HttpGet(uri) : new HttpPost(uri); // IllegalArgumentException if (creds != null) { byte[] toEncode = (creds.getUserName() + ":" + creds.getPassword()).getBytes(); String encoding = Base64Encode.encodeToString(toEncode, 0, toEncode.length); httpRequest.setHeader("Authorization", "Basic " + encoding); } if (jsonPost != null) { HttpPost post = (HttpPost) httpRequest; String postString = JSONUtils.encodeToJSON(jsonPost); if (AndroidUtils.DEBUG_RPC) { Log.d(TAG, id + "] Post: " + postString); } AbstractHttpEntity entity = (sendGzip && Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) ? getCompressedEntity(postString) : new StringEntity(postString); post.setEntity(entity); post.setHeader("Accept", "application/json"); post.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { setupRequestFroyo(httpRequest); } if (headers != null) { for (Header header : headers) { httpRequest.setHeader(header); } } // Execute the request HttpResponse response; then = System.currentTimeMillis(); if (AndroidUtils.DEBUG_RPC) { connSetupTime = (then - now); now = then; } httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException e, int i, HttpContext httpContext) { if (i < 2) { return true; } return false; } }); response = httpclient.execute(httpRequest); then = System.currentTimeMillis(); if (AndroidUtils.DEBUG_RPC) { connTime = (then - now); now = then; } HttpEntity entity = response.getEntity(); // XXX STATUSCODE! StatusLine statusLine = response.getStatusLine(); if (AndroidUtils.DEBUG_RPC) { Log.d(TAG, "StatusCode: " + statusLine.getStatusCode()); } if (entity != null) { long contentLength = entity.getContentLength(); if (contentLength >= Integer.MAX_VALUE - 2) { throw new RPCException("JSON response too large"); } // A Simple JSON Response Read InputStream instream = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) ? getUngzippedContent(entity) : entity.getContent(); InputStreamReader isr = new InputStreamReader(instream, "utf8"); StringBuilder sb = null; BufferedReader br = null; // JSONReader is 10x slower, plus I get more OOM errors.. :( // final boolean useStringBuffer = contentLength > (4 * 1024 * 1024) ? false // : DEFAULT_USE_STRINGBUFFER; final boolean useStringBuffer = DEFAULT_USE_STRINGBUFFER; if (useStringBuffer) { // Setting capacity saves StringBuffer from going through many // enlargeBuffers, and hopefully allows toString to not make a copy sb = new StringBuilder(contentLength > 512 ? (int) contentLength + 2 : 512); } else { if (AndroidUtils.DEBUG_RPC) { Log.d(TAG, "Using BR. ContentLength = " + contentLength); } br = new BufferedReader(isr, 8192); br.mark(32767); } try { // 9775 files on Nexus 7 (~2,258,731 bytes) // fastjson 1.1.46 (String) : 527- 624ms // fastjson 1.1.39 (String) : 924-1054ms // fastjson 1.1.39 (StringBuilder): 1227-1463ms // fastjson 1.1.39 (BR) : 2233-2260ms // fastjson 1.1.39 (isr) : 2312ms // GSON 2.2.4 (String) : 1539-1760ms // GSON 2.2.4 (BufferedReader) : 2646-3060ms // JSON-SMART 1.3.1 (String) : 572- 744ms (OOMs more often than fastjson) if (useStringBuffer) { char c[] = new char[8192]; while (true) { int read = isr.read(c); if (read < 0) { break; } sb.append(c, 0, read); } if (AndroidUtils.DEBUG_RPC) { then = System.currentTimeMillis(); if (DEBUG_DETAILED) { if (sb.length() > 2000) { Log.d(TAG, id + "] " + sb.substring(0, 2000) + "..."); } else { Log.d(TAG, id + "] " + sb.toString()); } } bytesRead = sb.length(); readTime = (then - now); now = then; } json = JSONUtils.decodeJSON(sb.toString()); //json = JSONUtilsGSON.decodeJSON(sb.toString()); } else { //json = JSONUtils.decodeJSON(isr); json = JSONUtils.decodeJSON(br); //json = JSONUtilsGSON.decodeJSON(br); } } catch (Exception pe) { // StatusLine statusLine = response.getStatusLine(); if (statusLine != null && statusLine.getStatusCode() == 409) { throw new RPCException(response, "409"); } try { String line; if (useStringBuffer) { line = sb.subSequence(0, Math.min(128, sb.length())).toString(); } else { br.reset(); line = br.readLine().trim(); } isr.close(); if (AndroidUtils.DEBUG_RPC) { Log.d(TAG, id + "]line: " + line); } Header contentType = entity.getContentType(); if (line.startsWith("<") || line.contains("<html") || (contentType != null && contentType.getValue().startsWith("text/html"))) { // TODO: use android strings.xml throw new RPCException(response, "Could not retrieve remote client location information. The most common cause is being on a guest wifi that requires login before using the internet."); } } catch (IOException ignore) { } Log.e(TAG, id, pe); if (statusLine != null) { String msg = statusLine.getStatusCode() + ": " + statusLine.getReasonPhrase() + "\n" + pe.getMessage(); throw new RPCException(msg, pe); } throw new RPCException(pe); } finally { closeOnNewThread(useStringBuffer ? isr : br); } if (AndroidUtils.DEBUG_RPC) { // Log.d(TAG, id + "]JSON Result: " + json); } } } catch (RPCException e) { throw e; } catch (Throwable e) { Log.e(TAG, id, e); throw new RPCException(e); } if (AndroidUtils.DEBUG_RPC) { then = System.currentTimeMillis(); Log.d(TAG, id + "] conn " + connSetupTime + "/" + connTime + "ms. Read " + bytesRead + " in " + readTime + "ms, parsed in " + (then - now) + "ms"); } return json; }
From source file:org.alfresco.repo.search.impl.lucene.analysis.VerbatimMLAnalayser.java
@Override public TokenStream tokenStream(String fieldName, Reader reader) { // We use read ahead to get the language info - if this does not exist we need to restart // an use the default - there foer we need mark and restore. if (!(reader instanceof BufferedReader)) { BufferedReader breader = new BufferedReader(reader); try {//from w ww .ja v a2 s . co m if (!breader.markSupported()) { throw new AnalysisException( "Multilingual tokenisation requires a reader that supports marks and reset"); } breader.mark(100); StringBuilder builder = new StringBuilder(); if (breader.read() == '\u0000') { String language = ""; String country = ""; String varient = ""; char c; int count = 0; while ((c = (char) breader.read()) != '\u0000') { if (count++ > 99) { breader.reset(); return getAnalyser().tokenStream(fieldName, breader); } if (c == '_') { if (language.length() == 0) { language = builder.toString(); } else if (country.length() == 0) { country = builder.toString(); } else if (varient.length() == 0) { varient = builder.toString(); } else { breader.reset(); return getAnalyser().tokenStream(fieldName, breader); } builder = new StringBuilder(); } else { builder.append(c); } } if (builder.length() > 0) { if (language.length() == 0) { language = builder.toString(); } else if (country.length() == 0) { country = builder.toString(); } else if (varient.length() == 0) { varient = builder.toString(); } else { breader.reset(); return getAnalyser().tokenStream(fieldName, breader); } } Locale locale = new Locale(language, country, varient); // leave the reader where it is .... return new MLTokenDuplicator(getAnalyser().tokenStream(fieldName, breader), locale, breader, mlAnalaysisMode); } else { breader.reset(); return getAnalyser().tokenStream(fieldName, breader); } } catch (IOException io) { try { breader.reset(); } catch (IOException e) { throw new AnalysisException("Failed to reset buffered reader - token stream will be invalid", e); } return getAnalyser().tokenStream(fieldName, breader); } } else { throw new AnalysisException("Multilingual tokenisation requires a buffered reader"); } }
From source file:org.apache.jmeter.save.CSVSaveService.java
/** * Read Samples from a file; handles quoted strings. * //from w w w.jav a2 s .c om * @param filename * input file * @param visualizer * where to send the results * @param resultCollector * the parent collector * @throws IOException * when the file referenced by <code>filename</code> can't be * read correctly */ public static void processSamples(String filename, Visualizer visualizer, ResultCollector resultCollector) throws IOException { BufferedReader dataReader = null; final boolean errorsOnly = resultCollector.isErrorLogging(); final boolean successOnly = resultCollector.isSuccessOnlyLogging(); try { dataReader = new BufferedReader( new InputStreamReader(new FileInputStream(filename), SaveService.getFileEncoding("UTF-8"))); dataReader.mark(400);// Enough to read the header column names // Get the first line, and see if it is the header String line = dataReader.readLine(); if (line == null) { throw new IOException(filename + ": unable to read header line"); } long lineNumber = 1; SampleSaveConfiguration saveConfig = CSVSaveService.getSampleSaveConfiguration(line, filename); if (saveConfig == null) {// not a valid header log.info(filename + " does not appear to have a valid header. Using default configuration."); saveConfig = (SampleSaveConfiguration) resultCollector.getSaveConfig().clone(); // may change the format later dataReader.reset(); // restart from beginning lineNumber = 0; } String[] parts; final char delim = saveConfig.getDelimiter().charAt(0); // TODO: does it matter that an empty line will terminate the loop? // CSV output files should never contain empty lines, so probably // not // If so, then need to check whether the reader is at EOF while ((parts = csvReadFile(dataReader, delim)).length != 0) { lineNumber++; SampleEvent event = CSVSaveService.makeResultFromDelimitedString(parts, saveConfig, lineNumber); if (event != null) { final SampleResult result = event.getResult(); if (ResultCollector.isSampleWanted(result.isSuccessful(), errorsOnly, successOnly)) { visualizer.add(result); } } } } finally { JOrphanUtils.closeQuietly(dataReader); } }
From source file:uk.ac.sanger.cgp.dbcon.sqllibs.SqlLibraries.java
/** * Takes in a string and looks to see if it is an XML definition file *///from w w w . j a v a 2 s .c o m private LibraryParser detectAndCreateParser(Reader reader) throws SqlLibraryRuntimeException { LibraryParser parser = null; try { BufferedReader br = new BufferedReader(reader); if (!br.markSupported()) { throw new SqlLibraryRuntimeException("Reader does not support marking - aborting"); } br.mark(50); char[] xmlDefArray = new char[6]; br.read(xmlDefArray, 0, 6); br.reset(); String xmlDef = new String(xmlDefArray); if (xmlDef.matches("^.*<\\?xml.*")) { if (log.isInfoEnabled()) log.info("Detected .xml sql library"); parser = new XMLLibraryParser(); } else { if (log.isInfoEnabled()) log.info("Detected .sqllib sql library"); parser = new SqlLibLibraryParser(); } } catch (IOException e) { throw new SqlLibraryRuntimeException("Error occured whilst testing the InputStream for XML syntax", e); } return parser; }
From source file:org.tellervo.desktop.io.TridasDoc.java
/** * Quickly check to see if it's an XML document * @param r//from ww w . j a v a 2 s . c o m * @throws IOException */ private void quickVerify(BufferedReader r) throws IOException { r.mark(4096); String firstLine = r.readLine(); if (firstLine == null || !firstLine.startsWith("<?xml")) throw new WrongFiletypeException(); r.reset(); }