Java Code Examples for java.io.BufferedReader.mark()

The following are Jave code examples for showing how to use mark() of the java.io.BufferedReader class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: hadoop   File: JHLogAnalyzer.java   Source Code and License Vote up 6 votes
private String readLine(BufferedReader reader) throws IOException {
  resBuffer.setLength(0);
  reader.mark(maxJobDelimiterLineLength);
  for(String line = reader.readLine();
            line != null; line = reader.readLine()) {
    if(isEndOfJobLog(line)) {
      if(resBuffer.length() == 0)
        resBuffer.append(line);
      else
        reader.reset();
      break;
    }
    if(resBuffer.length() == 0)
      resBuffer.append(line);
    else if(resBuffer.length() < 32000)
      resBuffer.append(line);
    if(line.endsWith(" .") || line.endsWith("\" ")) {
      break;
    }
    reader.mark(maxJobDelimiterLineLength);
  }
  String result = resBuffer.length() == 0 ? null : resBuffer.toString();
  resBuffer.setLength(0);
  return result;
}
 
Example 2
Project: spring-rest-basis   File: LoggingRequestInterceptor.java   Source Code and License Vote up 6 votes
private void writeBody(final BufferedReader bufferedReader) throws IOException {
    char[] buffer = new char[BUFFER_LENGTH];
    StringBuilder inputStringBuilder = new StringBuilder("Response Body : ");
    bufferedReader.mark(MARK_LENGTH);
    int len = bufferedReader.read(buffer);
    String line = new String(buffer);
    int curLen = 0;
    while (!StringUtils.isEmpty(line) && curLen > -1 && len < MARK_LENGTH) {
        inputStringBuilder.append(line);
        curLen = bufferedReader.read(buffer);
        len += curLen;
        line = new String(buffer);
    }
    if (len == MARK_LENGTH) {
        inputStringBuilder.append("...");
    }
    bufferedReader.reset();
    log(inputStringBuilder.toString());
}
 
Example 3
Project: geomapapp   File: FilesUtil.java   Source Code and License Vote up 6 votes
public static boolean sessionAlreadyImported(File xmlImportFile, File xmlSessionFile) throws IOException {
	BufferedReader br1 = new BufferedReader(new FileReader(xmlSessionFile)); //sessionfile
	BufferedReader br2 = new BufferedReader(new FileReader(xmlImportFile)); //desktop import
	String s1, s2;
	
	s1 = br1.readLine();
	s2 = br2.readLine();
	s2 = br2.readLine();
	br2.mark(2000);;
	while(s1 != null) {		
		while (s1 != null && s2 != null && s1.equals(s2)) {
			s1 = br1.readLine();
			s2 = br2.readLine();
			if (s2 == null) {
				br1.close();
				br2.close();
				return true;
			}
		}
		br2.reset();
		s1 = br1.readLine();
	}
	br1.close();
	br2.close();
	return false;
}
 
Example 4
Project: DNASDKJava   File: JBigInt.java   Source Code and License Vote up 5 votes
static JBigInt parseNumber(BufferedReader reader) throws IOException {
    skipSpace(reader);
    StringBuilder sb = new StringBuilder();
    while (true) {
    	reader.mark(1);
        int c = reader.read();
        if (c >= '0' && c <= '9' || c == '.' || c == '-') {
            sb.append((char)c);
        } else {
        	reader.reset();
            break;
        }
    }
    return new JBigInt(sb.toString());
}
 
Example 5
Project: DNASDKJava   File: JNumber.java   Source Code and License Vote up 5 votes
static JNumber parseNumber(BufferedReader reader) throws IOException {
    skipSpace(reader);
    StringBuilder sb = new StringBuilder();
    while (true) {
    	reader.mark(1);
        int c = reader.read();
        if (c >= '0' && c <= '9' || c == '.' || c == '-') {
            sb.append((char)c);
        } else {
        	reader.reset();
            break;
        }
    }
    return new JNumber(Double.parseDouble(sb.toString()));
}
 
Example 6
Project: BookyMcBookface   File: EpubBook.java   Source Code and License Vote up 5 votes
private static List<String> getRootFilesFromContainer(BufferedReader containerxml) {

        List<String> rootFiles = new ArrayList<>();

        try {

            containerxml.mark(4);
            if ('\ufeff' != containerxml.read()) containerxml.reset(); // not the BOM marker

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(containerxml);

            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("rootfile")) {
                        for (int i = 0; i < xpp.getAttributeCount(); i++) {
                            if (xpp.getAttributeName(i).equals("full-path")) {
                                rootFiles.add(xpp.getAttributeValue(i));
                            }
                        }
                    }

                }
                eventType = xpp.next();
            }
        } catch (Exception e) {
            Log.e("BMBF", "Error parsing xml " + e, e);
        }

        return rootFiles;

    }
 
Example 7
Project: BookyMcBookface   File: EpubBook.java   Source Code and License Vote up 5 votes
private static Map<String,?> processToc(BufferedReader tocReader) {
    Map<String,Object> bookdat = new LinkedHashMap<>();

    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    XPathFactory factory = XPathFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = dfactory.newDocumentBuilder();

        tocReader.mark(4);
        if ('\ufeff' != tocReader.read()) tocReader.reset(); // not the BOM marker

        Document doc = builder.parse(new InputSource(tocReader));

        XPath tocPath = factory.newXPath();
        tocPath.setNamespaceContext(tocnsc);

        Node nav = (Node)tocPath.evaluate("/ncx/navMap", doc, XPathConstants.NODE);

        int total = readNavPoint(nav, tocPath, bookdat, 0);
        bookdat.put(TOCCOUNT, total);

    } catch (ParserConfigurationException | IOException | SAXException | XPathExpressionException e) {
        Log.e("BMBF", "Error parsing xml " + e.getMessage(), e);
    }
    return bookdat;
}
 
Example 8
Project: morf   File: Version2to4TransformingReader.java   Source Code and License Vote up 5 votes
/**
 * Tests whether a given input stream contains XML format 2, and therefore
 * should have the transform applied.
 * <p>
 * This is designed to match the known output format of
 * {@link XmlDataSetConsumer} which previously produced invalid XML. It is
 * deliberately brittle. There is no need for a more intelligent XML parser
 * here.
 * </p>
 *
 * @param bufferedReader The input stream in a buffered reader
 * @return true if the transform should be applied. (because it's format 2)
 */
static int readVersion(BufferedReader bufferedReader) {
  try {
    bufferedReader.mark(1024); // arbitrary read-ahead limit - that's enough to get the info we want
    try {
      char[] buffer = new char[1024];

      int read = bufferedReader.read(buffer);
      if (read == -1) {
        return -1;
      }

      String content = new String(buffer, 0, read);

      // Apply the transform if the version number is 2 or 1
      Pattern pattern = Pattern.compile("table\\sversion=\"(\\d+)\""); //
      Matcher matcher = pattern.matcher(content);

      if (!matcher.find()) {
        return -1;
      } else {
        return Integer.parseInt(matcher.group(1));
      }

    } finally {
      bufferedReader.reset();
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}