| 1 | // SPDX-License-Identifier: 0BSD |
| 2 | // SPDX-FileCopyrightText: The XZ for Java authors and contributors |
| 3 | // SPDX-FileContributor: Lasse Collin <[email protected]> |
| 4 | // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/> |
| 5 | |
| 6 | package org.tukaani.xz; |
| 7 | |
| 8 | import java.io.InputStream; |
| 9 | import java.io.IOException; |
| 10 | |
| 11 | /** |
| 12 | * Base class for filter-specific options classes. |
| 13 | */ |
| 14 | public abstract class FilterOptions implements Cloneable { |
| 15 | /** |
| 16 | * Gets how much memory the encoder will need with |
| 17 | * the given filter chain. This function simply calls |
| 18 | * {@code getEncoderMemoryUsage()} for every filter |
| 19 | * in the array and returns the sum of the returned values. |
| 20 | */ |
| 21 | public static int getEncoderMemoryUsage(FilterOptions[] options) { |
| 22 | int m = 0; |
| 23 | |
| 24 | for (int i = 0; i < options.length; ++i) |
| 25 | m += options[i].getEncoderMemoryUsage(); |
| 26 | |
| 27 | return m; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Gets how much memory the decoder will need with |
| 32 | * the given filter chain. This function simply calls |
| 33 | * {@code getDecoderMemoryUsage()} for every filter |
| 34 | * in the array and returns the sum of the returned values. |
| 35 | */ |
| 36 | public static int getDecoderMemoryUsage(FilterOptions[] options) { |
| 37 | int m = 0; |
| 38 | |
| 39 | for (int i = 0; i < options.length; ++i) |
| 40 | m += options[i].getDecoderMemoryUsage(); |
| 41 | |
| 42 | return m; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Gets how much memory the encoder will need with these options. |
| 47 | */ |
| 48 | public abstract int getEncoderMemoryUsage(); |
| 49 | |
| 50 | /** |
| 51 | * Gets a raw (no XZ headers) encoder output stream using these options. |
| 52 | * Raw streams are an advanced feature. In most cases you want to store |
| 53 | * the compressed data in the .xz container format instead of using |
| 54 | * a raw stream. To use this filter in a .xz file, pass this object |
| 55 | * to XZOutputStream. |
| 56 | * <p> |
| 57 | * This is uses ArrayCache.getDefaultCache() as the ArrayCache. |
| 58 | */ |
| 59 | public FinishableOutputStream getOutputStream(FinishableOutputStream out) { |
| 60 | return getOutputStream(out, ArrayCache.getDefaultCache()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets a raw (no XZ headers) encoder output stream using these options |
| 65 | * and the given ArrayCache. |
| 66 | * Raw streams are an advanced feature. In most cases you want to store |
| 67 | * the compressed data in the .xz container format instead of using |
| 68 | * a raw stream. To use this filter in a .xz file, pass this object |
| 69 | * to XZOutputStream. |
| 70 | */ |
| 71 | public abstract FinishableOutputStream getOutputStream( |
| 72 | FinishableOutputStream out, ArrayCache arrayCache); |
| 73 | |
| 74 | /** |
| 75 | * Gets how much memory the decoder will need to decompress the data |
| 76 | * that was encoded with these options. |
| 77 | */ |
| 78 | public abstract int getDecoderMemoryUsage(); |
| 79 | |
| 80 | /** |
| 81 | * Gets a raw (no XZ headers) decoder input stream using these options. |
| 82 | * <p> |
| 83 | * This is uses ArrayCache.getDefaultCache() as the ArrayCache. |
| 84 | */ |
| 85 | public InputStream getInputStream(InputStream in) throws IOException { |
| 86 | return getInputStream(in, ArrayCache.getDefaultCache()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Gets a raw (no XZ headers) decoder input stream using these options |
| 91 | * and the given ArrayCache. |
| 92 | */ |
| 93 | public abstract InputStream getInputStream( |
| 94 | InputStream in, ArrayCache arrayCache) throws IOException; |
| 95 | |
| 96 | abstract FilterEncoder getFilterEncoder(); |
| 97 | |
| 98 | FilterOptions() {} |
| 99 | } |