commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1428942 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
Date Fri, 04 Jan 2013 15:59:39 GMT
Author: bodewig
Date: Fri Jan  4 15:59:38 2013
New Revision: 1428942

URL: http://svn.apache.org/viewvc?rev=1428942&view=rev
Log:
COMPRESS-212 use specified encoding when reading GNU long names in TarAIS

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1428942&r1=1428941&r2=1428942&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Fri Jan  4 15:59:38 2013
@@ -103,6 +103,10 @@ The <action> type attribute can be add,u
         ChangeSetPerformer has a new perform overload that uses a
         ZipFile instance as input.
       </action>
+      <action type="fix" date="2013-01-04" issue="COMPRESS-212">
+        TarArchiveInputStream ignored the encoding for GNU long name
+        entries.
+      </action>
     </release>
     <release version="1.4.1" date="2012-05-23"
              description="Release 1.4.1">

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java?rev=1428942&r1=1428941&r2=1428942&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
Fri Jan  4 15:59:38 2013
@@ -247,11 +247,11 @@ public class TarArchiveInputStream exten
 
         if (currEntry.isGNULongNameEntry()) {
             // read in the name
-            StringBuffer longName = new StringBuffer();
+            ByteArrayOutputStream longName = new ByteArrayOutputStream();
             byte[] buf = new byte[SMALL_BUFFER_SIZE];
             int length = 0;
             while ((length = read(buf)) >= 0) {
-                longName.append(new String(buf, 0, length)); // TODO default charset?
+                longName.write(buf, 0, length);
             }
             getNextEntry();
             if (currEntry == null) {
@@ -259,12 +259,19 @@ public class TarArchiveInputStream exten
                 // Malformed tar file - long entry name not followed by entry
                 return null;
             }
-            // remove trailing null terminator
-            if (longName.length() > 0
-                && longName.charAt(longName.length() - 1) == 0) {
-                longName.deleteCharAt(longName.length() - 1);
+            byte[] longNameData = longName.toByteArray();
+            // remove trailing null terminator(s)
+            length = longNameData.length;
+            while (length > 0 && longNameData[length - 1] == 0) {
+                --length;
             }
-            currEntry.setName(longName.toString());
+            if (length != longNameData.length) {
+                byte[] l = new byte[length];
+                System.arraycopy(longNameData, 0, l, 0, length);
+                longNameData = l;
+            }
+            
+            currEntry.setName(encoding.decode(longNameData));
         }
 
         if (currEntry.isPaxHeader()){ // Process Pax headers

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java?rev=1428942&r1=1428941&r2=1428942&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
Fri Jan  4 15:59:38 2013
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertTru
 import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -137,6 +138,31 @@ public class TarArchiveInputStreamTest {
         }
     }
 
+    @Test
+    public void shouldUseSpecifiedEncodingWhenReadingGNULongNames()
+        throws Exception {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        String encoding = CharsetNames.UTF_16;
+        String name = "1234567890123456789012345678901234567890123456789"
+            + "01234567890123456789012345678901234567890123456789"
+            + "01234567890\u00e4";
+        TarArchiveOutputStream tos =
+            new TarArchiveOutputStream(bos, encoding);
+        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
+        TarArchiveEntry t = new TarArchiveEntry(name);
+        t.setSize(1);
+        tos.putArchiveEntry(t);
+        tos.write(30);
+        tos.closeArchiveEntry();
+        tos.close();
+        byte[] data = bos.toByteArray();
+        ByteArrayInputStream bis = new ByteArrayInputStream(data);
+        TarArchiveInputStream tis =
+            new TarArchiveInputStream(bis, encoding);
+        t = tis.getNextTarEntry();
+        assertEquals(name, t.getName());
+    }
+
     private TarArchiveInputStream getTestStream(String name) {
         return new TarArchiveInputStream(
                 TarArchiveInputStreamTest.class.getResourceAsStream(name));



Mime
View raw message