Example usage for java.io Reader markSupported

List of usage examples for java.io Reader markSupported

Introduction

In this page you can find the example usage for java.io Reader markSupported.

Prototype

public boolean markSupported() 

Source Link

Document

Tells whether this stream supports the mark() operation.

Usage

From source file:com.streamsets.pipeline.lib.json.StreamingJsonParser.java

public StreamingJsonParser(Reader reader, long initialPosition, Mode mode) throws IOException {
    starting = true;//  ww  w. java2  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:org.zilverline.extractors.AbstractExtractor.java

/**
 * This method extracts all relevant info of the file as an ParsedFileInfo object. Uses getContent as callback.
 * /*from  ww w .j  av  a2  s.co  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.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 .jav a 2 s .c o  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);
    }
}

From source file:de.micromata.genome.gwiki.page.gspt.ExtendedTemplate.java

/**
 * Parses the to elements./*from   ww w .j  a  va  2s  . co  m*/
 *
 * @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:org.fhcrc.cpl.toolbox.filehandler.TabLoader.java

protected void setSource(Reader reader) {
    if (reader.markSupported())
        _reader = reader;//from   www.  ja  v a2  s.com
    else
        _reader = new BufferedReader(reader);

    try {
        // shouldn't throw as we checked markSupported
        _reader.mark(1024 * 1024);
    } catch (IOException x) {
        throw new RuntimeException(x);
    }
}

From source file:org.exist.collections.MutableCollection.java

@Override
public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final InputSource source)
        throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {
    storeXMLInternal(transaction, broker, info, storeInfo -> {
        try {//  ww  w.  ja v a 2 s  . co m
            final InputStream is = source.getByteStream();
            if (is != null && is.markSupported()) {
                is.reset();
            } else {
                final Reader cs = source.getCharacterStream();
                if (cs != null && cs.markSupported()) {
                    cs.reset();
                }
            }
        } catch (final IOException e) {
            // mark is not supported: exception is expected, do nothing
            LOG.debug(
                    "InputStream or CharacterStream underlying the InputSource does not support marking and therefore cannot be re-read.");
        }
        final XMLReader reader = getReader(broker, false, storeInfo.getCollectionConfig());
        storeInfo.setReader(reader, null);
        try {
            reader.parse(source);
        } catch (final IOException e) {
            throw new EXistException(e);
        } finally {
            releaseReader(broker, storeInfo, reader);
        }
    });
}

From source file:org.exist.collections.Collection.java

/** Stores an XML document in the database. {@link #validateXMLResourceInternal(org.exist.storage.txn.Txn,
 * org.exist.storage.DBBroker, org.exist.xmldb.XmldbURI, CollectionConfiguration, org.exist.collections.Collection.ValidateBlock)} 
 * should have been called previously in order to acquire a write lock for the document. Launches the finish trigger.
 * /*from w  w w. j a  va2  s. co  m*/
 * @param transaction
 * @param broker
 * @param info
 * @param source
 * @param privileged
 * 
 * @throws EXistException
 * @throws PermissionDeniedException
 * @throws TriggerException
 * @throws SAXException
 * @throws LockException
 */
public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final InputSource source,
        boolean privileged)
        throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {

    storeXMLInternal(transaction, broker, info, privileged, new StoreBlock() {
        @Override
        public void run() throws EXistException, SAXException {
            try {
                final InputStream is = source.getByteStream();
                if (is != null && is.markSupported()) {
                    is.reset();
                } else {
                    final Reader cs = source.getCharacterStream();
                    if (cs != null && cs.markSupported()) {
                        cs.reset();
                    }
                }
            } catch (final IOException e) {
                // mark is not supported: exception is expected, do nothing
                LOG.debug(
                        "InputStream or CharacterStream underlying the InputSource does not support marking and therefore cannot be re-read.");
            }
            final XMLReader reader = getReader(broker, false, info.getCollectionConfig());
            info.setReader(reader, null);
            try {
                reader.parse(source);
            } catch (final IOException e) {
                throw new EXistException(e);
            } finally {
                releaseReader(broker, info, reader);
            }
        }
    });
}

From source file:org.regenstrief.util.Util.java

/**
 * Retrieves a Reader that can be marked and marks it
 * //from   w ww.  j a v  a2 s .  c  o m
 * @param in the Reader
 * @return the markable Reader
 **/
public static final Reader getMarkableReader(Reader in) {
    in = in.markSupported() ? in : new BufferedReader(in);
    try {
        in.mark(MAX_MARK_BUFFER);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    return in;
}