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