Example usage for org.apache.commons.io.input BoundedInputStream BoundedInputStream

List of usage examples for org.apache.commons.io.input BoundedInputStream BoundedInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input BoundedInputStream BoundedInputStream.

Prototype

public BoundedInputStream(InputStream in, long size) 

Source Link

Document

Creates a new BoundedInputStream that wraps the given input stream and limits it to a certain size.

Usage

From source file:org.kuali.student.git.model.NodeProcessor.java

private ObjectId storeBlob(GitBranchData gbd, String path, long contentLength, long propContentLength) {

    try {// w ww.  j a  v a 2s  . c o  m

        long adjustedContentLength = contentLength - propContentLength;

        if (propContentLength > 0) {
            getInputStream().skip(propContentLength);
        }

        /*
         * We want to replace the contents of the path file with the content
         * that we read from the input stream.
         */

        ObjectInserter objectInserter = repo.newObjectInserter();

        ObjectId id = objectInserter.insert(Constants.OBJ_BLOB, adjustedContentLength,
                new BoundedInputStream(getInputStream(), adjustedContentLength));

        objectInserter.flush();
        objectInserter.release();

        return id;

    } catch (Exception e) {

        log.error("onNodeContentChanged failed: ", e);
        throw new RuntimeException("onNodeContentLength failed", e);
    }
}

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
    /* //from  w w w. j  a va2 s  .c o  m
     * 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.lockss.plugin.usdocspln.gov.gpo.fdsys.GPOFDSysHtmlFilterFactory.java

public boolean shouldFilter(InputStream is, String encoding) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(new BoundedInputStream(is, 2000), encoding));
    boolean title42 = false;
    for (String line = br.readLine(); line != null; line = br.readLine()) {
        if (logger.isDebug3()) {
            logger.debug3("line: " + line);
        }//from w w  w  . j a v a  2  s.  co m
        if (title42) {
            Matcher chapMat = chapPat.matcher(line);
            if (chapMat.find()) {
                logger.debug2("Chapter of Title 42");
                return true;
            }
        } else {
            if (line.contains("<title>")) {
                logger.debug2("<title> line");
                Matcher titleMat = titlePat.matcher(line);
                if (titleMat.find()) {
                    logger.debug2("Title 42");
                    title42 = true;
                } else {
                    logger.debug2("Not Title 42");
                    return true;
                }
            }
        }
    }
    // Filter iff inside Title 42 but not a chapter thereof
    logger.debug2(title42 ? "Title 42 but not a chapter thereof" : "Not Title 42 (final)");
    return !title42;
}

From source file:org.primefaces.component.imagecropper.ImageCropperRenderer.java

@Override
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue)
        throws ConverterException {
    String coords = (String) submittedValue;

    if (isValueBlank(coords)) {
        return null;
    }//from w  w w.  j av a 2 s  .c  o m

    String[] cropCoords = coords.split("_");

    int x = (int) Double.parseDouble(cropCoords[0]);
    int y = (int) Double.parseDouble(cropCoords[1]);
    int w = (int) Double.parseDouble(cropCoords[2]);
    int h = (int) Double.parseDouble(cropCoords[3]);

    if (w <= 0 || h <= 0) {
        return null;
    }

    ImageCropper cropper = (ImageCropper) component;
    Resource resource = getImageResource(context, cropper);
    InputStream inputStream = null;
    String imagePath = cropper.getImage();
    String contentType = null;

    try {

        if (resource != null && !"RES_NOT_FOUND".equals(resource.toString())) {
            inputStream = resource.getInputStream();
            contentType = resource.getContentType();
        } else {

            boolean isExternal = imagePath.startsWith("http");

            if (isExternal) {
                URL url = new URL(imagePath);
                URLConnection urlConnection = url.openConnection();
                inputStream = urlConnection.getInputStream();
                contentType = urlConnection.getContentType();
            } else {
                ExternalContext externalContext = context.getExternalContext();
                File file = new File(externalContext.getRealPath("") + imagePath);
                inputStream = new FileInputStream(file);
            }
        }

        // wrap input stream by BoundedInputStream to prevent uncontrolled resource consumption (#3286)
        if (cropper.getSizeLimit() != null) {
            inputStream = new BoundedInputStream(inputStream, cropper.getSizeLimit());
        }

        BufferedImage outputImage = ImageIO.read(inputStream);

        // avoid java.awt.image.RasterFormatException: (x + width) is outside of Raster
        // see #1208
        if (x + w > outputImage.getWidth()) {
            w = outputImage.getWidth() - x;
        }
        if (y + h > outputImage.getHeight()) {
            h = outputImage.getHeight() - y;
        }

        BufferedImage cropped = outputImage.getSubimage(x, y, w, h);
        ByteArrayOutputStream croppedOutImage = new ByteArrayOutputStream();
        String format = guessImageFormat(contentType, imagePath);
        ImageIO.write(cropped, format, croppedOutImage);

        return new CroppedImage(cropper.getImage(), croppedOutImage.toByteArray(), x, y, w, h);
    } catch (IOException e) {
        throw new ConverterException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.severe(e.getMessage());
            }
        }
    }
}

From source file:org.primefaces.model.NativeUploadedFile.java

@Override
public InputStream getInputstream() throws IOException {
    return sizeLimit == null ? part.getInputStream() : new BoundedInputStream(part.getInputStream(), sizeLimit);
}

From source file:org.swiftexplorer.swift.instructions.FastSegmentationPlanFile.java

@Override
protected InputStream createSegment() throws IOException {
    InputStream res = new BoundedInputStream(
            Channels.newInputStream(
                    this.randomAccessFile.getChannel().position(currentSegment * segmentationSize)),
            segmentationSize);//ww  w. j  a v  a2 s.  c o m
    ((BoundedInputStream) res).setPropagateClose(false);
    return res;
}

From source file:org.trellisldp.file.FileUtils.java

/**
 * Get a bounded inputstream./*from  ww  w.j a v  a2  s.com*/
 * @param stream the input stream
 * @param from the byte from which to start
 * @param to the byte to which to read
 * @throws IOException if an error occurs when skipping forward
 * @return the bounded inputstream
 */
public static InputStream getBoundedStream(final InputStream stream, final int from, final int to)
        throws IOException {
    final long skipped = stream.skip(from);
    LOGGER.debug("Skipped {} bytes", skipped);
    return new BoundedInputStream(stream, to - from);
}

From source file:org.trellisldp.http.impl.GetHandler.java

private ResponseBuilder getLdpNr(final String identifier, final Resource res, final ResponseBuilder builder) {

    final Instant mod = res.getBinary().map(Binary::getModified).orElseThrow(
            () -> new WebApplicationException("Could not access binary metadata for " + res.getIdentifier()));
    final EntityTag etag = new EntityTag(md5Hex(mod + identifier + "BINARY"));
    checkCache(req.getRequest(), mod, etag);

    // Set last-modified to be the binary's last-modified value
    builder.lastModified(from(mod));/*w  ww .jav a 2s.  com*/

    final IRI dsid = res.getBinary().map(Binary::getIdentifier).orElseThrow(
            () -> new WebApplicationException("Could not access binary metadata for " + res.getIdentifier()));

    builder.header(VARY, RANGE).header(VARY, WANT_DIGEST).header(ACCEPT_RANGES, "bytes").tag(etag);

    if (res.isMemento()) {
        builder.header(ALLOW, join(",", GET, HEAD, OPTIONS));
    } else {
        builder.header(ALLOW, join(",", GET, HEAD, OPTIONS, PUT, DELETE));
    }

    // Add instance digests, if Requested and supported
    ofNullable(req.getWantDigest()).map(WantDigest::getAlgorithms).ifPresent(
            algs -> algs.stream().filter(binaryService.supportedAlgorithms()::contains).findFirst().ifPresent(
                    alg -> getBinaryDigest(dsid, alg).ifPresent(digest -> builder.header(DIGEST, digest))));

    // Stream the binary content
    final StreamingOutput stream = new StreamingOutput() {
        @Override
        public void write(final OutputStream out) throws IOException {
            // TODO -- with JDK 9 use InputStream::transferTo instead of IOUtils::copy
            try (final InputStream binary = binaryService.getContent(req.getPartition(), dsid)
                    .orElseThrow(() -> new IOException("Could not retrieve content from " + dsid))) {
                if (isNull(req.getRange())) {
                    IOUtils.copy(binary, out);
                } else {
                    // Range Requests
                    final long skipped = binary.skip(req.getRange().getFrom());
                    if (skipped < req.getRange().getFrom()) {
                        LOGGER.warn("Trying to skip more data available in the input stream! {}, {}", skipped,
                                req.getRange().getFrom());
                    }
                    try (final InputStream sliced = new BoundedInputStream(binary,
                            req.getRange().getTo() - req.getRange().getFrom())) {
                        IOUtils.copy(sliced, out);
                    }
                }
            } catch (final IOException ex) {
                throw new WebApplicationException("Error processing binary content: " + ex.getMessage());
            }
        }
    };

    return builder.entity(stream);
}

From source file:org.warcbase.data.ArcRecordUtils.java

public static byte[] toBytes(ARCRecord record) throws IOException {
    ARCRecordMetaData meta = record.getMetaData();

    String metaline = meta.getUrl() + " " + meta.getIp() + " " + meta.getDate() + " " + meta.getMimetype() + " "
            + (int) meta.getLength();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(baos);
    dout.write(metaline.getBytes());/*  w  ww  .  j  av a 2  s. c  om*/
    dout.write("\n".getBytes());

    long recordLength = meta.getLength();
    long len = IOUtils.copyLarge(new BoundedInputStream(record, recordLength), dout);
    if (len != recordLength) {
        LOG.error("Read " + len + " bytes but expected " + recordLength + " bytes. Continuing...");
    }
    return baos.toByteArray();
}

From source file:org.warcbase.data.ArcRecordUtils.java

private static byte[] copyToByteArray(InputStream is, final int recordLength, boolean enforceLength)
        throws IOException {

    BoundedInputStream bis = new BoundedInputStream(is, recordLength);
    byte[] rawContents = IOUtils.toByteArray(bis);
    if (enforceLength && rawContents.length != recordLength) {
        LOG.error(/*w w w  .j a va 2 s  . c om*/
                "Read " + rawContents.length + " bytes but expected " + recordLength + " bytes. Continuing...");
    }
    return rawContents;
}