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