Example usage for java.io InputStream getClass

List of usage examples for java.io InputStream getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.commonjava.maven.galley.model.Transfer.java

public InputStream openInputStream(final boolean fireEvents, final EventMetadata eventMetadata)
        throws IOException {
    provider.waitForReadUnlock(resource);
    try {//from  w  w  w.  ja va  2 s . c om
        InputStream stream = provider.openInputStream(resource);
        if (stream == null) {
            return null;
        }

        if (fireEvents) {
            stream = new TransferInputStream(stream, new FileAccessEvent(this, eventMetadata),
                    fileEventManager);
        }

        stream = decorator == null ? stream : decorator.decorateRead(stream, this, eventMetadata);

        logger.trace("Returning stream: {} for transfer: {}", stream.getClass().getName(), this);
        return stream;
    } catch (final IOException e) {
        if (fireEvents) {
            fileEventManager.fire(new FileErrorEvent(this, e, eventMetadata));
        }
        throw e;
    }
}

From source file:org.dataconservancy.dcs.util.extraction.TarPackageExtractor.java

@Override
public List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName)
        throws UnpackException {

    List<File> files = new ArrayList<>();
    //use try-with-resources to make sure tar input stream is closed even in the event of an Exception
    try (TarArchiveInputStream tarInStream = TarArchiveInputStream.class
            .isAssignableFrom(packageInputStream.getClass()) ? (TarArchiveInputStream) packageInputStream
                    : new TarArchiveInputStream(packageInputStream)) {
        TarArchiveEntry entry = tarInStream.getNextTarEntry();
        //Get next tar entry returns null when there are no more entries
        while (entry != null) {
            //Directories are automatically handled by the base class so we can ignore them in this class.
            if (!entry.isDirectory()) {
                File entryFile = new File(packageDir,
                        FilePathUtil.convertToPlatformSpecificSlash(entry.getName()));
                List<File> savedFiles = saveExtractedFile(entryFile, tarInStream);
                files.addAll(savedFiles);
            }//from   w  w w.j av a2s.  c o  m
            entry = tarInStream.getNextTarEntry();
        }

        tarInStream.close();
    } catch (IOException e) {
        final String msg = "Error processing TarArchiveInputStream: " + e.getMessage();
        log.error(msg, e);
        throw new UnpackException(msg, e);
    }

    return files;
}

From source file:org.dataconservancy.ui.util.TarPackageExtractor.java

@Override
protected List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName)
        throws UnpackException {
    final TarArchiveInputStream tarInStream;

    if (!TarArchiveInputStream.class.isAssignableFrom(packageInputStream.getClass())) {
        tarInStream = new TarArchiveInputStream(packageInputStream);
    } else {//from w  w w  .ja va 2 s  . c o  m
        tarInStream = (TarArchiveInputStream) packageInputStream;
    }

    List<File> files = new ArrayList<File>();
    try {
        TarArchiveEntry entry = tarInStream.getNextTarEntry();
        //Get next tar entry returns null when there are no more entries
        while (entry != null) {
            //Directories are automatically handled by the base class so we can ignore them in this class.
            if (!entry.isDirectory()) {
                File entryFile = new File(packageDir, entry.getName());
                if (entryFile != null) {
                    List<File> savedFiles = saveExtractedFile(entryFile, tarInStream);
                    files.addAll(savedFiles);
                }
            }
            entry = tarInStream.getNextTarEntry();
        }

        tarInStream.close();
    } catch (IOException e) {
        final String msg = "Error processing TarArchiveInputStream: " + e.getMessage();
        log.error(msg, e);
        throw new UnpackException(msg, e);
    }

    return files;
}

From source file:org.dataconservancy.ui.util.ZipPackageExtractor.java

@Override
protected List<File> unpackFilesFromStream(InputStream packageInputStream, String packageDir, String fileName)
        throws UnpackException {
    final ZipInputStream zipInStream;

    if (!ZipInputStream.class.isAssignableFrom(packageInputStream.getClass())) {
        zipInStream = new ZipInputStream(packageInputStream);
    } else {/*w  w  w . j  a v  a 2  s.c  om*/
        zipInStream = (ZipInputStream) packageInputStream;
    }

    List<File> files = new ArrayList<File>();
    try {
        ZipEntry entry = zipInStream.getNextEntry();
        //Get next tar entry returns null when there are no more entries
        while (entry != null) {
            //Directories are automatically handled by the base class so we can ignore them in this class.
            if (!entry.isDirectory()) {
                File entryFile = new File(packageDir, entry.getName());
                if (entryFile != null) {
                    List<File> savedFiles = saveExtractedFile(entryFile, zipInStream);
                    files.addAll(savedFiles);
                }
            }
            entry = zipInStream.getNextEntry();
        }

        zipInStream.close();
    } catch (IOException e) {
        final String msg = "Error processing ZipInputStream: " + e.getMessage();
        log.error(msg, e);
        throw new UnpackException(msg, e);
    }

    return files;
}

From source file:org.dbpedia.spotlight.spot.OpenNLPUtil.java

/**Loads OpenNLP 5 models.
 * @param directoryPath Path of the FS directory. Used when creating/opening an InputStream to a file
 *        model file in the folder (direct file reading)
 * @param modelRelativePath This is the to the model file starting from a resource folder (i.e. when reading
 *   from a jar, this is the path of the model file in the jar file followed by the model file name.
 *   e.g. in case if model files are in a folder named "opennlp" in the jar file, then we can set "opennlp"
 *   to directorypath and "english/en-sent.zip" to model relativepath (note the modelfile en-sent.zip) is
 *   assumed to to be in opennlp/english/en-sent.zip.
 * @param modelType/*  w  ww . j a v  a  2 s .  c om*/
 * @return
 * @throws IOException
 */
protected static BaseModel loadModel(String directoryPath, String modelRelativePath, String modelType)
        throws ConfigurationException {
    ClassLoader loader = OpenNLPUtil.class.getClassLoader();
    InputStream in = null;
    try {
        if (directoryPath != null && directoryPath.length() > 0) {
            // load custom models from the provided FS directory
            File modelData = new File(new File(directoryPath), modelRelativePath);
            in = new FileInputStream(modelData);
            LOG.debug("**OpenNLP is Loading OpenNLP 1.5 " + modelType + " from a given directory path: "
                    + modelData.getAbsolutePath());
        } else {
            // load default OpenNLP models from jars
            String resourcePath = "opennlp/" + modelRelativePath;
            in = loader.getResourceAsStream(resourcePath);
            LOG.debug("**OpenNLP is Loading OpenNLP 1.5 " + modelType + " model by Regular class loading: "
                    + in.getClass().getCanonicalName());
            if (in == null) {
                throw new IOException("could not find resource: " + resourcePath);
            }
        }
        return loadOpenNlpModel(modelType, in);
    } catch (IOException e) {
        throw new ConfigurationException("Could not load OpenNLP Model file.");
    }
}

From source file:org.freezedry.serialization.ObjectSerializer.java

@Override
public synchronized <T> T deserialize(final InputStream input, final Class<T> clazz) {
    // read the input stream into an object. we use the the (apache commons-io) ClassLoaderObjectInputStream
    // to read the object because we need to be able to use the same class loader that loaded the class in
    // the first place (for example, the RestfulClassLoader).
    T object;/*w  ww  .j a  va 2s  .c o m*/
    try (final ClassLoaderObjectInputStream in = new ClassLoaderObjectInputStream(clazz.getClassLoader(),
            input)) {
        object = clazz.cast(in.readObject());
    } catch (IOException | ClassNotFoundException e) {
        final StringBuilder message = new StringBuilder();
        message.append("Unable to serialize object to output stream:").append(Constants.NEW_LINE);
        message.append("  Input Stream Type: ").append(input.getClass().getName()).append(Constants.NEW_LINE);
        message.append("  Object Type: ").append(clazz.getName()).append(Constants.NEW_LINE);
        LOGGER.error(message.toString(), e);
        throw new IllegalArgumentException(message.toString(), e);
    }
    return object;
}

From source file:org.kepler.objectmanager.cache.ActorCacheObject.java

/**
 * create an ActorCacheObject from a stream
 * /*from   w  w w .j av  a 2s.  co  m*/
 * @param actorStream
 *            the stream of the actor moml
 * @exception CacheException
 *                Description of the Exception
 */
public ActorCacheObject(InputStream actorStream) throws CacheException {
    super();
    if (isDebugging)
        log.debug("ActorCacheObject(" + actorStream.getClass().getName() + ")");

    KeplerActorMetadata kam = null;
    try {
        kam = KeplerMetadataExtractor.extractActorMetadata(actorStream);
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    if (kam != null) {
        _actorString = kam.getActorString();
        this.setName(kam.getName());
        this.setLSID(kam.getLsid());
        _className = kam.getClassName();
        _rootname = kam.getRootName();
        setSemanticTypes(kam.getSemanticTypes());

        Map<String, String> attributes = kam.getAttributes();
        if (attributes != null) {
            for (String attributeName : attributes.keySet()) {
                String attributeValue = attributes.get(attributeName);
                this.addAttribute(attributeName, attributeValue);

            }
        }
    }
}

From source file:org.microtitan.diffusive.diffuser.serializer.ObjectSerializer.java

@Override
public synchronized <T> T deserialize(final InputStream input, final Class<T> clazz) {
    // read the input stream into an object. we use the the (apache commons-io) ClassLoaderObjectInputStream
    // to read the object because we need to be able to use the same class loader that loaded the class in
    // the first place (for example, the RestfulClassLoader).
    T object = null;/*from w  w  w .  j av a 2  s  .  c o  m*/
    try (final ClassLoaderObjectInputStream in = new ClassLoaderObjectInputStream(clazz.getClassLoader(),
            input)) {
        object = clazz.cast(in.readObject());
    } catch (IOException | ClassNotFoundException e) {
        final StringBuffer message = new StringBuffer();
        message.append("Unable to serialize object to output stream:" + Constants.NEW_LINE);
        message.append("  Input Stream Type: " + input.getClass().getName() + Constants.NEW_LINE);
        message.append("  Object Type: " + clazz.getName() + Constants.NEW_LINE);
        LOGGER.error(message.toString(), e);
        throw new IllegalArgumentException(message.toString(), e);
    }
    return object;
}

From source file:org.mule.transport.sftp.SftpUtil.java

public void setErrorOccurredOnInputStream(InputStream inputStream) {

    if (isKeepFileOnError()) {
        // If an exception occurs and the keepFileOnError property is
        // true, keep the file on the originating endpoint
        // Note: this is only supported when using the sftp transport on
        // both inbound & outbound
        if (inputStream != null) {
            if (inputStream instanceof ErrorOccurredDecorator) {
                // Ensure that the SftpInputStream or
                // SftpFileArchiveInputStream knows about the error and
                // dont delete the file
                ((ErrorOccurredDecorator) inputStream).setErrorOccurred();

            } else {
                logger.warn("Class " + inputStream.getClass().getName()
                        + " did not implement the 'ErrorOccurred' decorator, errorOccured=true could not be set.");
            }//from  www  .  jav a  2 s.  c o m
        }
    }
}

From source file:org.roda.core.common.RodaUtils.java

/**
 * Closes an input stream while inspecting its class. The inspection is done
 * to avoid closing streams associates with jar files that may cause later
 * errors like// ww  w  .j  av a 2  s  . c  o m
 * 
 * <pre>
 * Caused by: java.io.IOException: Stream closed
 * </pre>
 * 
 * So, all streams whose class name does not start with
 * <code>sun.net.www.protocol.jar.JarURLConnection</code> will be closed
 * 
 * @throws IOException
 * 
 * @since 2016-09-20
 */
public static void close(InputStream inputstream) throws IOException {
    if (inputstream != null) {
        String inputstreamClassName = inputstream.getClass().getName();
        if (!inputstreamClassName.startsWith("sun.net.www.protocol.jar.JarURLConnection")) {
            inputstream.close();
        }
    }
}