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 */
017package org.apache.commons.compress.harmony.unpack200;
018
019import org.apache.commons.compress.harmony.pack200.Pack200Exception;
020
021/**
022 * Stores the combinations of bit flags that can be used in the segment header options. Whilst this could be defined in
023 * {@link Segment}, it's cleaner to pull it out into a separate class, not least because methods can then be used to
024 * determine the semantic meaning of the flags. In languages with a pre-processor, these may be defined by macros that
025 * do bitflag manipulation instead.
026 */
027public class SegmentOptions {
028
029    private static final int DEFLATE_HINT = 1 << 5;
030
031    private static final int HAVE_ALL_CODE_FLAGS = 1 << 2;
032
033    private static final int HAVE_CLASS_FLAGS_HI = 1 << 9;
034
035    // private static final int UNUSED_3 = 2^3;
036
037    private static final int HAVE_CODE_FLAGS_HI = 1 << 10;
038
039    private static final int HAVE_CP_NUMBERS = 1 << 1;
040
041    private static final int HAVE_FIELD_FLAGS_HI = 1 << 10;
042
043    private static final int HAVE_FILE_HEADERS = 1 << 4;
044
045    private static final int HAVE_FILE_MODTIME = 1 << 6;
046
047    private static final int HAVE_FILE_OPTIONS = 1 << 7;
048
049    private static final int HAVE_FILE_SIZE_HI = 1 << 8;
050
051    private static final int HAVE_METHOD_FLAGS_HI = 1 << 11;
052
053    private static final int HAVE_SPECIAL_FORMATS = 1 << 0;
054
055    /**
056     * The bit flags that are defined as unused by the specification; specifically, every bit above bit 13 and bit 3.
057     */
058    private static final int UNUSED = -1 << 13 | 1 << 3;
059
060    private final int options;
061
062    /**
063     * Creates a new segment options with the given integer value.
064     *
065     * @param options the integer value to use as the flags
066     * @throws Pack200Exception if an unused bit (bit 3 or bit 13+) is non-zero
067     */
068    public SegmentOptions(final int options) throws Pack200Exception {
069        if ((options & UNUSED) != 0) {
070            throw new Pack200Exception("Some unused flags are non-zero");
071        }
072        this.options = options;
073    }
074
075    public boolean hasAllCodeFlags() {
076        return (options & HAVE_ALL_CODE_FLAGS) != 0;
077    }
078
079    public boolean hasArchiveFileCounts() {
080        return (options & HAVE_FILE_HEADERS) != 0;
081    }
082
083    public boolean hasClassFlagsHi() {
084        return (options & HAVE_CLASS_FLAGS_HI) != 0;
085    }
086
087    public boolean hasCodeFlagsHi() {
088        return (options & HAVE_CODE_FLAGS_HI) != 0;
089    }
090
091    public boolean hasCPNumberCounts() {
092        return (options & HAVE_CP_NUMBERS) != 0;
093    }
094
095    public boolean hasFieldFlagsHi() {
096        return (options & HAVE_FIELD_FLAGS_HI) != 0;
097    }
098
099    public boolean hasFileModtime() {
100        return (options & HAVE_FILE_MODTIME) != 0;
101    }
102
103    public boolean hasFileOptions() {
104        return (options & HAVE_FILE_OPTIONS) != 0;
105    }
106
107    public boolean hasFileSizeHi() {
108        return (options & HAVE_FILE_SIZE_HI) != 0;
109    }
110
111    public boolean hasMethodFlagsHi() {
112        return (options & HAVE_METHOD_FLAGS_HI) != 0;
113    }
114
115    public boolean hasSpecialFormats() {
116        return (options & HAVE_SPECIAL_FORMATS) != 0;
117    }
118
119    public boolean shouldDeflate() {
120        return (options & DEFLATE_HINT) != 0;
121    }
122}