SegmentOptions.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.harmony.unpack200;

  18. import org.apache.commons.compress.harmony.pack200.Pack200Exception;

  19. /**
  20.  * 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
  21.  * 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,
  22.  * these may be defined by macros that do bitflag manipulation instead.
  23.  */
  24. public class SegmentOptions {

  25.     private static final int DEFLATE_HINT = 1 << 5;

  26.     private static final int HAVE_ALL_CODE_FLAGS = 1 << 2;

  27.     private static final int HAVE_CLASS_FLAGS_HI = 1 << 9;

  28.     // private static final int UNUSED_3 = 2^3;

  29.     private static final int HAVE_CODE_FLAGS_HI = 1 << 10;

  30.     private static final int HAVE_CP_NUMBERS = 1 << 1;

  31.     private static final int HAVE_FIELD_FLAGS_HI = 1 << 10;

  32.     private static final int HAVE_FILE_HEADERS = 1 << 4;

  33.     private static final int HAVE_FILE_MODTIME = 1 << 6;

  34.     private static final int HAVE_FILE_OPTIONS = 1 << 7;

  35.     private static final int HAVE_FILE_SIZE_HI = 1 << 8;

  36.     private static final int HAVE_METHOD_FLAGS_HI = 1 << 11;

  37.     private static final int HAVE_SPECIAL_FORMATS = 1 << 0;

  38.     /**
  39.      * The bit flags that are defined as unused by the specification; specifically, every bit above bit 13 and bit 3.
  40.      */
  41.     private static final int UNUSED = -1 << 13 | 1 << 3;

  42.     private final int options;

  43.     /**
  44.      * Creates a new segment options with the given integer value.
  45.      *
  46.      * @param options the integer value to use as the flags
  47.      * @throws Pack200Exception if an unused bit (bit 3 or bit 13+) is non-zero
  48.      */
  49.     public SegmentOptions(final int options) throws Pack200Exception {
  50.         if ((options & UNUSED) != 0) {
  51.             throw new Pack200Exception("Some unused flags are non-zero");
  52.         }
  53.         this.options = options;
  54.     }

  55.     public boolean hasAllCodeFlags() {
  56.         return (options & HAVE_ALL_CODE_FLAGS) != 0;
  57.     }

  58.     public boolean hasArchiveFileCounts() {
  59.         return (options & HAVE_FILE_HEADERS) != 0;
  60.     }

  61.     public boolean hasClassFlagsHi() {
  62.         return (options & HAVE_CLASS_FLAGS_HI) != 0;
  63.     }

  64.     public boolean hasCodeFlagsHi() {
  65.         return (options & HAVE_CODE_FLAGS_HI) != 0;
  66.     }

  67.     public boolean hasCPNumberCounts() {
  68.         return (options & HAVE_CP_NUMBERS) != 0;
  69.     }

  70.     public boolean hasFieldFlagsHi() {
  71.         return (options & HAVE_FIELD_FLAGS_HI) != 0;
  72.     }

  73.     public boolean hasFileModtime() {
  74.         return (options & HAVE_FILE_MODTIME) != 0;
  75.     }

  76.     public boolean hasFileOptions() {
  77.         return (options & HAVE_FILE_OPTIONS) != 0;
  78.     }

  79.     public boolean hasFileSizeHi() {
  80.         return (options & HAVE_FILE_SIZE_HI) != 0;
  81.     }

  82.     public boolean hasMethodFlagsHi() {
  83.         return (options & HAVE_METHOD_FLAGS_HI) != 0;
  84.     }

  85.     public boolean hasSpecialFormats() {
  86.         return (options & HAVE_SPECIAL_FORMATS) != 0;
  87.     }

  88.     public boolean shouldDeflate() {
  89.         return (options & DEFLATE_HINT) != 0;
  90.     }
  91. }