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:38 UTC
[james-project] 02/19: [Refactoring] replace FixedLengthInputStream
with guava ByteStreams.limit
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 a2f9042a4aa44cd163312eb661bf76f271df4fa5
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Wed Apr 15 21:40:55 2020 +0200
[Refactoring] replace FixedLengthInputStream with guava ByteStreams.limit
---
.../imap/decode/ImapRequestStreamLineReader.java | 11 ++-
.../james/imap/utils/FixedLengthInputStream.java | 108 ---------------------
.../netty/NettyStreamImapRequestLineReader.java | 9 +-
3 files changed, 11 insertions(+), 117 deletions(-)
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestStreamLineReader.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestStreamLineReader.java
index 1b1114b..bbc7445 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestStreamLineReader.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/ImapRequestStreamLineReader.java
@@ -25,7 +25,8 @@ import java.io.OutputStream;
import org.apache.james.imap.api.display.HumanReadableText;
import org.apache.james.imap.utils.EolInputStream;
-import org.apache.james.imap.utils.FixedLengthInputStream;
+
+import com.google.common.io.ByteStreams;
/**
* {@link ImapRequestLineReader} which use normal IO Streaming
@@ -44,7 +45,7 @@ public class ImapRequestStreamLineReader extends ImapRequestLineReader {
* Reads the next character in the current line. This method will continue
* to return the same character until the {@link #consume()} method is
* called.
- *
+ *
* @return The next character TODO: character encoding is variable and
* cannot be determine at the token level; this char is not accurate
* reported; should be an octet
@@ -77,11 +78,11 @@ public class ImapRequestStreamLineReader extends ImapRequestLineReader {
// Unset the next char.
nextSeen = false;
nextChar = 0;
- FixedLengthInputStream in = new FixedLengthInputStream(input, size);
+ InputStream limited = ByteStreams.limit(input, size);
if (extraCRLF) {
- return new EolInputStream(this, in);
+ return new EolInputStream(this, limited);
} else {
- return in;
+ return limited;
}
}
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/utils/FixedLengthInputStream.java b/protocols/imap/src/main/java/org/apache/james/imap/utils/FixedLengthInputStream.java
deleted file mode 100644
index fda5184..0000000
--- a/protocols/imap/src/main/java/org/apache/james/imap/utils/FixedLengthInputStream.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one *
- * or more contributor license agreements. See the NOTICE file *
- * distributed with this work for additional information *
- * regarding copyright ownership. The ASF licenses this file *
- * to you under the Apache License, Version 2.0 (the *
- * "License"); you may not use this file except in compliance *
- * with the License. You may obtain a copy of the License at *
- * *
- * http://www.apache.org/licenses/LICENSE-2.0 *
- * *
- * Unless required by applicable law or agreed to in writing, *
- * software distributed under the License is distributed on an *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
- * KIND, either express or implied. See the License for the *
- * specific language governing permissions and limitations *
- * under the License. *
- ****************************************************************/
-
-package org.apache.james.imap.utils;
-
-import java.io.FilterInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- *
- * An input stream which reads a fixed number of bytes from the underlying input
- * stream. Once the number of bytes has been read, the FixedLengthInputStream
- * will act as thought the end of stream has been reached, even if more bytes
- * are present in the underlying input stream.
- */
-public class FixedLengthInputStream extends FilterInputStream {
- private long pos = 0;
-
- private final long length;
-
- public FixedLengthInputStream(InputStream in, long length) {
- super(in);
- this.length = length;
- }
-
- @Override
- public int read() throws IOException {
- if (pos >= length) {
- return -1;
- }
- pos++;
- return super.read();
- }
-
- @Override
- public int read(byte[] b) throws IOException {
-
- return read(b, 0, b.length);
- }
-
- @Override
- public int read(byte[] b, int off, int len) throws IOException {
-
- if (pos >= length) {
- return -1;
- }
-
- if (pos + len >= length) {
- int readLimit = (int) length - (int) pos;
- pos = length;
-
- return super.read(b, off, readLimit);
- }
-
- int i = super.read(b, off, len);
- pos += i;
- return i;
-
- }
-
- @Override
- public long skip(long n) throws IOException {
- throw new IOException("Not implemented");
- // return super.skip( n );
- }
-
- @Override
- public int available() throws IOException {
- return (int) (length - pos);
- }
-
- @Override
- public void close() throws IOException {
- // Don't do anything to the underlying stream.
- }
-
- @Override
- public void mark(int readlimit) {
- // Don't do anything.
- }
-
- @Override
- public synchronized void reset() throws IOException {
- throw new IOException("mark not supported");
- }
-
- @Override
- public boolean markSupported() {
- return false;
- }
-}
diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyStreamImapRequestLineReader.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyStreamImapRequestLineReader.java
index 8e7481d..d6b2f61 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyStreamImapRequestLineReader.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/NettyStreamImapRequestLineReader.java
@@ -24,9 +24,10 @@ import java.io.InputStream;
import org.apache.james.imap.api.display.HumanReadableText;
import org.apache.james.imap.decode.DecodingException;
import org.apache.james.imap.utils.EolInputStream;
-import org.apache.james.imap.utils.FixedLengthInputStream;
import org.jboss.netty.channel.Channel;
+import com.google.common.io.ByteStreams;
+
public class NettyStreamImapRequestLineReader extends AbstractNettyImapRequestLineReader {
private final InputStream in;
@@ -86,11 +87,11 @@ public class NettyStreamImapRequestLineReader extends AbstractNettyImapRequestLi
// Unset the next char.
nextSeen = false;
nextChar = 0;
- FixedLengthInputStream fin = new FixedLengthInputStream(this.in, size);
+ InputStream limited = ByteStreams.limit(this.in, size);
if (extraCRLF) {
- return new EolInputStream(this, fin);
+ return new EolInputStream(this, limited);
} else {
- return fin;
+ return limited;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org