You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/04/21 02:56:39 UTC

[james-project] 03/19: [Refactoring] remove FastByteArrayOutputStream use from ImapRequestLineReader

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 620662e73945e8a52cea713f5c8e3f4007885a46
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Wed Apr 15 22:33:40 2020 +0200

    [Refactoring] remove FastByteArrayOutputStream use from ImapRequestLineReader
    
    	The only use was in a context where the size is already known
    	and the buffer can be allocated to the right size from the start
---
 .../james/imap/decode/ImapRequestLineReader.java   | 24 +++++++++++-----------
 .../imap/decode/parser/AppendCommandParser.java    |  4 +++-
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java
index ac0e7d9..dabe917 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestLineReader.java
@@ -35,6 +35,7 @@ import java.util.List;
 import javax.mail.Flags;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.james.imap.api.ImapConstants;
 import org.apache.james.imap.api.Tag;
 import org.apache.james.imap.api.display.HumanReadableText;
@@ -44,7 +45,6 @@ import org.apache.james.imap.api.message.UidRange;
 import org.apache.james.imap.api.message.request.DayMonthYear;
 import org.apache.james.imap.api.process.ImapSession;
 import org.apache.james.imap.api.process.SearchResUtil;
-import org.apache.james.imap.utils.FastByteArrayOutputStream;
 import org.apache.james.mailbox.MessageUid;
 
 /**
@@ -369,22 +369,22 @@ public abstract class ImapRequestLineReader {
         if (charset == null) {
             return consumeLiteral(US_ASCII);
         } else {
-            try (FastByteArrayOutputStream out = new FastByteArrayOutputStream();
-                 InputStream in = consumeLiteral(false)) {
-                IOUtils.copy(in, out, 2048);
-                byte[] bytes = out.toByteArray();
-                ByteBuffer buffer = ByteBuffer.wrap(bytes);
+            ImmutablePair<Integer, InputStream> literal = consumeLiteral(false);
+            try (InputStream in = literal.right) {
+                Integer size = literal.left;
+                byte[] data = IOUtils.readFully(in, size);
+                ByteBuffer buffer = ByteBuffer.wrap(data);
                 return decode(charset, buffer);
             } catch (IOException e) {
                 throw new DecodingException(HumanReadableText.BAD_IO_ENCODING, "Bad character encoding", e);
             }
-            // ignore on close
-            // ignore on close
-
         }
     }
 
-    public InputStream consumeLiteral(boolean extraCRLF) throws DecodingException {
+    /**
+     * @return the literal data and its expected size
+     */
+    public ImmutablePair<Integer, InputStream> consumeLiteral(boolean extraCRLF) throws DecodingException {
         // The 1st character must be '{'
         consumeChar('{');
 
@@ -415,8 +415,8 @@ public abstract class ImapRequestLineReader {
             commandContinuationRequest();
         }
 
-        final int size = Integer.parseInt(digits.toString());
-        return read(size, extraCRLF);
+        int size = Integer.parseInt(digits.toString());
+        return ImmutablePair.of(size, read(size, extraCRLF));
     }
 
     private String decode(Charset charset, ByteBuffer buffer) throws DecodingException {
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java
index 7f4b520..9e94825 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/AppendCommandParser.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.james.imap.decode.parser;
 
+import java.io.InputStream;
 import java.time.Clock;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
@@ -81,6 +82,7 @@ public class AppendCommandParser extends AbstractImapCommandParser {
         LocalDateTime datetime = parseDateTime(request);
         request.nextWordChar();
 
-        return new AppendRequest(mailboxName, flags, Date.from(datetime.atZone(ZoneId.systemDefault()).toInstant()), request.consumeLiteral(true), tag);
+        InputStream literal = request.consumeLiteral(true).right;
+        return new AppendRequest(mailboxName, flags, Date.from(datetime.atZone(ZoneId.systemDefault()).toInstant()), literal, tag);
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org