~ubuntu-branches/ubuntu/saucy/libcommons-compress-java/saucy-proposed

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2011-08-07 01:56:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110807015615-cvbmhuv10g12fpz1
Tags: 1.2-1
* New upstream release
* Clean up Depends and Suggests fields.
* Switch to source format 3.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
18
package org.apache.commons.compress.archivers.zip;
 
19
 
 
20
/**
 
21
 * Parser/encoder for the "general purpose bit" field in ZIP's local
 
22
 * file and central directory headers.
 
23
 * @since Apache Commons Compress 1.1
 
24
 * @NotThreadSafe
 
25
 */
 
26
public final class GeneralPurposeBit {
 
27
    /**
 
28
     * Indicates that the file is encrypted.
 
29
     */
 
30
    private static final int ENCRYPTION_FLAG = 1 << 0;
 
31
 
 
32
    /**
 
33
     * Indicates that a data descriptor stored after the file contents
 
34
     * will hold CRC and size information.
 
35
     */
 
36
    private static final int DATA_DESCRIPTOR_FLAG = 1 << 3;
 
37
 
 
38
    /**
 
39
     * Indicates strong encryption.
 
40
     */
 
41
    private static final int STRONG_ENCRYPTION_FLAG = 1 << 6;
 
42
 
 
43
    /**
 
44
     * Indicates that filenames are written in utf-8.
 
45
     *
 
46
     * <p>The only reason this is public is that {@link
 
47
     * ZipArchiveOutputStream#EFS_FLAG} was public in Apache Commons
 
48
     * Compress 1.0 and we needed a substitute for it.</p>
 
49
     */
 
50
    public static final int UFT8_NAMES_FLAG = 1 << 11;
 
51
 
 
52
    private boolean languageEncodingFlag = false;
 
53
    private boolean dataDescriptorFlag = false;
 
54
    private boolean encryptionFlag = false;
 
55
    private boolean strongEncryptionFlag = false;
 
56
 
 
57
    public GeneralPurposeBit() {
 
58
    }
 
59
 
 
60
    /**
 
61
     * whether the current entry uses UTF8 for file name and comment.
 
62
     */
 
63
    public boolean usesUTF8ForNames() {
 
64
        return languageEncodingFlag;
 
65
    }
 
66
 
 
67
    /**
 
68
     * whether the current entry will use UTF8 for file name and comment.
 
69
     */
 
70
    public void useUTF8ForNames(boolean b) {
 
71
        languageEncodingFlag = b;
 
72
    }
 
73
 
 
74
    /**
 
75
     * whether the current entry uses the data descriptor to store CRC
 
76
     * and size information
 
77
     */
 
78
    public boolean usesDataDescriptor() {
 
79
        return dataDescriptorFlag;
 
80
    }
 
81
 
 
82
    /**
 
83
     * whether the current entry will use the data descriptor to store
 
84
     * CRC and size information
 
85
     */
 
86
    public void useDataDescriptor(boolean b) {
 
87
        dataDescriptorFlag = b;
 
88
    }
 
89
 
 
90
    /**
 
91
     * whether the current entry is encrypted
 
92
     */
 
93
    public boolean usesEncryption() {
 
94
        return encryptionFlag;
 
95
    }
 
96
 
 
97
    /**
 
98
     * whether the current entry will be encrypted
 
99
     */
 
100
    public void useEncryption(boolean b) {
 
101
        encryptionFlag = b;
 
102
    }
 
103
 
 
104
    /**
 
105
     * whether the current entry is encrypted using strong encryption
 
106
     */
 
107
    public boolean usesStrongEncryption() {
 
108
        return encryptionFlag && strongEncryptionFlag;
 
109
    }
 
110
 
 
111
    /**
 
112
     * whether the current entry will be encrypted  using strong encryption
 
113
     */
 
114
    public void useStrongEncryption(boolean b) {
 
115
        strongEncryptionFlag = b;
 
116
        if (b) {
 
117
            useEncryption(true);
 
118
        }
 
119
    }
 
120
 
 
121
    /**
 
122
     * Encodes the set bits in a form suitable for ZIP archives.
 
123
     */
 
124
    public byte[] encode() {
 
125
        return 
 
126
            ZipShort.getBytes((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0)
 
127
                              |
 
128
                              (languageEncodingFlag ? UFT8_NAMES_FLAG : 0)
 
129
                              |
 
130
                              (encryptionFlag ? ENCRYPTION_FLAG : 0)
 
131
                              |
 
132
                              (strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0)
 
133
                              );
 
134
    }
 
135
 
 
136
    /**
 
137
     * Parses the supported flags from the given archive data.
 
138
     * @param data local file header or a central directory entry.
 
139
     * @param offset offset at which the general purpose bit starts
 
140
     */
 
141
    public static GeneralPurposeBit parse(final byte[] data, final int offset) {
 
142
        final int generalPurposeFlag = ZipShort.getValue(data, offset);
 
143
        GeneralPurposeBit b = new GeneralPurposeBit();
 
144
        b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0);
 
145
        b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0);
 
146
        b.useStrongEncryption((generalPurposeFlag & STRONG_ENCRYPTION_FLAG)
 
147
                              != 0);
 
148
        b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0);
 
149
        return b;
 
150
    }
 
151
 
 
152
    public int hashCode() {
 
153
        return 3 * (7 * (13 * (17 * (encryptionFlag ? 1 : 0)
 
154
                               + (strongEncryptionFlag ? 1 : 0))
 
155
                         + (languageEncodingFlag ? 1 : 0))
 
156
                    + (dataDescriptorFlag ? 1 : 0));
 
157
    }
 
158
 
 
159
    public boolean equals(Object o) {
 
160
        if (!(o instanceof GeneralPurposeBit)) {
 
161
            return false;
 
162
        }
 
163
        GeneralPurposeBit g = (GeneralPurposeBit) o;
 
164
        return g.encryptionFlag == encryptionFlag
 
165
            && g.strongEncryptionFlag == strongEncryptionFlag
 
166
            && g.languageEncodingFlag == languageEncodingFlag
 
167
            && g.dataDescriptorFlag == dataDescriptorFlag;
 
168
    }
 
169
}