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:42 UTC

[james-project] 06/19: [Refactoring] remove LimitingFileInputStream

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 524705ddfd1bd975eeffe4c17ca6dc44c19b3471
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Wed Apr 15 23:10:06 2020 +0200

    [Refactoring] remove LimitingFileInputStream
    
    	The only feature that is not handled by ByteStreams.limit
    	is the possibility to use the underlying file channel but
    	it's never used
---
 .../mailbox/maildir/mail/model/MaildirMessage.java |   6 +-
 .../store/streaming/LimitingFileInputStream.java   | 263 ---------------------
 2 files changed, 4 insertions(+), 265 deletions(-)

diff --git a/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java b/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
index 5826657..c92a2d9 100644
--- a/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
+++ b/mailbox/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/model/MaildirMessage.java
@@ -38,7 +38,6 @@ import org.apache.james.mailbox.store.mail.model.Property;
 import org.apache.james.mailbox.store.mail.model.impl.MessageParser;
 import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
 import org.apache.james.mailbox.store.streaming.CountingInputStream;
-import org.apache.james.mailbox.store.streaming.LimitingFileInputStream;
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.message.DefaultBodyDescriptorBuilder;
 import org.apache.james.mime4j.message.MaximalBodyDescriptor;
@@ -47,6 +46,8 @@ import org.apache.james.mime4j.stream.MimeConfig;
 import org.apache.james.mime4j.stream.MimeTokenStream;
 import org.apache.james.mime4j.stream.RecursionMode;
 
+import com.google.common.io.ByteStreams;
+
 public class MaildirMessage implements Message {
 
     private final MaildirMessageName messageName;
@@ -263,7 +264,8 @@ public class MaildirMessage implements Message {
         if (limit < 0) {
             limit = 0;
         }
-        return new LimitingFileInputStream(messageName.getFile(), limit);
+
+        return ByteStreams.limit(new FileInputStream(messageName.getFile()), limit);
     }
 
     @Override
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/streaming/LimitingFileInputStream.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/streaming/LimitingFileInputStream.java
deleted file mode 100644
index 1c7b738..0000000
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/streaming/LimitingFileInputStream.java
+++ /dev/null
@@ -1,263 +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.mailbox.store.streaming;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
-import java.nio.channels.ReadableByteChannel;
-import java.nio.channels.WritableByteChannel;
-
-public class LimitingFileInputStream extends FileInputStream {
-    private long pos = 0;
-    private final long limit;
-    
-    public LimitingFileInputStream(File file, long limit) throws FileNotFoundException {
-        super(file);
-        this.limit = limit;
-    }
-
-    @Override
-    public int read() throws IOException {
-        if (pos >= limit) {
-            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 >= limit) {
-            return -1;
-        }
-        int readLimit;
-        if (pos + len >= limit) {
-            readLimit = (int) limit - (int) pos;
-        } else {
-            readLimit = len;
-        }
-
-        int i = super.read(b, off, readLimit);
-        pos += i;
-        return i;
-    }
-
-    @Override
-    public long skip(long n) throws IOException {
-        long currentPos = pos;
-        long i = super.skip(n);
-        if (currentPos == pos) {
-            pos += i;
-        }
-        return i;
-    }
-
-    @Override
-    public int available() throws IOException {
-        int i = super.available();
-        if (i == -1) {
-            return -1;
-        } else {
-            if (i >= limit) {
-                return (int) limit - (int) pos;
-            } else {
-                return i;
-            }
-        }
-    }
-    
-    
-    /**
-     * Return the limit 
-     * 
-     * @return limit
-     */
-    public long getLimit() {
-        return limit;
-    }
-
-    /**
-     * Return a READ-ONLY {@link FileChannel} which is limited also in the size
-     * 
-     * @return channel
-     */
-    @Override
-    public FileChannel getChannel() {
-        return new LimitingFileChannel(super.getChannel());
-    }
-    
-    
-    /**
-     * A {@link FileChannel} implementation which wrapps another {@link FileChannel} and limit the access to it
-     * 
-     *
-     */
-    private final class LimitingFileChannel extends FileChannel {
-
-        private final FileChannel channel;
-
-        public LimitingFileChannel(FileChannel channel) {
-            this.channel = channel;
-        }
-        
-        @Override
-        public int read(ByteBuffer dst) throws IOException {
-            int bufLimit = dst.limit();
-            int left = (int) bytesLeft();
-            int r;
-            if (bufLimit > left) {
-                dst.limit(left);
-                r = channel.read(dst);
-                dst.limit(bufLimit);
-            } else {
-                r = channel.read(dst);
-            }
-            return r;
-            
-        }
-
-        @Override
-        public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
-            long r = 0;
-            for (int a = offset; a < length; a++) {
-                r += read(dsts[a]);
-            }
-            
-            return r;
-        }
-
-        @Override
-        public int write(ByteBuffer src) throws IOException {
-            throw new IOException("Read-Only FileChannel");
-        }
-
-        @Override
-        public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
-            throw new IOException("Read-Only FileChannel");
-        }
-
-        @Override
-        public long position() throws IOException {
-            return channel.position();
-        }
-
-        @Override
-        public FileChannel position(long newPosition) throws IOException {
-            if (newPosition <= limit) {
-                channel.position(newPosition);
-            }
-            return LimitingFileChannel.this;
-        }
-
-        @Override
-        public long size() throws IOException {
-            return limit;
-        }
-
-        @Override
-        public FileChannel truncate(long size) throws IOException {
-            throw new IOException("Read-Only FileChannel");
-        }
-
-        @Override
-        public void force(boolean metaData) throws IOException {
-            channel.force(metaData);
-        }
-
-        @Override
-        public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
-            if (position > limit) {
-                return 0;
-            } else {
-                long left = bytesLeft();
-                
-                if (count > left) {
-                    count = left;
-                }
-                return channel.transferTo(position, count, target);
-            }
-        }
-
-        @Override
-        public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
-            throw new IOException("Read-Only FileChannel");
-        }
-
-        @Override
-        public int read(ByteBuffer dst, long position) throws IOException {
-            if (position > size()) {
-                return 0;
-            }
-            int bufLimit = dst.limit();
-            int left = (int) bytesLeft();
-            int r;
-            if (bufLimit > left) {
-                dst.limit(left);
-                r = channel.read(dst, position);
-                dst.limit(bufLimit);
-            } else {
-                r = channel.read(dst, position);
-            }
-            return r;
-        }
-
-        @Override
-        public int write(ByteBuffer src, long position) throws IOException {
-            throw new IOException("Read-Only FileChannel");
-
-        }
-
-        @Override
-        public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException {
-            return channel.map(mode, position, size);
-        }
-
-        @Override
-        public FileLock lock(long position, long size, boolean shared) throws IOException {
-            return channel.lock(position, size, shared);
-        }
-
-        @Override
-        public FileLock tryLock(long position, long size, boolean shared) throws IOException {
-            return channel.tryLock(position, size, shared);
-        }
-
-        @Override
-        protected void implCloseChannel() throws IOException {
-            channel.close();
-        }
-        
-        private long bytesLeft() throws IOException {
-            return limit - position();
-        }
-    }
-    
-
-}


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