    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.harmony.unpack200;
    020
    021import org.apache.commons.compress.harmony.pack200.Pack200Exception;
    022
    023/**
    024 * 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
    025 * 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,
    026 * these may be defined by macros that do bitflag manipulation instead.
    027 */
    028public class SegmentOptions {
    029
    030    private static final int DEFLATE_HINT = 1 << 5;
    031
    032    private static final int HAVE_ALL_CODE_FLAGS = 1 << 2;
    033
    034    private static final int HAVE_CLASS_FLAGS_HI = 1 << 9;
    035
    036    // private static final int UNUSED_3 = 2^3;
    037
    038    private static final int HAVE_CODE_FLAGS_HI = 1 << 10;
    039
    040    private static final int HAVE_CP_NUMBERS = 1 << 1;
    041
    042    private static final int HAVE_FIELD_FLAGS_HI = 1 << 10;
    043
    044    private static final int HAVE_FILE_HEADERS = 1 << 4;
    045
    046    private static final int HAVE_FILE_MODTIME = 1 << 6;
    047
    048    private static final int HAVE_FILE_OPTIONS = 1 << 7;
    049
    050    private static final int HAVE_FILE_SIZE_HI = 1 << 8;
    051
    052    private static final int HAVE_METHOD_FLAGS_HI = 1 << 11;
    053
    054    private static final int HAVE_SPECIAL_FORMATS = 1 << 0;
    055
    056    /**
    057     * The bit flags that are defined as unused by the specification; specifically, every bit above bit 13 and bit 3.
    058     */
    059    private static final int UNUSED = -1 << 13 | 1 << 3;
    060
    061    private final int options;
    062
    063    /**
    064     * Creates a new segment options with the given integer value.
    065     *
    066     * @param options the integer value to use as the flags
    067     * @throws Pack200Exception if an unused bit (bit 3 or bit 13+) is non-zero
    068     */
    069    public SegmentOptions(final int options) throws Pack200Exception {
    070        if ((options & UNUSED) != 0) {
    071            throw new Pack200Exception("Some unused flags are non-zero");
    072        }
    073        this.options = options;
    074    }
    075
    076    public boolean hasAllCodeFlags() {
    077        return (options & HAVE_ALL_CODE_FLAGS) != 0;
    078    }
    079
    080    public boolean hasArchiveFileCounts() {
    081        return (options & HAVE_FILE_HEADERS) != 0;
    082    }
    083
    084    public boolean hasClassFlagsHi() {
    085        return (options & HAVE_CLASS_FLAGS_HI) != 0;
    086    }
    087
    088    public boolean hasCodeFlagsHi() {
    089        return (options & HAVE_CODE_FLAGS_HI) != 0;
    090    }
    091
    092    public boolean hasCPNumberCounts() {
    093        return (options & HAVE_CP_NUMBERS) != 0;
    094    }
    095
    096    public boolean hasFieldFlagsHi() {
    097        return (options & HAVE_FIELD_FLAGS_HI) != 0;
    098    }
    099
    100    public boolean hasFileModtime() {
    101        return (options & HAVE_FILE_MODTIME) != 0;
    102    }
    103
    104    public boolean hasFileOptions() {
    105        return (options & HAVE_FILE_OPTIONS) != 0;
    106    }
    107
    108    public boolean hasFileSizeHi() {
    109        return (options & HAVE_FILE_SIZE_HI) != 0;
    110    }
    111
    112    public boolean hasMethodFlagsHi() {
    113        return (options & HAVE_METHOD_FLAGS_HI) != 0;
    114    }
    115
    116    public boolean hasSpecialFormats() {
    117        return (options & HAVE_SPECIAL_FORMATS) != 0;
    118    }
    119
    120    public boolean shouldDeflate() {
    121        return (options & DEFLATE_HINT) != 0;
    122    }
    123}