[pal-cvs 2070] CVS-Update: libraries/portletoutputoptimizer/src/main/java/jp/sf/pal/pooptimizer committed by shinsuke

Back to archive index

Shinsuke SUGAYA shins****@users*****
2007年 2月 11日 (日) 07:41:13 JST


Update of /cvsroot/pal/libraries/portletoutputoptimizer/src/main/java/jp/sf/pal/pooptimizer
In directory sf-cvs:/tmp/cvs-serv629/src/main/java/jp/sf/pal/pooptimizer

Modified Files:
	OptimizerFilter.java 
Log Message:
replaced writer with stream

libraries/portletoutputoptimizer/src/main/java/jp/sf/pal/pooptimizer/OptimizerFilter.java 1.2 -> 1.3 (modified)
http://cvs.sourceforge.jp/cgi-bin/viewcvs.cgi/pal/libraries/portletoutputoptimizer/src/main/java/jp/sf/pal/pooptimizer/OptimizerFilter.java.diff?r1=1.2&r2=1.3

===================================================================
RCS file: libraries/portletoutputoptimizer/src/main/java/jp/sf/pal/pooptimizer/OptimizerFilter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- OptimizerFilter.java	2007/01/27 15:22:00	1.2
+++ OptimizerFilter.java	2007/02/10 22:41:13	1.3
@@ -15,7 +15,7 @@
  */
 package jp.sf.pal.pooptimizer;
 
-import java.io.BufferedWriter;
+import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -44,7 +44,11 @@
      */
     private static final Log log = LogFactory.getLog(OptimizerFilter.class);
 
-    private static final int BLOCK_SIZE = 4096;
+    private static final int DEFAULT_BLOCK_SIZE = 4096;
+
+    private static final String BLOCK_SIZE_KEY = "block-size";
+
+    private int blockSize = DEFAULT_BLOCK_SIZE;
 
     /*
      * (non-Javadoc)
@@ -61,7 +65,14 @@
      * @see org.apache.portals.bridges.portletfilter.PortletFilter#init(org.apache.portals.bridges.portletfilter.PortletFilterConfig)
      */
     public void init(PortletFilterConfig config) throws PortletException {
-
+        String blockSizeStr = config.getInitParameter(BLOCK_SIZE_KEY);
+        if (blockSizeStr != null) {
+            try {
+                blockSize = Integer.parseInt(blockSizeStr);
+            } catch (NumberFormatException e) {
+                log.warn("Could not parse " + blockSizeStr, e);
+            }
+        }
     }
 
     /*
@@ -112,36 +123,44 @@
             bufferedResponseStream.commit();
 
             InputStream reader = bufferedResponseStream.getInputStream();
-            BufferedWriter writer = new BufferedWriter(bufferedResponseStream
-                    .getWriter());
+            BufferedOutputStream outputStream = new BufferedOutputStream(
+                    bufferedResponseStream.getOutputStream());
 
-            byte[] bytes = new byte[BLOCK_SIZE];
+            byte[] bytes = new byte[blockSize];
             try {
                 int length = reader.read(bytes);
                 if (length != -1) {
-                    String str = new String(bytes, 0, length,
-                            bufferedResponseStream.getEncoding());
+                    String str = new String(bytes, 0, length, "ISO-8859-1");
                     int beginBodyIndex = str.indexOf("<body");
+                    if (beginBodyIndex == -1) {
+                        beginBodyIndex = str.indexOf("<BODY");
+                    }
                     if (beginBodyIndex >= 0) {
-                        str = str.substring(beginBodyIndex).replaceFirst(
-                                "<body[^>]*>", "");
+                        int beginBodyIndexEnd = str
+                                .indexOf(">", beginBodyIndex);
                         int endBodyIndex = str.indexOf("</body");
-                        if (endBodyIndex >= 0) {
-                            writer.write(str.substring(0, endBodyIndex));
+                        if (endBodyIndex == -1) {
+                            endBodyIndex = str.indexOf("</BODY");
+                        }
+                        if (endBodyIndex > beginBodyIndex) {
+                            outputStream.write(bytes, beginBodyIndexEnd + 1,
+                                    endBodyIndex - beginBodyIndexEnd - 1);
                         } else {
+                            outputStream.write(bytes, beginBodyIndexEnd + 1,
+                                    length - beginBodyIndexEnd - 1);
                             length = reader.read(bytes);
                             while (length != -1) {
-                                str = new String(bytes, 0, length,
-                                        bufferedResponseStream.getEncoding());
+                                str = new String(bytes, 0, length, "ISO-8859-1");
                                 endBodyIndex = str.indexOf("</body");
+                                if (endBodyIndex == -1) {
+                                    endBodyIndex = str.indexOf("</BODY");
+                                }
                                 if (endBodyIndex >= 0) {
-                                    writer
-                                            .write(str.substring(0,
-                                                    endBodyIndex));
+                                    outputStream.write(bytes, 0, endBodyIndex);
                                     break;
                                 } else {
-                                    if (length != 0) {
-                                        writer.write(str);
+                                    if (length > 0) {
+                                        outputStream.write(bytes, 0, length);
                                     }
                                     length = reader.read(bytes);
                                 }
@@ -149,20 +168,20 @@
                         }
                     } else {
                         if (length != -1) {
-                            writer.write(str);
+                            outputStream.write(bytes, 0, length);
                         }
                         length = reader.read(bytes);
                         while (length != -1) {
                             str = new String(bytes, 0, length,
                                     bufferedResponseStream.getEncoding());
-                            if (length != 0) {
-                                writer.write(str);
+                            if (length > 0) {
+                                outputStream.write(bytes, 0, length);
                             }
                             length = reader.read(bytes);
                         }
                     }
                 }
-                writer.flush();
+                outputStream.flush();
             } finally {
                 bytes = null;
             }



pal-cvs メーリングリストの案内
Back to archive index