001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.commons.compress.archivers.sevenz;
019
020/**
021 * Collects options for reading 7z archives.
022 *
023 * @since 1.19
024 * @Immutable
025 */
026public class SevenZFileOptions {
027    private static final int DEFAUL_MEMORY_LIMIT_IN_KB = Integer.MAX_VALUE;
028    private static final boolean DEFAULT_USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES= false;
029
030    private final int maxMemoryLimitInKb;
031    private final boolean useDefaultNameForUnnamedEntries;
032
033    private SevenZFileOptions(int maxMemoryLimitInKb, boolean useDefaultNameForUnnamedEntries) {
034        this.maxMemoryLimitInKb = maxMemoryLimitInKb;
035        this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries;
036    }
037
038    /**
039     * The default options.
040     *
041     * <ul>
042     *   <li>no memory limit</li>
043     *   <li>don't modifiy the name of unnamed entries</li>
044     * </ul>
045     */
046    public static final SevenZFileOptions DEFAULT = new SevenZFileOptions(DEFAUL_MEMORY_LIMIT_IN_KB,
047        DEFAULT_USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES);
048
049    /**
050     * Obtains a builder for SevenZFileOptions.
051     * @return a builder for SevenZFileOptions.
052     */
053    public static Builder builder() {
054        return new Builder();
055    }
056
057    /**
058     * Gets the maximum amount of memory to use for extraction. Not
059     * all codecs will honor this setting. Currently only lzma and
060     * lzma2 are supported.
061     * @return the maximum amount of memory to use for extraction
062     */
063    public int getMaxMemoryLimitInKb() {
064        return maxMemoryLimitInKb;
065    }
066
067    /**
068     * Gets whether entries without a name should get their names set
069     * to the archive's default file name.
070     * @return whether entries without a name should get their names
071     * set to the archive's default file name
072     */
073    public boolean getUseDefaultNameForUnnamedEntries() {
074        return useDefaultNameForUnnamedEntries;
075    }
076
077    /**
078     * Mutable builder for the immutable {@link SevenZFileOptions}.
079     *
080     * @since 1.19
081     */
082    public static class Builder {
083        private int maxMemoryLimitInKb = DEFAUL_MEMORY_LIMIT_IN_KB;
084        private boolean useDefaultNameForUnnamedEntries = DEFAULT_USE_DEFAULTNAME_FOR_UNNAMED_ENTRIES;
085        /**
086         * Sets the maximum amount of memory to use for
087         * extraction. Not all codecs will honor this
088         * setting. Currently only lzma and lzma2 are supported.
089         *
090         * @param maxMemoryLimitInKb limit of the maximum amount of memory to use
091         * @return the reconfigured builder
092         */
093        public Builder withMaxMemoryLimitInKb(int maxMemoryLimitInKb) {
094            this.maxMemoryLimitInKb = maxMemoryLimitInKb;
095            return this;
096        }
097
098        /**
099         * Sets whether entries without a name should get their names
100         * set to the archive's default file name.
101         *
102         * @param useDefaultNameForUnnamedEntries if true the name of
103         * unnamed entries will be set to the archive's default name
104         * @return the reconfigured builder
105         */
106        public Builder withUseDefaultNameForUnnamedEntries(boolean useDefaultNameForUnnamedEntries) {
107            this.useDefaultNameForUnnamedEntries = useDefaultNameForUnnamedEntries;
108            return this;
109        }
110
111        /**
112         * Create the {@link SevenZFileOptions}.
113         *
114         * @return configured {@link SevenZFileOptions}.
115         */
116        public SevenZFileOptions build() {
117            return new SevenZFileOptions(maxMemoryLimitInKb, useDefaultNameForUnnamedEntries);
118        }
119    }
120}