Example usage for java.io InputStream markSupported

List of usage examples for java.io InputStream markSupported

Introduction

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

Prototype

public boolean markSupported() 

Source Link

Document

Tests if this input stream supports the mark and reset methods.

Usage

From source file:cn.ctyun.amazonaws.services.s3.transfer.internal.UploadCallable.java

/**
 * Uploads all parts in the request in serial in this thread, then completes
 * the upload and returns the result.//www. j a v a  2 s.  co  m
 */
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {

    final List<PartETag> partETags = new ArrayList<PartETag>();

    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
        // Mark the stream in case we need to reset it
        InputStream inputStream = uploadPartRequest.getInputStream();
        if (inputStream != null && inputStream.markSupported()) {
            if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
                inputStream.mark(Integer.MAX_VALUE);
            } else {
                inputStream.mark((int) uploadPartRequest.getPartSize());
            }
        }
        partETags.add(s3.uploadPart(uploadPartRequest).getPartETag());
    }

    CompleteMultipartUploadResult completeMultipartUploadResult = s3
            .completeMultipartUpload(new CompleteMultipartUploadRequest(putObjectRequest.getBucketName(),
                    putObjectRequest.getKey(), multipartUploadId, partETags));

    fireProgressEvent(ProgressEvent.COMPLETED_EVENT_CODE);

    UploadResult uploadResult = new UploadResult();
    uploadResult.setBucketName(completeMultipartUploadResult.getBucketName());
    uploadResult.setKey(completeMultipartUploadResult.getKey());
    uploadResult.setETag(completeMultipartUploadResult.getETag());
    uploadResult.setVersionId(completeMultipartUploadResult.getVersionId());
    return uploadResult;
}

From source file:org.xmlactions.email.EMailParser.java

private void addContent(InputStream inputStream) throws DocumentException, IOException {

    if (inputStream.markSupported()) {
        inputStream.mark(inputStream.available());
    }/*  w  w  w .  j  a  v  a  2s.c o m*/

    String bodyContent = IOUtils.toString(inputStream);

    if (this.firstMessageProcessed == false) {
    }
    if (inputStream.markSupported()) {
        inputStream.reset();
    }
}

From source file:fr.mby.utils.common.prefs.StreamPreferences.java

/**
 * @param parent//w w  w . j a v a  2 s.c om
 * @param name
 */
public StreamPreferences(final AbstractPreferences parent, final String name, final InputStream inputStream,
        final OutputStream outputStream) {
    super(parent, name);

    Assert.notNull(inputStream, "No InputStream provided !");
    Assert.notNull(outputStream, "No OutputStream provided !");
    Assert.isTrue(inputStream.markSupported(), "The provided InputStream does not support mark !");

    this.inputStreamStorage = inputStream;
    this.outputStreamStorage = outputStream;

    this.prefsStorage = new ConcurrentHashMap<String, String>();
    this.childrenStorage = new ConcurrentHashMap<String, AbstractPreferences>();
}

From source file:com.sina.cloudstorage.services.scs.transfer.internal.UploadCallable.java

/**
 * Uploads all parts in the request in serial in this thread, then completes
 * the upload and returns the result.//from  www.j  a va2  s  .  c  o m
 */
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {

    final List<PartETag> partETags = new ArrayList<PartETag>();

    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
        // Mark the stream in case we need to reset it
        InputStream inputStream = uploadPartRequest.getInputStream();
        if (inputStream != null && inputStream.markSupported()) {
            if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
                inputStream.mark(Integer.MAX_VALUE);
            } else {
                inputStream.mark((int) uploadPartRequest.getPartSize());
            }
        }
        partETags.add(s3.uploadPart(uploadPartRequest).getPartETag());
    }

    //        CompleteMultipartUploadResult completeMultipartUploadResult = s3
    //                .completeMultipartUpload(new CompleteMultipartUploadRequest(putObjectRequest.getBucketName(),
    //                        putObjectRequest.getKey(), multipartUploadId, partETags));
    //        UploadResult uploadResult = new UploadResult();
    //        uploadResult.setBucketName(completeMultipartUploadResult.getBucketName());
    //        uploadResult.setKey(completeMultipartUploadResult.getKey());
    //        uploadResult.setETag(completeMultipartUploadResult.getETag());
    //        uploadResult.setVersionId(completeMultipartUploadResult.getVersionId());
    //        return uploadResult;

    s3.completeMultipartUpload(new CompleteMultipartUploadRequest(putObjectRequest.getBucketName(),
            putObjectRequest.getKey(), multipartUploadId, partETags));

    UploadResult uploadResult = new UploadResult();
    //        uploadResult.setBucketName(completeMultipartUploadResult.getBucketName());
    //        uploadResult.setKey(completeMultipartUploadResult.getKey());
    //        uploadResult.setETag(completeMultipartUploadResult.getETag());
    //        uploadResult.setVersionId(completeMultipartUploadResult.getVersionId());
    return uploadResult;
}

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

/**
 * Extract contents of an archive./*from w  w w .ja  va 2  s . c o m*/
 * 
 * @param dest_dir
 *            Destination to write archive content.
 * @param is
 *            Archive file.
 * @return Name of package base directory in dest_dir
 * @throws ArchiveException if there is an error creating the ArchiveInputStream
 * @throws IOException  if there is more than one package root
 */
protected String extract(File dest_dir, InputStream is) throws ArchiveException, IOException {
    // Apache commons compress requires buffered input streams

    if (!is.markSupported()) {
        is = new BufferedInputStream(is);
    }

    // If file is compressed, uncompress.

    try {
        is = new CompressorStreamFactory().createCompressorInputStream(is);
    } catch (CompressorException e) {
    }

    // Extract entries from archive

    if (!is.markSupported()) {
        is = new BufferedInputStream(is);
    }

    String archive_base = null;

    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(is);
    ArchiveEntry entry;

    while ((entry = ais.getNextEntry()) != null) {
        File file = extract(dest_dir, entry, ais);

        String root = get_root_file_name(file);

        if (archive_base == null) {
            archive_base = root;
        } else if (!archive_base.equals(root)) {
            throw new IOException("Package has more than one base directory.");
        }
    }

    return archive_base;
}

From source file:org.apache.tika.parser.audio.AudioParser.java

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    // AudioSystem expects the stream to support the mark feature
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }/*from  w ww.j a  v  a 2s.c om*/
    stream = new SkipFullyInputStream(stream);
    try {
        AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(stream);
        Type type = fileFormat.getType();
        if (type == Type.AIFC || type == Type.AIFF) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/x-aiff");
        } else if (type == Type.AU || type == Type.SND) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/basic");
        } else if (type == Type.WAVE) {
            metadata.set(Metadata.CONTENT_TYPE, "audio/vnd.wave");
        }

        AudioFormat audioFormat = fileFormat.getFormat();
        int channels = audioFormat.getChannels();
        if (channels != AudioSystem.NOT_SPECIFIED) {
            metadata.set("channels", String.valueOf(channels));
            // TODO: Use XMPDM.TRACKS? (see also frame rate in AudioFormat)
        }
        float rate = audioFormat.getSampleRate();
        if (rate != AudioSystem.NOT_SPECIFIED) {
            metadata.set("samplerate", String.valueOf(rate));
            metadata.set(XMPDM.AUDIO_SAMPLE_RATE, Integer.toString((int) rate));
        }
        int bits = audioFormat.getSampleSizeInBits();
        if (bits != AudioSystem.NOT_SPECIFIED) {
            metadata.set("bits", String.valueOf(bits));
            if (bits == 8) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "8Int");
            } else if (bits == 16) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "16Int");
            } else if (bits == 32) {
                metadata.set(XMPDM.AUDIO_SAMPLE_TYPE, "32Int");
            }
        }
        metadata.set("encoding", audioFormat.getEncoding().toString());

        // Javadoc suggests that some of the following properties might
        // be available, but I had no success in finding any:

        // "duration" Long playback duration of the file in microseconds
        // "author" String name of the author of this file
        // "title" String title of this file
        // "copyright" String copyright message
        // "date" Date date of the recording or release
        // "comment" String an arbitrary text

        addMetadata(metadata, fileFormat.properties());
        addMetadata(metadata, audioFormat.properties());
    } catch (UnsupportedAudioFileException e) {
        // There is no way to know whether this exception was
        // caused by the document being corrupted or by the format
        // just being unsupported. So we do nothing.
    }

    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    xhtml.endDocument();
}

From source file:com.seajas.search.contender.service.storage.StorageService.java

/**
 * Store the given content in GridFS and return the relevant ObjectId.
 *
 * @param content/*from w ww .j  a  va  2 s . c o m*/
 * @return ObjectId
 */
private ObjectId storeContent(final InputStream content) {
    if (content == null)
        throw new IllegalArgumentException("Content storage was requested but no content was given");

    if (!content.markSupported())
        logger.warn(
                "Marking of the (original) content stream is not supported - will not reset the stream after storage");

    GridFSInputFile inputFile = gridFs.createFile(content, false);

    try {
        inputFile.save();
    } finally {
        if (content.markSupported())
            try {
                content.reset();
            } catch (IOException e) {
                logger.error("Unable to reset the given stream, despite mark() being supported", e);
            }

        return (ObjectId) inputFile.getId();
    }
}

From source file:org.guvnor.m2repo.backend.server.helpers.HttpPostHelper.java

private String uploadJar(final FormData formData) throws IOException {
    GAV gav = formData.getGav();/*w w w.j  a  va 2s.  com*/
    InputStream jarStream = null;

    try {
        jarStream = formData.getFile().getInputStream();
        if (gav == null) {
            if (!jarStream.markSupported()) {
                jarStream = new BufferedInputStream(jarStream);
            }

            // is available() safe?
            jarStream.mark(jarStream.available());

            PomModel pomModel = PomModelResolver.resolveFromJar(jarStream);

            //If we were able to get a POM model we can get the GAV
            if (pomModel != null) {
                String groupId = pomModel.getReleaseId().getGroupId();
                String artifactId = pomModel.getReleaseId().getArtifactId();
                String version = pomModel.getReleaseId().getVersion();

                if (isNullOrEmpty(groupId) || isNullOrEmpty(artifactId) || isNullOrEmpty(version)) {
                    return UPLOAD_MISSING_POM;
                } else {
                    gav = new GAV(groupId, artifactId, version);
                }

            } else {
                return UPLOAD_MISSING_POM;
            }
            jarStream.reset();
        }

        m2RepoService.deployJar(jarStream, gav);

        return UPLOAD_OK;

    } catch (IOException ioe) {
        log.error(ioe.getMessage(), ioe);
        throw ExceptionUtilities.handleException(ioe);

    } finally {
        if (jarStream != null) {
            jarStream.close();
        }

    }
}

From source file:org.cryptonode.jncryptor.AES256JNCryptorInputStreamTest.java

@Test
public void testMarkNotSupported() throws Exception {
    byte[] plaintext = getRandomBytes(1);

    final String password = "Testing1234";

    JNCryptor cryptor = new AES256JNCryptor();
    byte[] data = cryptor.encryptData(plaintext, password.toCharArray());

    InputStream in = new AES256JNCryptorInputStream(new ByteArrayInputStream(data), password.toCharArray());
    assertFalse(in.markSupported());
    in.close();//from  w w w.j  a v a 2s .  c o m
}

From source file:org.guvnor.m2repo.backend.server.FileServlet.java

public String uploadFile(FormData uploadItem) throws IOException {
    InputStream fileData = uploadItem.getFile().getInputStream();
    String fileName = uploadItem.getFile().getName();
    GAV gav = uploadItem.getGav();/*from  ww w. j a  v  a 2  s  .c  o  m*/

    try {
        if (gav == null) {
            if (!fileData.markSupported()) {
                fileData = new BufferedInputStream(fileData);
            }

            fileData.mark(fileData.available()); // is available() safe?
            String pom = GuvnorM2Repository.loadPOMFromJar(fileData);
            fileData.reset();

            if (pom != null) {
                Model model = new MavenXpp3Reader().read(new StringReader(pom));

                String groupId = model.getGroupId();
                String artifactId = model.getArtifactId();
                String version = model.getVersion();

                if (groupId == null) {
                    groupId = model.getParent().getGroupId();
                }
                if (version == null) {
                    version = model.getParent().getVersion();
                }

                gav = new GAV(groupId, artifactId, version);
            } else {
                return "NO VALID POM";
            }
        }

        m2RepoService.deployJar(fileData, gav);
        uploadItem.getFile().getInputStream().close();

        return "OK";
    } catch (XmlPullParserException e) {
    } catch (IOException ioe) {
    }

    return "INTERNAL ERROR";
}