Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6616FD107 for ; Thu, 16 May 2013 14:39:17 +0000 (UTC) Received: (qmail 84953 invoked by uid 500); 16 May 2013 14:39:16 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 84862 invoked by uid 500); 16 May 2013 14:39:16 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 84740 invoked by uid 99); 16 May 2013 14:39:16 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 May 2013 14:39:16 +0000 Date: Thu, 16 May 2013 14:39:16 +0000 (UTC) From: "Sebb (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Resolved] (IO-339) MaxBytesInputStream to limit size of bytes read MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/IO-339?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Sebb resolved IO-339. --------------------- Resolution: Duplicate Code already existed. > MaxBytesInputStream to limit size of bytes read > ----------------------------------------------- > > Key: IO-339 > URL: https://issues.apache.org/jira/browse/IO-339 > Project: Commons IO > Issue Type: Improvement > Components: Streams/Writers > Affects Versions: 2.4 > Reporter: Ken Weiner > Priority: Minor > > I wrote an input stream that stops when it reaches a configured size in bytes. I found it useful for applications that download web pages and images, but want to enforce a max size to avoid memory problems for extremely large pages/images. > I was thinking that this class is generic and useful enough to include in the commons-io distribution. Please consider it, and if it isn't appropriate for inclusion here, please recommend a better place to open source and share it. > {code} > package org.apache.commons.io.input; > import java.io.IOException; > import java.io.InputStream; > /** > * An input stream that will end when the amount of bytes read reaches > * the configured amount of maxBytes. > * This is useful for preventing OutOfMemoryExceptions when downloading > * very large files in cases where getting partial content is acceptable. > * @author Ken Weiner > */ > public class MaxBytesInputStream extends CountingInputStream { > private long maxBytes; > public MaxBytesInputStream(InputStream is, long maxBytes) { > super(is); > this.maxBytes = maxBytes; > } > @Override > protected void afterRead(int n) { > super.afterRead((int) Math.min(n, this.maxBytes - getByteCount())); > } > @Override > public int read() throws IOException { > if (getByteCount() < this.maxBytes) { > return super.read(); > } > return -1; > } > @Override > public int read(byte[] b) throws IOException { > if (getByteCount() < this.maxBytes) { > return super.read(b); > } > return -1; > } > @Override > public int read(byte[] b, int off, int len) throws IOException { > if (getByteCount() < this.maxBytes) { > return super.read(b, off, len); > } > return -1; > } > } > {code} -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira