List of usage examples for java.io Reader reset
public void reset() throws IOException
From source file:net.metanotion.json.StreamingParser.java
private String lexDigits(final Reader in) throws IOException { final StringBuilder sb = new StringBuilder(); while (true) { in.mark(MAX_BUFFER);/*from w w w .j av a 2s.c o m*/ final int c = in.read(); if (Character.isDigit(c)) { sb.append(Character.toChars(c)); } else { in.reset(); if (sb.length() == 0) { throw new ParserException(EXPECTED_DIGIT); } return sb.toString(); } } }
From source file:net.metanotion.json.StreamingParser.java
private Lexeme maybeNumber(final Reader in, int firstChar) throws IOException { // this might be a number, if it is, lex it and return the token, otherwise throw an exception. final String integer = lexInt(in, firstChar); in.mark(MAX_BUFFER);/*from www . j ava 2 s . c om*/ final int c = in.read(); if (c == '.') { final String decimal = integer + lexFraction(in); return new Lexeme(Token.FLOAT, Double.valueOf(decimal)); } else if (Character.toLowerCase(c) == 'e') { in.reset(); final String decimal = integer + lexExp(in); return new Lexeme(Token.FLOAT, Double.valueOf(decimal)); } else { in.reset(); return new Lexeme(Token.INT, Long.valueOf(integer)); } }
From source file:org.zilverline.extractors.AbstractExtractor.java
/** * This method extracts all relevant info of the file as an ParsedFileInfo object. Uses getContent as callback. * /* ww w . j a v a 2s.c o m*/ * @param f the File to extract content from * * @return ParsedFileInfo the object containing relevant info of the provided file */ public final ParsedFileInfo extractInfo(final File f) { if (f == null) { log.warn("Something went terribly wrong, file = null, returning null "); return null; } try { setFile(f); Reader reader = getContent(f); fileInfo.setReader(reader); // get the summary from the reader if (reader != null) { String summary = fileInfo.getSummary(); if (!StringUtils.hasText(summary)) { char[] sumChars = new char[SUMMARY_SIZE]; int numChars = 0; try { if (reader.markSupported()) { reader.mark(SUMMARY_SIZE); numChars = reader.read(sumChars); reader.reset(); } if (numChars > 0) { summary = new String(sumChars, 0, numChars); } if (log.isDebugEnabled()) { log.debug("Summary extracted from reader: " + summary); } setSummary(getSummaryFromContent(summary)); } catch (IOException e) { log.warn("Error extracting summary form reader", e); } } } // Set the title if there's none yet if (!StringUtils.hasLength(fileInfo.getTitle())) { fileInfo.setTitle(FileUtils.getBasename(f)); } } catch (Exception e) { // here we don't throw any, since we do not want to interrupt the indexing process log.warn("Unexpected Error extracting content from " + f.getName(), e); } catch (OutOfMemoryError e) { // this happens with very, very large Documents log.error("Very Serious Error. Out of Memory for very large documents: " + f.getName() + ", try increasing your JVM heap size: for example, start your server with option '-Xmx128m'." + " Skipping file.", e); } catch (Throwable e) { log.error("Very Serious Error while extracting contents from: " + f.getName(), e); } return fileInfo; }
From source file:com.streamsets.pipeline.lib.json.StreamingJsonParser.java
public StreamingJsonParser(Reader reader, long initialPosition, Mode mode) throws IOException { starting = true;/* w w w .j a v a 2 s. co m*/ this.reader = reader; if (mode == Mode.MULTIPLE_OBJECTS) { if (initialPosition > 0) { IOUtils.skipFully(reader, initialPosition); posCorrection += initialPosition; } if (reader.markSupported()) { reader.mark(MAX_CHARS_TO_READ_FORWARD); int count = 0; byte firstByte = -1; while (count++ < MAX_CHARS_TO_READ_FORWARD && (firstByte = (byte) reader.read()) != -1 && firstByte <= ' ') ; // everything less than a space is whitespace if (firstByte > ' ') { firstNonSpaceChar = firstByte; } reader.reset(); } } jsonParser = getObjectMapper().getFactory().createParser(reader); if (mode == Mode.ARRAY_OBJECTS && initialPosition > 0) { fastForwardJsonParser(initialPosition); } this.mode = mode; }
From source file:net.metanotion.json.StreamingParser.java
private String lexExp(final Reader in) throws IOException { in.mark(MAX_BUFFER);//from w w w .j av a 2s.c om int c = in.read(); if (Character.toLowerCase(c) == 'e') { c = in.read(); if (c == '+') { return "e+" + lexDigits(in); } else if (c == '-') { return "e-" + lexDigits(in); } else if (Character.isDigit(c)) { return (new String(Character.toChars(c))) + lexDigits(in); } else if (c == -1) { throw new ParserException("Unexpected end of stream"); } else { throw new ParserException( "Expected exponent, instead found: '" + (new String(Character.toChars(c))) + QUOTE); } } else { in.reset(); return ""; } }
From source file:net.metanotion.json.StreamingParser.java
private String lexInt(final Reader in, final int firstChar) throws IOException { final StringBuilder sb = new StringBuilder(); int digits = 0; if (firstChar == '-') { sb.append("-"); } else if (Character.isDigit(firstChar)) { sb.append(Character.toChars(firstChar)); digits++;//ww w .jav a2 s.c o m } else { final String found = new String(Character.toChars(firstChar)); throw new ParserException("Expecting a number, instead found: '" + found + QUOTE); } while (true) { in.mark(MAX_BUFFER); final int c = in.read(); if (Character.isDigit(c)) { digits++; sb.append(Character.toChars(c)); } else { in.reset(); if (digits == 0) { throw new ParserException(EXPECTED_DIGIT); } return sb.toString(); } } }
From source file:com.streamsets.datacollector.json.JsonObjectReaderImpl.java
public JsonObjectReaderImpl(Reader reader, long initialPosition, Mode mode, Class<?> objectClass, ObjectMapper objectMapper) throws IOException { this.mode = mode; this.objectClass = objectClass; this.objectMapper = objectMapper; starting = true;/*from w w w .ja v a 2s . c om*/ this.reader = reader; if (mode == Mode.MULTIPLE_OBJECTS) { if (initialPosition > 0) { IOUtils.skipFully(reader, initialPosition); posCorrection += initialPosition; } if (reader.markSupported()) { reader.mark(MAX_CHARS_TO_READ_FORWARD); int count = 0; byte firstByte = -1; while (count++ < MAX_CHARS_TO_READ_FORWARD && (firstByte = (byte) reader.read()) != -1 && firstByte <= ' ') ; // everything less than a space is whitespace if (firstByte > ' ') { firstNonSpaceChar = firstByte; } reader.reset(); } } jsonParser = getObjectMapper().getFactory().createParser(reader); if (mode == Mode.ARRAY_OBJECTS && initialPosition > 0) { fastForwardJsonParser(initialPosition); } }
From source file:de.micromata.genome.gwiki.page.gspt.ExtendedTemplate.java
/** * Parses the to elements./* ww w . j a v a 2 s . c om*/ * * @param reader the reader * @return the list * @throws IOException Signals that an I/O exception has occurred. */ protected List<ParseElement> parseToElements(Reader reader) throws IOException { if (!reader.markSupported()) { reader = new BufferedReader(reader); } // StringWriter sw = new StringWriter(); List<ParseElement> elements = new ArrayList<ParseElement>(); startScript(elements); // boolean start = false; int c; while ((c = reader.read()) != -1) { if (c == '<') { reader.mark(1); c = reader.read(); if (c != '%') { // sw.write('<'); elements.add(new ParseElement(Type.ConstString, "<")); reader.reset(); } else { reader.mark(1); c = reader.read(); if (c == '=') { groovyExpression(reader, elements); } else if (c == '-') { reader.read(); groovyComment(reader, elements); } else if (c == '#') { groovySection(Type.GlobalCode, reader, elements); } else if (c == '!') { groovySection(Type.ClassCode, reader, elements); } else { reader.reset(); groovySection(Type.Statement, reader, elements); } } continue; // at least '<' is consumed ... read next chars. } else if (c == '-') { reader.mark(4); if (reader.read() == '-' && reader.read() == '%' && reader.read() == '>') { /** * @logging * @reason Innerhalb einer GSPT-Datei ist ein Kommentarendesequenz ohne oeffnendes * @action GSPT korrigieren */ GWikiLog.warn("In gspt --%> comment without open"); } reader.reset(); } if (elements.size() == 0 || elements.get(elements.size() - 1).type != Type.ConstString) elements.add(new ParseElement(Type.ConstString, "")); elements.get(elements.size() - 1).text.append((char) c); } return elements; }
From source file:com.webcohesion.ofx4j.io.BaseOFXReader.java
/** * Parse the reader, including the headers. * * @param reader The reader./*from w ww . ja v a 2s .c om*/ */ public void parse(Reader reader) throws IOException, OFXParseException { //make sure we're buffering... reader = new BufferedReader(reader); StringBuilder header = new StringBuilder(); final char[] firstElementStart = getFirstElementStart(); final char[] buffer = new char[firstElementStart.length]; reader.mark(firstElementStart.length); int ch = reader.read(buffer); while ((ch != -1) && (!Arrays.equals(buffer, firstElementStart))) { if (!contains(buffer, '<')) { //if the buffer contains a '<', then we might already have marked the beginning. reader.mark(firstElementStart.length); } ch = reader.read(); char shifted = shiftAndAppend(buffer, (char) ch); header.append(shifted); } if (ch == -1) { throw new OFXParseException("Invalid OFX: no root <OFX> element!"); } else { Matcher matcher = OFX_2_PROCESSING_INSTRUCTION_PATTERN.matcher(header); if (matcher.find()) { if (LOG.isInfoEnabled()) { LOG.info("Processing OFX 2 header..."); } processOFXv2Headers(matcher.group(1)); reader.reset(); parseV2FromFirstElement(reader); } else { LOG.info("Processing OFX 1 headers..."); processOFXv1Headers(header.toString()); reader.reset(); parseV1FromFirstElement(reader); } } }
From source file:BayesianAnalyzer.java
private String nextToken(Reader reader) throws java.io.IOException { StringBuffer token = new StringBuffer(); int i;//from www . j av a2 s . co m char ch, ch2; boolean previousWasDigit = false; boolean tokenCharFound = false; if (!reader.ready()) { return null; } while ((i = reader.read()) != -1) { ch = (char) i; if (ch == ':') { String tokenString = token.toString() + ':'; if (tokenString.equals("From:") || tokenString.equals("Return-Path:") || tokenString.equals("Subject:") || tokenString.equals("To:")) { return tokenString; } } if (Character.isLetter(ch) || ch == '-' || ch == '$' || ch == '\u20AC' // the EURO symbol || ch == '!' || ch == '\'') { tokenCharFound = true; previousWasDigit = false; token.append(ch); } else if (Character.isDigit(ch)) { tokenCharFound = true; previousWasDigit = true; token.append(ch); } else if (previousWasDigit && (ch == '.' || ch == ',')) { reader.mark(1); previousWasDigit = false; i = reader.read(); if (i == -1) { break; } ch2 = (char) i; if (Character.isDigit(ch2)) { tokenCharFound = true; previousWasDigit = true; token.append(ch); token.append(ch2); } else { reader.reset(); break; } } else if (ch == '\r') { // cr found, ignore } else if (ch == '\n') { // eol found tokenCharFound = true; previousWasDigit = false; token.append(ch); break; } else if (tokenCharFound) { break; } } if (tokenCharFound) { // System.out.println("Token read: " + token); return token.toString(); } else { return null; } }