[Jython-checkins] jython: Removed extra decoding/encoding phases from bz2; eliminated extra whitespace;

alex.gronholm jython-checkins at python.org
Sat Jan 26 04:28:01 CET 2013


http://hg.python.org/jython/rev/b541cb7c32b2
changeset:   6956:b541cb7c32b2
user:        Alex Grönholm <alex.gronholm at nextday.fi>
date:        Thu Jan 17 02:01:46 2013 +0200
summary:
  Removed extra decoding/encoding phases from bz2; eliminated extra whitespace; replaced new PyString() with Py.EmptyString

files:
  src/org/python/modules/bz2/PyBZ2Compressor.java   |  35 ++-------
  src/org/python/modules/bz2/PyBZ2Decompressor.java |   9 +-
  src/org/python/modules/bz2/PyBZ2File.java         |  18 +----
  src/org/python/modules/bz2/bz2.java               |  37 +--------
  4 files changed, 18 insertions(+), 81 deletions(-)


diff --git a/src/org/python/modules/bz2/PyBZ2Compressor.java b/src/org/python/modules/bz2/PyBZ2Compressor.java
--- a/src/org/python/modules/bz2/PyBZ2Compressor.java
+++ b/src/org/python/modules/bz2/PyBZ2Compressor.java
@@ -3,7 +3,6 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.Formatter;
 
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
 import org.python.core.ArgParser;
@@ -11,6 +10,7 @@
 import org.python.core.PyObject;
 import org.python.core.PyString;
 import org.python.core.PyType;
+import org.python.core.util.StringUtil;
 import org.python.expose.ExposedMethod;
 import org.python.expose.ExposedNew;
 import org.python.expose.ExposedType;
@@ -45,12 +45,10 @@
         } catch (IOException e) {
             throw Py.IOError(e.getMessage());
         }
-
     }
 
     @ExposedMethod
     public PyString BZ2Compressor_compress(PyObject[] args, String[] kwds) {
-
         ArgParser ap = new ArgParser("compress", args, kwds,
                 new String[] { "data" }, 1);
 
@@ -69,35 +67,18 @@
     }
 
     private PyString readData() {
-
-        PyString returnData;
-        if (captureStream.hasData()) {
-            StringBuilder encodeBuf = new StringBuilder();
-            Formatter encodeFormatter = new Formatter(encodeBuf);
-            byte[] buf = captureStream.readData();
-            for (byte b : buf) {
-                if (b < 32 || b > 126) {
-                    encodeFormatter.format("\\x%02x", b);
-                } else {
-                    encodeFormatter.format("%c", b);
-                }
-            }
-
-            returnData = new PyString(encodeBuf.toString());
-            encodeFormatter.close();
-
-            captureStream.resetByteArray();
-
-        } else {
-            returnData = new PyString();
+        if (!captureStream.hasData()) {
+            return Py.EmptyString;
         }
-        return returnData;
+        
+        byte[] buf = captureStream.readData();
+        captureStream.resetByteArray();
+        return new PyString(StringUtil.fromBytes(buf));
     }
 
     @ExposedMethod
     public PyString BZ2Compressor_flush(PyObject[] args, String[] kwds) {
-
-        PyString finalData = new PyString();
+        PyString finalData = Py.EmptyString;
         try {
             compressStream.finish();
             compressStream.close();
diff --git a/src/org/python/modules/bz2/PyBZ2Decompressor.java b/src/org/python/modules/bz2/PyBZ2Decompressor.java
--- a/src/org/python/modules/bz2/PyBZ2Decompressor.java
+++ b/src/org/python/modules/bz2/PyBZ2Decompressor.java
@@ -20,7 +20,7 @@
 public class PyBZ2Decompressor extends PyObject {
 
     @ExposedGet
-    public PyString unused_data = new PyString();
+    public PyString unused_data = Py.EmptyString;
 
     private boolean eofReached = false;
     private BZip2CompressorInputStream decompressStream = null;
@@ -46,13 +46,12 @@
 
     @ExposedMethod
     final PyString BZ2Decompressor_decompress(PyObject[] args, String[] kwds) {
-
         ArgParser ap = new ArgParser("compress", args, kwds,
                 new String[] { "data" }, 1);
 
         PyString data = (PyString) ap.getPyObject(0);
 
-        PyString returnData = new PyString();
+        PyString returnData = Py.EmptyString;
 
         if (eofReached) {
             throw Py.EOFError("Data stream EOF reached");
@@ -86,7 +85,7 @@
         try {
             decompressStream = new BZip2CompressorInputStream(compressedData);
         } catch (IOException e) {
-            return new PyString();
+            return Py.EmptyString;
         }
 
         ByteArrayOutputStream databuf = new ByteArrayOutputStream();
@@ -105,7 +104,7 @@
             }
             eofReached = true;
         } catch (IOException e) {
-            return new PyString();
+            return Py.EmptyString;
         }
 
         return returnData;
diff --git a/src/org/python/modules/bz2/PyBZ2File.java b/src/org/python/modules/bz2/PyBZ2File.java
--- a/src/org/python/modules/bz2/PyBZ2File.java
+++ b/src/org/python/modules/bz2/PyBZ2File.java
@@ -96,7 +96,6 @@
             }
 
             if (mode.contains("w")) {
-
                 File f = new File(fileName);
                 if (!f.exists()) {
                     f.createNewFile();
@@ -104,9 +103,7 @@
 
                 writeStream = new BZip2CompressorOutputStream(
                         new FileOutputStream(fileName), compresslevel);
-
             } else {
-
                 FileInputStream fin = new FileInputStream(fileName);
                 BufferedInputStream bin = new BufferedInputStream(fin);
                 BZip2CompressorInputStream bZin = new BZip2CompressorInputStream(
@@ -126,7 +123,6 @@
                 bin.close();
                 fin.close();
             }
-
         } catch (IOException e) {
             throw Py.IOError("File " + fileName + " not found,");
         }
@@ -139,7 +135,6 @@
 
     @ExposedMethod
     public void BZ2File_close() {
-
         fileData = null;
 
         if (writeStream != null) {
@@ -154,7 +149,6 @@
     }
 
     private void BZ2File_flush() {
-
         if (writeStream != null) {
             try {
                 writeStream.flush();
@@ -176,7 +170,6 @@
 
     @ExposedMethod
     public PyObject BZ2File_read(PyObject[] args, String[] kwds) {
-
         checkInIterMode();
 
         ArgParser ap = new ArgParser("read", args, kwds,
@@ -190,7 +183,6 @@
     }
 
     private byte[] _BZ2File_read(int size) {
-
         byte[] buf = null;
         if (size == 0) {
             return new byte[0];
@@ -243,7 +235,6 @@
 
     @ExposedMethod
     public PyString BZ2File_readline(PyObject[] args, String[] kwds) {
-
         checkInIterMode();
 
         ArgParser ap = new ArgParser("read", args, kwds,
@@ -288,7 +279,6 @@
     }
 
     private void addNewlineMarker(String newline) {
-
         if (newlines == null) {
             newlines = new PyString(newline);
         } else {
@@ -307,7 +297,6 @@
 
     @ExposedMethod
     public PyList BZ2File_readlines(PyObject[] args, String[] kwds) {
-
         checkInIterMode();
 
         // make sure file data valid
@@ -318,7 +307,7 @@
         PyList lineList = new PyList();
 
         PyString line = null;
-        while (!(line = BZ2File_readline(args, kwds)).equals(new PyString())) {
+        while (!(line = BZ2File_readline(args, kwds)).equals(Py.EmptyString)) {
             lineList.add(line);
         }
 
@@ -382,7 +371,6 @@
 
     @ExposedMethod
     public void BZ2File_write(PyObject[] args, String[] kwds) {
-
         checkFileWritable();
 
         ArgParser ap = new ArgParser("write", args, kwds,
@@ -406,7 +394,6 @@
 
     @ExposedMethod
     public void BZ2File_writelines(PyObject[] args, String[] kwds) {
-
         checkFileWritable();
 
         ArgParser ap = new ArgParser("writelines", args, kwds,
@@ -442,10 +429,9 @@
 
         @Override
         public PyObject __iternext__() {
-
             PyString s = BZ2File_readline(new PyObject[0], new String[0]);
 
-            if (s.equals(new PyString())) {
+            if (s.equals(Py.EmptyString)) {
                 return null;
             } else {
                 return s;
diff --git a/src/org/python/modules/bz2/bz2.java b/src/org/python/modules/bz2/bz2.java
--- a/src/org/python/modules/bz2/bz2.java
+++ b/src/org/python/modules/bz2/bz2.java
@@ -3,7 +3,6 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.util.Formatter;
 
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
@@ -33,7 +32,6 @@
     }
 
     public static PyString compress(PyString data, int compresslevel) {
-
         PyString returnData = null;
 
         try {
@@ -45,20 +43,8 @@
             bzbuf.finish();
             bzbuf.close();
 
-            StringBuilder encodeBuf = new StringBuilder();
-            Formatter encodeFormatter = new Formatter(encodeBuf);
-
-            byte[] buf = compressedArray.toByteArray();
-            for (byte b : buf) {
-                if (b < 32 || b > 126) {
-                    encodeFormatter.format("\\x%02x", b);
-                } else {
-                    encodeFormatter.format("%c", b);
-                }
-            }
+            returnData = new PyString(compressedArray.toString("iso-8859-1"));
             compressedArray.close();
-
-            returnData = new PyString(encodeBuf.toString());
         } catch (IOException e) {
             throw Py.IOError(e.getMessage());
         }
@@ -67,28 +53,14 @@
     }
 
     public static PyString decompress(PyString data) {
-
         PyString returnString = null;
 
         if (data.toString().equals("")) {
-            return new PyString();
+            return Py.EmptyString;
         }
         try {
-            ByteArrayOutputStream decodedStream = new ByteArrayOutputStream();
-            final byte[] buf = data.toBytes();
-            for (int i = 0; i < buf.length; i++) {
-                if (((char) buf[i] == '\\') && ((char) buf[i + 1] == 'x')) {
-                    int decodedByte = ((Character.digit((char) buf[i + 2], 16) << 4) + Character
-                            .digit((char) buf[i + 3], 16));
-                    decodedStream.write(decodedByte);
-                    i += 3;
-                } else {
-                    decodedStream.write(buf[i]);
-                }
-            }
-
             ByteArrayInputStream inputArray = new ByteArrayInputStream(
-                    decodedStream.toByteArray());
+                    data.toBytes());
             BZip2CompressorInputStream bzbuf = new BZip2CompressorInputStream(
                     inputArray);
 
@@ -100,12 +72,11 @@
                 outputArray.write(buffer, 0, n);
             }
 
-            returnString = new PyString(new String(outputArray.toByteArray()));
+            returnString = new PyString(outputArray.toString("iso-8859-1"));
 
             outputArray.close();
             bzbuf.close();
             inputArray.close();
-
         } catch (IOException e) {
             throw Py.ValueError(e.getMessage());
         }

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list