(no subject)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

(no subject)

Marko Reiprecht
Hi,

du kannst hier auch auf den BufferedReader verzichten, wenn du selber ein char[] buffer verwendest.
Dann bedeutet der BufferedReader nämlich unnötigen overhead.

Interessant das so komischer code noch im textural payload steckte....

Gruß Marko

-----Original Message-----
From: Security-commits [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Donnerstag, 21. August 2014 13:03
To: [hidden email]
Subject: [Security-commits] 52north.org SVN: security-52n:[3785] 52n-security-api/trunk/ 52n-security-core/src

Revision: 3785
          https://svn.52north.org/cgi-bin/viewvc.cgi?root=security&rev=3785&view=rev
Author:   JanDrewnak
Date:     2014-08-21 13:02:38 +0200 (Thu, 21 Aug 2014)
Log Message:
-----------
-- handle line breaks correctly in TextualPayload

Modified Paths:
--------------
    52n-security-api/trunk/52n-security-core/src/main/java/org/n52/security/common/artifact/TextualPayload.java

Added Paths:
-----------
    52n-security-api/trunk/52n-security-core/src/test/java/org/n52/security/common/artifact/TextualPayloadTest.java

Modified: 52n-security-api/trunk/52n-security-core/src/main/java/org/n52/security/common/artifact/TextualPayload.java
===================================================================
--- 52n-security-api/trunk/52n-security-core/src/main/java/org/n52/security/common/artifact/TextualPayload.java 2014-08-21 09:54:35 UTC (rev 3784)
+++ 52n-security-api/trunk/52n-security-core/src/main/java/org/n52/security/common/artifact/TextualPayload.java 2014-08-21 11:02:38 UTC (rev 3785)
@@ -37,7 +37,7 @@
 public class TextualPayload implements Payload {
     private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(TextualPayload.class);
 
-    private String m_payload;
+    private String m_payload = "";
 
     private String m_streamCharset;
 
@@ -52,24 +52,26 @@
     }
 
     private void init(final Reader pReader) throws IOException {
-        BufferedReader lReader = new BufferedReader(pReader, 256);
-        StringBuffer lBuffer = new StringBuffer(256);
-        String lLine = null;
+        BufferedReader lReader = new BufferedReader(pReader, 4096);
+        StringBuffer lBuffer = new StringBuffer(4096);
+        char[] buffer = new char[4096];
+
         try {
-            while ((lLine = lReader.readLine()) != null) {
-                lBuffer.append(lLine).append("\n");
+            int readChars;
+            while ((readChars = lReader.read(buffer)) != -1) {
+                lBuffer.append(buffer, 0, readChars);
             }
+
         } finally {
             lReader.close();
         }
         if (lBuffer.length() > 0) {
-            m_payload = lBuffer.substring(0);
+            m_payload = lBuffer.toString();
         }
     }
 
     public InputStream getAsStream() {
         try {
-            // return new ByteArrayInputStream(mPayload.getBytes(mStreamCharset!=null? mStreamCharset:"UTF-8"));
             return new StringInputStream(m_payload,
                     (m_streamCharset != null && !m_streamCharset.equals("")) ? m_streamCharset : "UTF-8");
         } catch (UnsupportedEncodingException e) {

Added: 52n-security-api/trunk/52n-security-core/src/test/java/org/n52/security/common/artifact/TextualPayloadTest.java
===================================================================
--- 52n-security-api/trunk/52n-security-core/src/test/java/org/n52/security/common/artifact/TextualPayloadTest.java                        (rev 0)
+++ 52n-security-api/trunk/52n-security-core/src/test/java/org/n52/security/common/artifact/TextualPayloadTest.java 2014-08-21 11:02:38 UTC (rev 3785)
@@ -0,0 +1,59 @@
+package org.n52.security.common.artifact;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.junit.Test;
+
+
+public class TextualPayloadTest {
+
+
+
+    private static final String LINE1 = "line1";
+
+    @Test
+    public void lineEndHandling() throws Exception {
+
+        String content = LINE1;
+        InputStream contentStream = new ByteArrayInputStream(content.getBytes("UTF-8"));
+        TextualPayload payload = new TextualPayload(contentStream, "UTF-8");
+        String payloadContent = payload.toString();
+
+        assertEquals(content, payloadContent);
+    }
+
+    @Test
+    public void lineEndHandlingEmptyLine() throws Exception {
+
+        String content = "";
+        InputStream contentStream = new ByteArrayInputStream(content.getBytes("UTF-8"));
+        TextualPayload payload = new TextualPayload(contentStream, "UTF-8");
+        String payloadContent = payload.toString();
+
+        assertEquals(content, payloadContent);
+    }
+
+    @Test
+    public void lineEndHandlingWithTrailingLinebreak() throws Exception {
+
+        String content = LINE1 + "\n";
+        InputStream contentStream = new ByteArrayInputStream(content.getBytes("UTF-8"));
+        TextualPayload payload = new TextualPayload(contentStream, "UTF-8");
+        String payloadContent = payload.toString();
+        assertEquals(content, payloadContent);
+    }
+
+    @Test
+    public void lineEndHandlingWithTrailingEmptyLine() throws Exception {
+
+        String content = LINE1 + "\n\n";
+        InputStream contentStream = new ByteArrayInputStream(content.getBytes("UTF-8"));
+        TextualPayload payload = new TextualPayload(contentStream, "UTF-8");
+        String payloadContent = payload.toString();
+        assertEquals(content, payloadContent);
+    }
+
+}

_______________________________________________
Security-commits mailing list
[hidden email]
http://list.52north.org/mailman/listinfo/security-commits
_______________________________________________
Security mailing list
[hidden email]
http://list.52north.org/mailman/listinfo/security
http://security.forum.52north.org
Loading...