| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.commons.compress; |
| 20 | |
| 21 | /** |
| 22 | * 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 |
| 23 | * to allocate a byte array that is larger than the allowable limit. |
| 24 | * |
| 25 | * @since 1.14 |
| 26 | */ |
| 27 | public class MemoryLimitException extends CompressException { |
| 28 | |
| 29 | private static final long serialVersionUID = 1L; |
| 30 | |
| 31 | private static String buildMessage(final long memoryNeededInKb, final int memoryLimitInKb) { |
| 32 | return String.format("%,d KiB of memory would be needed; limit was %,d KiB. If the file is not corrupt, consider increasing the memory limit.", |
| 33 | memoryNeededInKb, memoryLimitInKb); |
| 34 | } |
| 35 | |
| 36 | /** A long instead of int to account for overflow for corrupt files. */ |
| 37 | private final long memoryNeededKiB; |
| 38 | private final int memoryLimitKiB; |
| 39 | |
| 40 | /** |
| 41 | * Constructs a new instance. |
| 42 | * |
| 43 | * @param memoryNeededKiB The memory needed in kibibytes (KiB). |
| 44 | * @param memoryLimitKiB The memory limit in kibibytes (KiB). |
| 45 | */ |
| 46 | public MemoryLimitException(final long memoryNeededKiB, final int memoryLimitKiB) { |
| 47 | super(buildMessage(memoryNeededKiB, memoryLimitKiB)); |
| 48 | this.memoryNeededKiB = memoryNeededKiB; |
| 49 | this.memoryLimitKiB = memoryLimitKiB; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Constructs a new instance. |
| 54 | * |
| 55 | * @param memoryNeededKiB The memory needed in kibibytes (KiB). |
| 56 | * @param memoryLimitKiB The memory limit in kibibytes (KiB). |
| 57 | * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method). (A null value is permitted, and indicates that |
| 58 | * the cause is nonexistent or unknown.) |
| 59 | * @deprecated Use {@link #MemoryLimitException(long, int, Throwable)}. |
| 60 | */ |
| 61 | @Deprecated |
| 62 | public MemoryLimitException(final long memoryNeededKiB, final int memoryLimitKiB, final Exception cause) { |
| 63 | super(buildMessage(memoryNeededKiB, memoryLimitKiB), cause); |
| 64 | this.memoryNeededKiB = memoryNeededKiB; |
| 65 | this.memoryLimitKiB = memoryLimitKiB; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Constructs a new instance. |
| 70 | * |
| 71 | * @param memoryNeededKiB The memory needed in kibibytes (KiB). |
| 72 | * @param memoryLimitKiB The memory limit in kibibytes (KiB). |
| 73 | * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} method). (A null value is permitted, and indicates that |
| 74 | * the cause is nonexistent or unknown.) |
| 75 | */ |
| 76 | public MemoryLimitException(final long memoryNeededKiB, final int memoryLimitKiB, final Throwable cause) { |
| 77 | super(buildMessage(memoryNeededKiB, memoryLimitKiB), cause); |
| 78 | this.memoryNeededKiB = memoryNeededKiB; |
| 79 | this.memoryLimitKiB = memoryLimitKiB; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Gets the memory limit in kibibytes (KiB). |
| 84 | * |
| 85 | * @return the memory limit in kibibytes (KiB). |
| 86 | */ |
| 87 | public int getMemoryLimitInKb() { |
| 88 | return memoryLimitKiB; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Gets the memory needed in kibibytes (KiB). |
| 93 | * |
| 94 | * @return the memory needed in kibibytes (KiB). |
| 95 | */ |
| 96 | public long getMemoryNeededInKb() { |
| 97 | return memoryNeededKiB; |
| 98 | } |
| 99 | } |