001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress; 020 021import java.io.IOException; 022 023/** 024 * If a stream checks for estimated memory allocation, and the estimate goes above the memory limit, this is thrown. This can also be thrown if a stream tries 025 * to allocate a byte array that is larger than the allowable limit. 026 * 027 * @since 1.14 028 */ 029public class MemoryLimitException extends IOException { 030 031 private static final long serialVersionUID = 1L; 032 033 private static String buildMessage(final long memoryNeededInKb, final int memoryLimitInKb) { 034 return memoryNeededInKb + " kb of memory would be needed; limit was " + memoryLimitInKb + " kb. " 035 + "If the file is not corrupt, consider increasing the memory limit."; 036 } 037 038 /** A long instead of int to account for overflow for corrupt files. */ 039 private final long memoryNeededInKb; 040 041 private final int memoryLimitInKb; 042 043 public MemoryLimitException(final long memoryNeededInKb, final int memoryLimitInKb) { 044 super(buildMessage(memoryNeededInKb, memoryLimitInKb)); 045 this.memoryNeededInKb = memoryNeededInKb; 046 this.memoryLimitInKb = memoryLimitInKb; 047 } 048 049 public MemoryLimitException(final long memoryNeededInKb, final int memoryLimitInKb, final Exception e) { 050 super(buildMessage(memoryNeededInKb, memoryLimitInKb), e); 051 this.memoryNeededInKb = memoryNeededInKb; 052 this.memoryLimitInKb = memoryLimitInKb; 053 } 054 055 public int getMemoryLimitInKb() { 056 return memoryLimitInKb; 057 } 058 059 public long getMemoryNeededInKb() { 060 return memoryNeededInKb; 061 } 062}