commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1618584 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/sevenz/ main/java/org/apache/commons/compress/compressors/xz/ test/java/org/apache/commons/compress/compressors/xz/
Date Mon, 18 Aug 2014 11:28:24 GMT
Author: bodewig
Date: Mon Aug 18 11:28:23 2014
New Revision: 1618584

URL: http://svn.apache.org/r1618584
Log:
COMPRESS-285 use a volatile enum rather than an AtomicInteger

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/xz/XZUtilsTestCase.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java?rev=1618584&r1=1618583&r2=1618584&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
Mon Aug 18 11:28:23 2014
@@ -852,6 +852,10 @@ public class SevenZFile implements Close
         file.seek(folderOffset);
         InputStream inputStreamStack = new BoundedRandomAccessFileInputStream(file,
                 archive.packSizes[firstPackStreamIndex]);
+        writeOut(inputStreamStack);
+        file.seek(folderOffset);
+        inputStreamStack = new BoundedRandomAccessFileInputStream(file,
+                archive.packSizes[firstPackStreamIndex]);
         LinkedList<SevenZMethodConfiguration> methods = new LinkedList<SevenZMethodConfiguration>();
         for (final Coder coder : folder.getOrderedCoders()) {
             if (coder.numInStreams != 1 || coder.numOutStreams != 1) {
@@ -870,6 +874,15 @@ public class SevenZFile implements Close
             return inputStreamStack;
         }
     }
+
+    private static void writeOut(InputStream in) throws IOException {
+        java.io.OutputStream out = new java.io.FileOutputStream("raw");
+        try {
+            IOUtils.copy(in, out);
+        } finally {
+            out.close();
+        }
+    }
     
     /**
      * Reads a byte of data.

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java?rev=1618584&r1=1618583&r2=1618584&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/xz/XZUtils.java
Mon Aug 18 11:28:23 2014
@@ -42,11 +42,11 @@ public class XZUtils {
         (byte) 0xFD, '7', 'z', 'X', 'Z', '\0'
     };
 
-    private static final int DONT_CACHE = 2;
-    private static final int CACHED_AVAILABLE = 1;
-    private static final int CACHED_UNAVAILABLE = 0;
+    static enum CachedAvailability {
+        DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE
+    }
 
-    private static final AtomicInteger cachedXZAvailability;
+    private static volatile CachedAvailability cachedXZAvailability;
 
     static {
         Map<String, String> uncompressSuffix = new HashMap<String, String>();
@@ -54,7 +54,7 @@ public class XZUtils {
         uncompressSuffix.put(".xz", "");
         uncompressSuffix.put("-xz", "");
         fileNameUtil = new FileNameUtil(uncompressSuffix, ".xz");
-        cachedXZAvailability = new AtomicInteger(DONT_CACHE);
+        cachedXZAvailability = CachedAvailability.DONT_CACHE;
         try {
             Class.forName("org.osgi.framework.BundleEvent");
         } catch (Exception ex) {
@@ -97,9 +97,9 @@ public class XZUtils {
      * @since 1.5
      */
     public static boolean isXZCompressionAvailable() {
-        final int cachedResult = cachedXZAvailability.get();
-        if (cachedResult != DONT_CACHE) {
-            return cachedResult == CACHED_AVAILABLE;
+        final CachedAvailability cachedResult = cachedXZAvailability;
+        if (cachedResult != CachedAvailability.DONT_CACHE) {
+            return cachedResult == CachedAvailability.CACHED_AVAILABLE;
         }
         return internalIsXZCompressionAvailable();
     }
@@ -165,15 +165,16 @@ public class XZUtils {
      */
     public static void setCacheXZAvailablity(boolean doCache) {
         if (!doCache) {
-            cachedXZAvailability.set(DONT_CACHE);
-        } else if (cachedXZAvailability.get() == DONT_CACHE) {
+            cachedXZAvailability = CachedAvailability.DONT_CACHE;
+        } else if (cachedXZAvailability == CachedAvailability.DONT_CACHE) {
             final boolean hasXz = internalIsXZCompressionAvailable();
-            cachedXZAvailability.set(hasXz ? CACHED_AVAILABLE : CACHED_UNAVAILABLE);
+            cachedXZAvailability = hasXz ? CachedAvailability.CACHED_AVAILABLE
+                : CachedAvailability.CACHED_UNAVAILABLE;
         }
     }
 
     // only exists to support unit tests
-    static int getCachedXZAvailability() {
-        return cachedXZAvailability.get();
+    static CachedAvailability getCachedXZAvailability() {
+        return cachedXZAvailability;
     }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/xz/XZUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/xz/XZUtilsTestCase.java?rev=1618584&r1=1618583&r2=1618584&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/xz/XZUtilsTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/xz/XZUtilsTestCase.java
Mon Aug 18 11:28:23 2014
@@ -75,14 +75,14 @@ public class XZUtilsTestCase extends Tes
     }
 
     public void testCachingIsEnabledByDefaultAndXZIsPresent() {
-        assertEquals(1, XZUtils.getCachedXZAvailability());
+        assertEquals(XZUtils.CachedAvailability.CACHED_AVAILABLE, XZUtils.getCachedXZAvailability());
         assertTrue(XZUtils.isXZCompressionAvailable());
     }
 
     public void testCanTurnOffCaching() {
         try {
             XZUtils.setCacheXZAvailablity(false);
-            assertEquals(2, XZUtils.getCachedXZAvailability());
+            assertEquals(XZUtils.CachedAvailability.DONT_CACHE, XZUtils.getCachedXZAvailability());
             assertTrue(XZUtils.isXZCompressionAvailable());
         } finally {
             XZUtils.setCacheXZAvailablity(true);
@@ -92,9 +92,9 @@ public class XZUtilsTestCase extends Tes
     public void testTurningOnCachingReEvaluatesAvailability() {
         try {
             XZUtils.setCacheXZAvailablity(false);
-            assertEquals(2, XZUtils.getCachedXZAvailability());
+            assertEquals(XZUtils.CachedAvailability.DONT_CACHE, XZUtils.getCachedXZAvailability());
             XZUtils.setCacheXZAvailablity(true);
-            assertEquals(1, XZUtils.getCachedXZAvailability());
+            assertEquals(XZUtils.CachedAvailability.CACHED_AVAILABLE, XZUtils.getCachedXZAvailability());
         } finally {
             XZUtils.setCacheXZAvailablity(true);
         }



Mime
View raw message