View Javadoc
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  
19  import org.apache.commons.compress.harmony.pack200.Pack200Exception;
20  
21  /**
22   * 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
23   * 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,
24   * these may be defined by macros that do bitflag manipulation instead.
25   */
26  public class SegmentOptions {
27  
28      private static final int DEFLATE_HINT = 1 << 5;
29  
30      private static final int HAVE_ALL_CODE_FLAGS = 1 << 2;
31  
32      private static final int HAVE_CLASS_FLAGS_HI = 1 << 9;
33  
34      // private static final int UNUSED_3 = 2^3;
35  
36      private static final int HAVE_CODE_FLAGS_HI = 1 << 10;
37  
38      private static final int HAVE_CP_NUMBERS = 1 << 1;
39  
40      private static final int HAVE_FIELD_FLAGS_HI = 1 << 10;
41  
42      private static final int HAVE_FILE_HEADERS = 1 << 4;
43  
44      private static final int HAVE_FILE_MODTIME = 1 << 6;
45  
46      private static final int HAVE_FILE_OPTIONS = 1 << 7;
47  
48      private static final int HAVE_FILE_SIZE_HI = 1 << 8;
49  
50      private static final int HAVE_METHOD_FLAGS_HI = 1 << 11;
51  
52      private static final int HAVE_SPECIAL_FORMATS = 1 << 0;
53  
54      /**
55       * The bit flags that are defined as unused by the specification; specifically, every bit above bit 13 and bit 3.
56       */
57      private static final int UNUSED = -1 << 13 | 1 << 3;
58  
59      private final int options;
60  
61      /**
62       * Creates a new segment options with the given integer value.
63       *
64       * @param options the integer value to use as the flags
65       * @throws Pack200Exception if an unused bit (bit 3 or bit 13+) is non-zero
66       */
67      public SegmentOptions(final int options) throws Pack200Exception {
68          if ((options & UNUSED) != 0) {
69              throw new Pack200Exception("Some unused flags are non-zero");
70          }
71          this.options = options;
72      }
73  
74      public boolean hasAllCodeFlags() {
75          return (options & HAVE_ALL_CODE_FLAGS) != 0;
76      }
77  
78      public boolean hasArchiveFileCounts() {
79          return (options & HAVE_FILE_HEADERS) != 0;
80      }
81  
82      public boolean hasClassFlagsHi() {
83          return (options & HAVE_CLASS_FLAGS_HI) != 0;
84      }
85  
86      public boolean hasCodeFlagsHi() {
87          return (options & HAVE_CODE_FLAGS_HI) != 0;
88      }
89  
90      public boolean hasCPNumberCounts() {
91          return (options & HAVE_CP_NUMBERS) != 0;
92      }
93  
94      public boolean hasFieldFlagsHi() {
95          return (options & HAVE_FIELD_FLAGS_HI) != 0;
96      }
97  
98      public boolean hasFileModtime() {
99          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 }