Example usage for java.io InputStream mark

List of usage examples for java.io InputStream mark

Introduction

In this page you can find the example usage for java.io InputStream mark.

Prototype

public synchronized void mark(int readlimit) 

Source Link

Document

Marks the current position in this input stream.

Usage

From source file:edu.utsa.sifter.FileInfo.java

Document makeDoc(final AbstractParser tika, final Analyzer analyzer, final long id, final InputStream data,
        final Map<String, Object> metadata, final String ext, final boolean noTikaAndTest) throws IOException {
    Document doc = new Document();
    DocMaker.addField(doc, "ID", Long.toString(id)); // makes querying easier if this is a string, counter-intuitively
    if (ext != null && !ext.isEmpty()) {
        DocMaker.addField(doc, "extension", ext);
    }// ww  w.  ja v a2 s .  c o m
    DocMaker.addMetadata(doc, Metadata, "");
    final String fp = fullPath();
    data.mark((int) FileSize);
    try {
        if (noTikaAndTest) {
            return rawDoc(analyzer, doc, data, fp, noTikaAndTest);
        } else {
            DocMaker.addBody(doc, basename(), data, tika, analyzer, false);
        }
    } catch (IOException ex) {
        data.reset();
        //      System.err.println("Could not extract body from " + fullPath() + ". " + ex.toString());
        return rawDoc(analyzer, doc, data, fp, noTikaAndTest);
    } catch (SAXException ex) {
        data.reset();
        // System.err.println("Had SAXException on body of " + fp + ". " + ex.toString());
        return rawDoc(analyzer, doc, data, fp, noTikaAndTest);
    } catch (TikaException ex) {
        data.reset();
        // System.err.println("Extracting text raw. Had TikaException on body of " + fp + ". " + ex.toString());
        return rawDoc(analyzer, doc, data, fp, noTikaAndTest);
    }
    return doc;
}

From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializerTest.java

@Test
public void testUnmarshalEntireState() throws Exception {
    // First, create a state file to unmarshal
    // 1. It must be a zip archive; unmarshalling a non-archive package state file isn't supported
    // 2. We use the liveMarshallerMap; no mocks.
    // 3. We use a live ArchiveStreamFactory
    underTest.setArchive(true);/*w w  w .j  av  a  2  s  . c  om*/
    underTest.setMarshallerMap(liveMarshallerMap);
    underTest.setArxStreamFactory(new ZipArchiveStreamFactory());
    File tmp = File.createTempFile(this.getClass().getName() + "_UnmarshalEntireState", ".zip");
    OutputStream out = new FileOutputStream(tmp);
    underTest.serialize(state, out);
    assertTrue(tmp.exists() && tmp.length() > 1);

    // Verify that we wrote out a zip file.
    final byte[] signature = new byte[12];
    InputStream in = new BufferedInputStream(new FileInputStream(tmp));
    in.mark(signature.length);
    int bytesRead = org.apache.commons.compress.utils.IOUtils.readFully(in, signature);
    assertTrue(ZipArchiveInputStream.matches(signature, bytesRead));
    in.reset();

    // Create a new instance of PackageState, and deserialize the zip archive created above, which contains the
    // test objects from the {@link #state prepared instance} of PackageState
    PackageState state = new PackageState(); // a new instance of PackageState with no internal state
    underTest.deserialize(state, in);

    Map<StreamId, PropertyDescriptor> pds = SerializationAnnotationUtil
            .getStreamDescriptors(PackageState.class);
    assertTrue(pds.size() > 0);
    pds.keySet().stream().forEach(streamId -> {
        try {
            assertNotNull("Expected non-null value for PackageState field '" + pds.get(streamId).getName()
                    + "', " + "StreamId '" + streamId + "'", pds.get(streamId).getReadMethod().invoke(state));
        } catch (IllegalAccessException | InvocationTargetException e) {
            fail(e.getMessage());
        }
    });

    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tmp);
}

From source file:com.smartitengineering.event.hub.core.ChannelEventsResource.java

public Response get(String placeholderId, boolean isBefore) {
    if (count == null) {
        count = 10;//from w w  w .j av a 2s  . c  om
    }
    ResponseBuilder responseBuilder = Response.ok();
    Feed atomFeed = getFeed("Events", new Date());

    int thisCount = count;
    if (isBefore) {
        thisCount = count * -1;
    }

    Collection<Event> events = HubPersistentStorerSPI.getInstance().getStorer().getEvents(placeholderId,
            channelId, thisCount);

    if (events != null && !events.isEmpty()) {
        MultivaluedMap<String, String> queryParams = getUriInfo().getQueryParameters();

        List<Event> eventList = new ArrayList<Event>(events);
        Link nextLink = getAbderaFactory().newLink();
        nextLink.setRel(Link.REL_PREVIOUS);
        Event lastEvent = eventList.get(0);
        final UriBuilder nextUri = getRelativeURIBuilder().path(ChannelEventsResource.class).path(AFTER_METHOD);
        final UriBuilder previousUri = getRelativeURIBuilder().path(ChannelEventsResource.class)
                .path(BEFORE_METHOD);

        for (String key : queryParams.keySet()) {
            final Object[] values = queryParams.get(key).toArray();
            nextUri.queryParam(key, values);
            previousUri.queryParam(key, values);
        }

        nextLink.setHref(nextUri.build(channelId, lastEvent.getPlaceholderId()).toString());
        atomFeed.addLink(nextLink);

        Link previousLink = getAbderaFactory().newLink();
        previousLink.setRel(Link.REL_NEXT);
        Event firstEvent = eventList.get(events.size() - 1);
        previousLink.setHref(previousUri.build(channelId, firstEvent.getPlaceholderId()).toString());
        atomFeed.addLink(previousLink);

        for (Event event : events) {
            Entry eventEntry = getAbderaFactory().newEntry();

            eventEntry.setId(event.getPlaceholderId());
            eventEntry.setTitle(event.getPlaceholderId().toString());

            InputStream contentStream = event.getEventContent().getContent();
            String contentAsString = "";

            if (contentStream != null) {
                if (contentCache.containsKey(event)) {
                    contentAsString = contentCache.get(event);
                } else {
                    try {
                        if (contentStream.markSupported()) {
                            contentStream.mark(Integer.MAX_VALUE);
                        }
                        contentAsString = IOUtils.toString(contentStream);
                        contentCache.put(event, contentAsString);
                        if (contentStream.markSupported()) {
                            contentStream.reset();
                        }
                    } catch (IOException ex) {
                    }
                }
            }

            eventEntry.setContent(contentAsString);
            eventEntry.setUpdated(event.getCreationDate());

            Link eventLink = getAbderaFactory().newLink();

            eventLink.setHref(getRelativeURIBuilder().path(EventResource.class).build(event.getPlaceholderId())
                    .toString());
            eventLink.setRel(Link.REL_ALTERNATE);
            eventLink.setMimeType(MediaType.APPLICATION_JSON);

            eventEntry.addLink(eventLink);
            atomFeed.addEntry(eventEntry);
        }
    }
    responseBuilder.entity(atomFeed);
    return responseBuilder.build();
}

From source file:com.smartitengineering.event.hub.core.AllEventsResource.java

public Response get(String placeholderId, boolean isBefore) {
    if (count == null) {
        count = 10;//from   w  w  w.  java  2 s .  c om
    }
    int thisCount = count;
    if (isBefore) {
        thisCount = count * -1;
    }
    ResponseBuilder responseBuilder = Response.ok();
    Feed atomFeed = getFeed("Events", new Date());

    Link eventsLink = getAbderaFactory().newLink();
    eventsLink.setHref(getRelativeURIBuilder().path(RootResource.class).build().toString());
    eventsLink.setRel("root");

    atomFeed.addLink(eventsLink);

    Collection<Event> events = HubPersistentStorerSPI.getInstance().getStorer().getEvents(placeholderId, null,
            thisCount);

    if (events != null && !events.isEmpty()) {
        MultivaluedMap<String, String> queryParams = getUriInfo().getQueryParameters();

        List<Event> eventList = new ArrayList<Event>(events);
        Link nextLink = getAbderaFactory().newLink();
        nextLink.setRel(Link.REL_PREVIOUS);
        Event lastEvent = eventList.get(0);
        final UriBuilder nextUri = getRelativeURIBuilder().path(AllEventsResource.class).path(AFTER_METHOD);
        final UriBuilder previousUri = getRelativeURIBuilder().path(AllEventsResource.class)
                .path(BEFORE_METHOD);

        for (String key : queryParams.keySet()) {
            final Object[] values = queryParams.get(key).toArray();
            nextUri.queryParam(key, values);
            previousUri.queryParam(key, values);
        }

        nextLink.setHref(nextUri.build(lastEvent.getPlaceholderId()).toString());
        atomFeed.addLink(nextLink);

        Link previousLink = getAbderaFactory().newLink();
        previousLink.setRel(Link.REL_NEXT);
        Event firstEvent = eventList.get(events.size() - 1);
        previousLink.setHref(previousUri.build(firstEvent.getPlaceholderId()).toString());
        atomFeed.addLink(previousLink);

        for (Event event : events) {
            Entry eventEntry = getAbderaFactory().newEntry();

            eventEntry.setId(event.getPlaceholderId());
            eventEntry.setTitle(event.getPlaceholderId().toString());

            InputStream contentStream = event.getEventContent().getContent();
            String contentAsString = "";

            if (contentStream != null) {
                if (contentCache.containsKey(event)) {
                    contentAsString = contentCache.get(event);
                } else {
                    try {
                        if (contentStream.markSupported()) {
                            contentStream.mark(Integer.MAX_VALUE);
                        }
                        contentAsString = IOUtils.toString(contentStream);
                        contentCache.put(event, contentAsString);
                        if (contentStream.markSupported()) {
                            contentStream.reset();
                        }
                    } catch (IOException ex) {
                    }
                }
            }

            eventEntry.setContent(contentAsString);
            eventEntry.setUpdated(event.getCreationDate());

            Link eventLink = getAbderaFactory().newLink();

            eventLink.setHref(getRelativeURIBuilder().path(EventResource.class).build(event.getPlaceholderId())
                    .toString());
            eventLink.setRel(Link.REL_ALTERNATE);
            eventLink.setMimeType(MediaType.APPLICATION_JSON);

            eventEntry.addLink(eventLink);
            atomFeed.addEntry(eventEntry);
        }
    }
    responseBuilder.entity(atomFeed);
    return responseBuilder.build();
}

From source file:org.globus.gsi.X509Credential.java

public X509Credential(InputStream certInputStream, InputStream keyInputStream) throws CredentialException {
    if (certInputStream.markSupported()) {
        certInputStream.mark(BUFFER_SIZE);
    }//w  w w. jav  a2s. c  o m
    loadKey(keyInputStream);
    loadCertificate(certInputStream);
    validateCredential();
}

From source file:org.lockss.plugin.atypon.BaseAtyponRisFilterFactory.java

public InputStream createFilteredInputStream(ArchivalUnit au, InputStream in, String encoding)
        throws PluginException {

    InputStream inBuf = null; // to make sure mark() is supported
    /* /* ww  w. j  a  v a 2 s . c  om*/
     * RIS files are collected with content type text/plain (encoding) and so
     * we have to filter all text/plain and then determine if they're a RIS file 
     * here.  We are working on a different long-term solution by allowing us to verify
     * the URL against a regexp.
     */

    BufferedReader bReader;

    if (in.markSupported() != true) {
        inBuf = new BufferedInputStream(in); //wrap the one that came in
    } else {
        inBuf = in; //use the one passed in
    }
    int BUF_LEN = 2000;
    inBuf.mark(BUF_LEN); // not sure about the limit...just reading far enough to identify file type

    try {
        //Now create a BoundedInputReader to make sure that we don't overstep our reset mark
        bReader = new BufferedReader(new InputStreamReader(new BoundedInputStream(inBuf, BUF_LEN), encoding));

        String aLine = bReader.readLine();
        // The first tag in a RIS file must be "TY - "; be nice about WS
        // The specification doesn't allow for comments or other preceding characters

        // isBlank() checks if whitespace, empty or null
        // keep initial null check or you'd never exit loop if you hit the end of input!
        while (aLine != null && StringUtils.isBlank(aLine)) {
            aLine = bReader.readLine(); // get the next line
        }

        // do NOT close bReader - it would also close underlying inBuf!
        inBuf.reset();
        // if we have  data, see if it matches the RIS pattern
        if (aLine != null && RIS_PATTERN.matcher(aLine).find()) {
            return new RisFilterInputStream(inBuf, encoding, "Y2");
        }
        return inBuf; // If not a RIS file, just return reset file
    } catch (UnsupportedEncodingException e) {
        log.debug2("Internal error (unsupported encoding)", e);
        throw new PluginException("Unsupported encoding looking ahead in input stream", e);
    } catch (IOException e) {
        log.debug2("Internal error (IO exception)", e);
        throw new PluginException("IO exception looking ahead in input stream", e);
    }
}

From source file:org.apache.tika.parser.pkg.PackageExtractor.java

public void parse(InputStream stream) throws IOException, SAXException, TikaException {
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();//from w  ww  .  j  a v  a2  s . com

    // At the end we want to close the package/compression stream to
    // release any associated resources, but the underlying document
    // stream should not be closed
    stream = new CloseShieldInputStream(stream);

    // Capture two bytes to determine the packaging/compression format
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    stream.mark(2);
    int a = stream.read();
    int b = stream.read();
    stream.reset();

    // Select decompression or unpacking mechanism based on the two bytes
    if (a == 'B' && b == 'Z') {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-bzip");
        decompress(new BZip2CompressorInputStream(stream), xhtml);
    } else if (a == 0x1f && b == 0x8b) {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-gzip");
        decompress(new GZIPInputStream(stream), xhtml);
    } else if (a == 'P' && b == 'K') {
        metadata.set(Metadata.CONTENT_TYPE, "application/zip");
        unpack(new ZipArchiveInputStream(stream), xhtml);
    } else if ((a == '0' && b == '7') || (a == 0x71 && b == 0xc7) || (a == 0xc7 && b == 0x71)) {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-cpio");
        unpack(new CpioArchiveInputStream(stream), xhtml);
    } else if (a == '=' && (b == '<' || b == '!')) {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-archive");
        unpack(new ArArchiveInputStream(stream), xhtml);
    } else {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-tar");
        unpack(new TarArchiveInputStream(stream), xhtml);
    }

    xhtml.endDocument();
}

From source file:org.archive.io.arc.ARCWriterTest.java

public void testArchiveRecordMarkSupport() throws Exception {
    ARCReader r = getSingleRecordReader("testArchiveRecordMarkSupport");
    ARCRecord record = getSingleRecord(r);
    record.setStrict(true);/*from  ww  w  .j  a  v  a2 s . c  o  m*/
    // ensure mark support
    InputStream stream = new BufferedInputStream(record);
    if (stream.markSupported()) {
        for (int i = 0; i < 3; i++) {
            this.readToEOS(stream);
            stream.mark(stream.available());
            stream.reset();
        }
        stream.close();
    }
    r.close();
}

From source file:com.gargoylesoftware.htmlunit.WebResponseData.java

private InputStream getStream(final DownloadedContent downloadedContent, final List<NameValuePair> headers)
        throws IOException {

    InputStream stream = downloadedContent_.getInputStream();
    if (stream == null) {
        return null;
    }//from   w  w w .  ja  v a  2s. c  o  m

    if (downloadedContent.isEmpty()) {
        return stream;
    }

    final String encoding = getHeader(headers, "content-encoding");
    if (encoding != null) {
        if (StringUtils.contains(encoding, "gzip")) {
            stream = new GZIPInputStream(stream);
        } else if (StringUtils.contains(encoding, "deflate")) {
            boolean zlibHeader = false;
            if (stream.markSupported()) { // should be always the case as the content is in a byte[] or in a file
                stream.mark(2);
                final byte[] buffer = new byte[2];
                stream.read(buffer, 0, 2);
                zlibHeader = (((buffer[0] & 0xff) << 8) | (buffer[1] & 0xff)) == 0x789c;
                stream.reset();
            }
            if (zlibHeader) {
                stream = new InflaterInputStream(stream);
            } else {
                stream = new InflaterInputStream(stream, new Inflater(true));
            }
        }
    }
    return stream;
}

From source file:org.liveSense.misc.configloader.ConfigurationLoader.java

/**
 * Set the configuration based on the config file.
 *
 * @param f//from   w  w  w.j  a  v a 2  s  .  c  o m
 *            Configuration file
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
boolean setConfig(URL f) throws Exception {
    Properties p = new Properties();

    @SuppressWarnings("rawtypes")
    Dictionary ht = new Hashtable();

    InputStream in = new BufferedInputStream(f.openStream());
    try {
        // If the file name ends with .config, we using the Felix configuration format
        if (f.getFile().endsWith(".config")) {
            ht = ConfigurationHandler.read(in);
        } else {
            in.mark(1);
            boolean isXml = in.read() == '<';
            in.reset();
            if (isXml) {
                p.loadFromXML(in);
            } else {
                p.load(in);
            }
            ((Hashtable) ht).putAll(p);
        }
    } finally {
        in.close();
    }

    // Searching for templated config entry.
    // If we found one we get Java System properties
    // named as the macros. The config became activated if that
    // system proprty is set.

    Pattern macros = Pattern.compile("\\$\\{(.*?)\\}");
    boolean valid = true;

    Enumeration enumr = ht.keys();
    while (enumr.hasMoreElements()) {
        Object key = enumr.nextElement();
        if (ht.get(key) instanceof String) {
            String str = (String) ht.get(key);
            if (str != null) {
                Matcher matcher = macros.matcher(str);

                HashSet<String> propNames = new HashSet<String>();
                while (matcher.find()) {
                    propNames.add(matcher.group(1));
                }

                for (String prop : propNames) {
                    String sysProp = System.getProperty(prop);
                    if (sysProp == null) {
                        valid = false;
                    }
                    if (valid) {
                        str = StringUtils.replace(str, "${" + prop + "}", sysProp);
                        //str = str.replaceAll("\\$\\{"+prop+"\\}", sysProp);
                    }
                }
                if (valid) {
                    ht.put(key, str);
                }
            }
        }
    }

    if (valid) {
        Util.performSubstitution(p);
        String pid[] = parsePid(getName(f.getFile()));
        ht.put(CONFIGURATION_PROPERTY_NAME, getPidName(pid[0], pid[1]));

        Configuration config = getConfiguration(pid[0], pid[1]);

        /*
        // Backuping parameters for restore
        String persistanceName = pid[0]+(pid[1] == null ? "" : "-" + pid[1]);
        if (config.getProperties() != null && config.getProperties().get(CONFIGURATION_PROPERTY_NAME) == null) {
           if (persistence.load(persistanceName).isEmpty()) {
              persistence.store(persistanceName, config.getProperties());
           }
        }
         */
        if (config.getBundleLocation() != null) {
            config.setBundleLocation(null);
        }

        // If the configuration does not created by configuration loader we update it
        // In other cases (for example the user modified the loaded config) there is no configuration overwrite
        if (config.getProperties() == null || config.getProperties().get(CONFIGURATION_PROPERTY_NAME) == null
                || !config.getProperties().get(CONFIGURATION_PROPERTY_NAME).equals(getName(f.getFile()))) {
            config.update(ht);
        }
    }
    return true;
}