001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.opends.server.util;
028
029import java.io.InputStream;
030import java.io.IOException;
031
032/**
033 * An implementation of input stream that enforces an read size limit.
034 */
035public class SizeLimitInputStream extends InputStream
036{
037  private int bytesRead;
038  private int markBytesRead;
039  private int readLimit;
040  private InputStream parentStream;
041
042  /**
043   * Creates a new a new size limit input stream.
044   *
045   * @param parentStream
046   *          The parent stream.
047   * @param readLimit
048   *          The size limit.
049   */
050  public SizeLimitInputStream(InputStream parentStream, int readLimit)
051  {
052    this.parentStream = parentStream;
053    this.readLimit = readLimit;
054  }
055
056  /** {@inheritDoc} */
057  public int available() throws IOException
058  {
059    int streamAvail = parentStream.available();
060    int limitedAvail = readLimit - bytesRead;
061    return limitedAvail < streamAvail ? limitedAvail : streamAvail;
062  }
063
064  /** {@inheritDoc} */
065  public synchronized void mark(int readlimit)
066  {
067    parentStream.mark(readlimit);
068    markBytesRead = bytesRead;
069  }
070
071  /** {@inheritDoc} */
072  public int read() throws IOException
073  {
074    if(bytesRead >= readLimit)
075    {
076      return -1;
077    }
078
079    int b = parentStream.read();
080    if (b != -1)
081    {
082      ++bytesRead;
083    }
084    return b;
085  }
086
087  /** {@inheritDoc} */
088  public int read(byte b[], int off, int len) throws IOException
089  {
090    if(off < 0 || len < 0 || off+len > b.length)
091    {
092      throw new IndexOutOfBoundsException();
093    }
094
095    if(len == 0)
096    {
097      return 0;
098    }
099
100    if(bytesRead >= readLimit)
101    {
102      return -1;
103    }
104
105    if(bytesRead + len > readLimit)
106    {
107      len = readLimit - bytesRead;
108    }
109
110    int readLen = parentStream.read(b, off, len);
111    if(readLen > 0)
112    {
113      bytesRead += readLen;
114    }
115    return readLen;
116  }
117
118  /** {@inheritDoc} */
119  public synchronized void reset() throws IOException
120  {
121    parentStream.reset();
122    bytesRead = markBytesRead;
123  }
124
125  /** {@inheritDoc} */
126  public long skip(long n) throws IOException
127  {
128    if(bytesRead + n > readLimit)
129    {
130      n = readLimit - bytesRead;
131    }
132
133    bytesRead += n;
134    return parentStream.skip(n);
135  }
136
137  /** {@inheritDoc} */
138  public boolean markSupported() {
139    return parentStream.markSupported();
140  }
141
142  /** {@inheritDoc} */
143  public void close() throws IOException {
144    parentStream.close();
145  }
146
147  /**
148   * Retrieves the number of bytes read from this stream.
149   *
150   * @return The number of bytes read from this stream.
151   */
152  public int getBytesRead()
153  {
154    return bytesRead;
155  }
156
157  /**
158   * Retrieves the size limit of this stream.
159   *
160   * @return The size limit of this stream.
161   */
162  public int getSizeLimit()
163  {
164    return readLimit;
165  }
166}