View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.unpack200;
20  
21  import org.apache.commons.compress.harmony.pack200.Pack200Exception;
22  
23  /**
24   * 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
25   * 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,
26   * these may be defined by macros that do bitflag manipulation instead.
27   */
28  public class SegmentOptions {
29  
30      private static final int DEFLATE_HINT = 1 << 5;
31  
32      private static final int HAVE_ALL_CODE_FLAGS = 1 << 2;
33  
34      private static final int HAVE_CLASS_FLAGS_HI = 1 << 9;
35  
36      // private static final int UNUSED_3 = 2^3;
37  
38      private static final int HAVE_CODE_FLAGS_HI = 1 << 10;
39  
40      private static final int HAVE_CP_NUMBERS = 1 << 1;
41  
42      private static final int HAVE_FIELD_FLAGS_HI = 1 << 10;
43  
44      private static final int HAVE_FILE_HEADERS = 1 << 4;
45  
46      private static final int HAVE_FILE_MODTIME = 1 << 6;
47  
48      private static final int HAVE_FILE_OPTIONS = 1 << 7;
49  
50      private static final int HAVE_FILE_SIZE_HI = 1 << 8;
51  
52      private static final int HAVE_METHOD_FLAGS_HI = 1 << 11;
53  
54      private static final int HAVE_SPECIAL_FORMATS = 1 << 0;
55  
56      /**
57       * The bit flags that are defined as unused by the specification; specifically, every bit above bit 13 and bit 3.
58       */
59      private static final int UNUSED = -1 << 13 | 1 << 3;
60  
61      private final int options;
62  
63      /**
64       * Creates a new segment options with the given integer value.
65       *
66       * @param options the integer value to use as the flags
67       * @throws Pack200Exception if an unused bit (bit 3 or bit 13+) is non-zero
68       */
69      public SegmentOptions(final int options) throws Pack200Exception {
70          if ((options & UNUSED) != 0) {
71              throw new Pack200Exception("Some unused flags are non-zero");
72          }
73          this.options = options;
74      }
75  
76      public boolean hasAllCodeFlags() {
77          return (options & HAVE_ALL_CODE_FLAGS) != 0;
78      }
79  
80      public boolean hasArchiveFileCounts() {
81          return (options & HAVE_FILE_HEADERS) != 0;
82      }
83  
84      public boolean hasClassFlagsHi() {
85          return (options & HAVE_CLASS_FLAGS_HI) != 0;
86      }
87  
88      public boolean hasCodeFlagsHi() {
89          return (options & HAVE_CODE_FLAGS_HI) != 0;
90      }
91  
92      public boolean hasCPNumberCounts() {
93          return (options & HAVE_CP_NUMBERS) != 0;
94      }
95  
96      public boolean hasFieldFlagsHi() {
97          return (options & HAVE_FIELD_FLAGS_HI) != 0;
98      }
99  
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 }