Author: bodewig
Date: Fri Feb 19 09:56:37 2010
New Revision: 911765
URL: http://svn.apache.org/viewvc?rev=911765&view=rev
Log:
extract general purpose bit logic
Added:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
(with props)
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
(with props)
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
Added:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java?rev=911765&view=auto
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
(added)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
Fri Feb 19 09:56:37 2010
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.zip;
+
+/**
+ * Parser/encoder for the "general purpose bit" field in ZIP's local
+ * file and cebtral directory headers.
+ * @since Apache Commons Compress 1.1
+ */
+public class GeneralPurposeBit {
+ /**
+ * Indicates that a data descriptor stored after the file contents
+ * will hold CRC and size information.
+ */
+ private static final int DATA_DESCRIPTOR_FLAG = 1 << 3;
+
+ /**
+ * Indicates that filenames are written in utf-8.
+ *
+ * <p>The only reason this is public is that {...@link
+ * ZipArchiveOutputStream#EFS_FLAG} was public in Apache Commons
+ * Compress 1.0 and we needed a substitute for it.</p>
+ */
+ public static final int UFT8_NAMES_FLAG = 1 << 11;
+
+ private boolean languageEncodingFlag = false;
+ private boolean dataDescriptorFlag = false;
+
+ public GeneralPurposeBit() {
+ }
+
+ /**
+ * whether the current entry uses UTF8 for file name and comment.
+ */
+ public boolean usesUTF8ForNames() {
+ return languageEncodingFlag;
+ }
+
+ /**
+ * whether the current entry will use UTF8 for file name and comment.
+ */
+ public void useUTF8ForNames(boolean b) {
+ languageEncodingFlag = b;
+ }
+
+ /**
+ * whether the current entry uses the data descriptor to store CRC
+ * and size information
+ */
+ public boolean usesDataDescriptor() {
+ return dataDescriptorFlag;
+ }
+
+ /**
+ * whether the current entry will use the data descriptor to store
+ * CRC and size information
+ */
+ public void useDataDescriptor(boolean b) {
+ dataDescriptorFlag = b;
+ }
+
+ /**
+ * Encodes the set bits in a form suitable for ZIP archives.
+ */
+ public byte[] encode() {
+ return
+ ZipShort.getBytes((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0)
+ |
+ (languageEncodingFlag ? UFT8_NAMES_FLAG : 0)
+ );
+ }
+
+ /**
+ * Parses the supported flags from the given archive data.
+ * @param data local file header or a central directory entry.
+ * @param offset offset at which the general purpose bit starts
+ */
+ public static GeneralPurposeBit parse(final byte[] data, final int offset)
{
+ final int generalPurposeFlag = ZipShort.getValue(data, offset);
+ GeneralPurposeBit b = new GeneralPurposeBit();
+ b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0);
+ b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0);
+ return b;
+ }
+}
Propchange:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=911765&r1=911764&r2=911765&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
Fri Feb 19 09:56:37 2010
@@ -134,12 +134,11 @@
current.setPlatform((versionMadeBy >> ZipFile.BYTE_SHIFT)
& ZipFile.NIBLET_MASK);
- final int generalPurposeFlag = ZipShort.getValue(lfh, off);
- final boolean hasUTF8Flag =
- (generalPurposeFlag & ZipArchiveOutputStream.UFT8_NAMES_FLAG) != 0;
+ final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(lfh, off);
+ final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
final ZipEncoding entryEncoding =
hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
- hasDataDescriptor = (generalPurposeFlag & 8) != 0;
+ hasDataDescriptor = gpFlag.usesDataDescriptor();
off += SHORT;
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java?rev=911765&r1=911764&r2=911765&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
Fri Feb 19 09:56:37 2010
@@ -97,15 +97,9 @@
/**
* General purpose flag, which indicates that filenames are
* written in utf-8.
+ * @deprecated use {...@link GeneralPurposeBit#UFT8_NAMES_FLAG} instead
*/
- public static final int UFT8_NAMES_FLAG = 1 << 11;
-
- /**
- * General purpose flag, which indicates that filenames are
- * written in utf-8.
- * @deprecated use {...@link #UFT8_NAMES_FLAG} instead
- */
- public static final int EFS_FLAG = UFT8_NAMES_FLAG;
+ public static final int EFS_FLAG = GeneralPurposeBit.UFT8_NAMES_FLAG;
/**
* Current entry.
@@ -898,20 +892,20 @@
// CheckStyle:MagicNumber OFF
int versionNeededToExtract = 10;
- int generalPurposeFlag = (useUTF8Flag || utfFallback) ?
UFT8_NAMES_FLAG : 0;
+ GeneralPurposeBit b = new GeneralPurposeBit();
+ b.useUTF8ForNames(useUTF8Flag || utfFallback);
if (zipMethod == DEFLATED && raf == null) {
// requires version 2 as we are going to store length info
// in the data descriptor
versionNeededToExtract = 20;
- // bit3 set to signal, we use a data descriptor
- generalPurposeFlag |= 8;
+ b.useDataDescriptor(true);
}
// CheckStyle:MagicNumber ON
// version needed to extract
writeOut(ZipShort.getBytes(versionNeededToExtract));
// general purpose bit flag
- writeOut(ZipShort.getBytes(generalPurposeFlag));
+ writeOut(b.encode());
}
/**
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=911765&r1=911764&r2=911765&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
Fri Feb 19 09:56:37 2010
@@ -329,9 +329,8 @@
off += SHORT; // skip version info
- final int generalPurposeFlag = ZipShort.getValue(cfh, off);
- final boolean hasUTF8Flag =
- (generalPurposeFlag & ZipArchiveOutputStream.UFT8_NAMES_FLAG)
!= 0;
+ final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(cfh, off);
+ final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
final ZipEncoding entryEncoding =
hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING :
zipEncoding;
Added:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java?rev=911765&view=auto
==============================================================================
---
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
(added)
+++
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
Fri Feb 19 09:56:37 2010
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.commons.compress.archivers.zip;
+
+import java.util.Arrays;
+import junit.framework.TestCase;
+
+public class GeneralPurposeBitTest extends TestCase {
+
+ public void testDefaults() {
+ assertFalse(new GeneralPurposeBit().usesDataDescriptor());
+ assertFalse(new GeneralPurposeBit().usesUTF8ForNames());
+ byte[] b = new byte[2];
+ assertTrue(Arrays.equals(b, new GeneralPurposeBit().encode()));
+ }
+
+ public void testParseEdgeCases() {
+ assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
+ .usesDataDescriptor());
+ assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
+ .usesUTF8ForNames());
+ assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
+ 0)
+ .usesDataDescriptor());
+ assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
+ 0)
+ .usesUTF8ForNames());
+ }
+
+ public void testDataDescriptor() {
+ byte[] flags = new byte[] {(byte) 8, (byte) 0};
+ assertTrue(GeneralPurposeBit.parse(flags, 0).usesDataDescriptor());
+ GeneralPurposeBit b = new GeneralPurposeBit();
+ b.useDataDescriptor(true);
+ assertTrue(Arrays.equals(flags, b.encode()));
+ }
+
+ public void testLanguageEncodingFlag() {
+ byte[] flags = new byte[] {(byte) 0, (byte) 8};
+ assertTrue(GeneralPurposeBit.parse(flags, 0).usesUTF8ForNames());
+ GeneralPurposeBit b = new GeneralPurposeBit();
+ b.useUTF8ForNames(true);
+ assertTrue(Arrays.equals(flags, b.encode()));
+ }
+}
\ No newline at end of file
Propchange:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
------------------------------------------------------------------------------
svn:eol-style = native