From 902e9917c99279fb8902992180c373823878c62c Mon Sep 17 00:00:00 2001 From: Tung Tran Date: Tue, 18 Jul 2023 15:19:08 +0700 Subject: [PATCH] fixup! [UPGRADE] - Maven commons-io:commons-io 2.11.0 -> 2.13.0 - adapt new version --- .../mailbox/store/StoreMessageManager.java | 14 +- .../james/transport/KeyStoreHolder.java | 7 +- .../james/transport/SMIMEKeyHolder.java | 8 +- .../transport/mailets/RecoverAttachment.java | 7 +- .../core/MimeMessageInputStreamSource.java | 7 +- .../io/UnsynchronizedBufferedInputStream.java | 238 ------------------ 6 files changed, 30 insertions(+), 251 deletions(-) delete mode 100644 server/container/util/src/main/java/org/apache/james/util/io/UnsynchronizedBufferedInputStream.java diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java index a5fe322689d1..1bbe3e27c7d9 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java @@ -46,6 +46,8 @@ import javax.mail.Flags.Flag; import org.apache.commons.io.input.TeeInputStream; +import org.apache.commons.io.input.UnsynchronizedBufferedInputStream; +import org.apache.commons.io.input.UnsynchronizedFilterInputStream; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.apache.james.events.EventBus; @@ -107,7 +109,6 @@ import org.apache.james.mime4j.stream.RecursionMode; import org.apache.james.util.io.BodyOffsetInputStream; import org.apache.james.util.io.InputStreamConsummer; -import org.apache.james.util.io.UnsynchronizedBufferedInputStream; import org.apache.james.util.streams.Iterators; import org.reactivestreams.Publisher; @@ -358,7 +359,10 @@ public AppendResult appendMessage(InputStream msgIn, Date internalDate, final Ma file = Files.createTempFile("imap", ".msg").toFile(); try (FileOutputStream out = new FileOutputStream(file); BufferedOutputStream bufferedOut = new BufferedOutputStream(out); - UnsynchronizedBufferedInputStream tmpMsgIn = new UnsynchronizedBufferedInputStream(new TeeInputStream(msgIn, bufferedOut)); + UnsynchronizedFilterInputStream tmpMsgIn = UnsynchronizedBufferedInputStream.builder() + .setInputStream(new TeeInputStream(msgIn, bufferedOut)) + .setBufferSize(8192) + .get(); BodyOffsetInputStream bIn = new BodyOffsetInputStream(tmpMsgIn)) { Pair pair = parseProperties(bIn); PropertyBuilder propertyBuilder = pair.getLeft(); @@ -405,8 +409,10 @@ private Mono appendMessage(Content msgIn, Date internalDate, final } try (InputStream contentStream = msgIn.getInputStream(); - UnsynchronizedBufferedInputStream bufferedContentStream = new UnsynchronizedBufferedInputStream(contentStream); - BodyOffsetInputStream bIn = new BodyOffsetInputStream(bufferedContentStream)) { + UnsynchronizedFilterInputStream bufferedContentStream = UnsynchronizedBufferedInputStream.builder() + .setInputStream(contentStream) + .get(); + BodyOffsetInputStream bIn = new BodyOffsetInputStream(bufferedContentStream)) { Pair pair = parseProperties(bIn); PropertyBuilder propertyBuilder = pair.getLeft(); HeaderImpl headers = pair.getRight(); diff --git a/mailet/crypto/src/main/java/org/apache/james/transport/KeyStoreHolder.java b/mailet/crypto/src/main/java/org/apache/james/transport/KeyStoreHolder.java index 0142293e1b50..7d2dc57012f1 100644 --- a/mailet/crypto/src/main/java/org/apache/james/transport/KeyStoreHolder.java +++ b/mailet/crypto/src/main/java/org/apache/james/transport/KeyStoreHolder.java @@ -46,7 +46,7 @@ import javax.mail.MessagingException; -import org.apache.james.util.io.UnsynchronizedBufferedInputStream; +import org.apache.commons.io.input.UnsynchronizedBufferedInputStream; import org.bouncycastle.cert.jcajce.JcaCertStoreBuilder; import org.bouncycastle.cert.selector.X509CertificateHolderSelector; import org.bouncycastle.cert.selector.jcajce.JcaX509CertSelectorConverter; @@ -101,7 +101,10 @@ public KeyStoreHolder(String keyStoreFileName, String keyStorePassword, String k } keyStore = KeyStore.getInstance(keyStoreType); - keyStore.load(new UnsynchronizedBufferedInputStream(new FileInputStream(keyStoreFileName)), keyStorePassword.toCharArray()); + keyStore.load(UnsynchronizedBufferedInputStream + .builder() + .setInputStream(new FileInputStream(keyStoreFileName)) + .get(), keyStorePassword.toCharArray()); if (keyStore.size() == 0) { throw new KeyStoreException("The keystore must be not empty"); } diff --git a/mailet/crypto/src/main/java/org/apache/james/transport/SMIMEKeyHolder.java b/mailet/crypto/src/main/java/org/apache/james/transport/SMIMEKeyHolder.java index 5bb183043525..020437a5d619 100644 --- a/mailet/crypto/src/main/java/org/apache/james/transport/SMIMEKeyHolder.java +++ b/mailet/crypto/src/main/java/org/apache/james/transport/SMIMEKeyHolder.java @@ -45,7 +45,7 @@ import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; -import org.apache.james.util.io.UnsynchronizedBufferedInputStream; +import org.apache.commons.io.input.UnsynchronizedBufferedInputStream; import org.bouncycastle.cert.jcajce.JcaCertStore; import org.bouncycastle.cms.SignerInfoGenerator; import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoGeneratorBuilder; @@ -128,7 +128,11 @@ public SMIMEKeyHolder(String keyStoreFileName, String keyStorePassword, String k } KeyStore keyStore = KeyStore.getInstance(keyStoreType); - keyStore.load(new UnsynchronizedBufferedInputStream(new FileInputStream(keyStoreFileName)), keyStorePassword.toCharArray()); + + keyStore.load(UnsynchronizedBufferedInputStream.builder() + .setInputStream(new FileInputStream(keyStoreFileName)) + .get(), + keyStorePassword.toCharArray()); Enumeration aliases = keyStore.aliases(); if (keyAlias == null) { diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java index 87a898bc8f9c..1c5a3d862b85 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RecoverAttachment.java @@ -30,7 +30,7 @@ import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; -import org.apache.james.util.io.UnsynchronizedBufferedInputStream; +import org.apache.commons.io.input.UnsynchronizedBufferedInputStream; import org.apache.mailet.AttributeName; import org.apache.mailet.AttributeUtils; import org.apache.mailet.AttributeValue; @@ -119,8 +119,9 @@ private void processAttachment(Mail mail, Map> attach continue; } byte[] bytes = (byte[]) i.next().getValue(); - InputStream is = new UnsynchronizedBufferedInputStream( - new ByteArrayInputStream(bytes)); + InputStream is = UnsynchronizedBufferedInputStream.builder() + .setInputStream(new ByteArrayInputStream(bytes)) + .get(); MimeBodyPart p = new MimeBodyPart(is); if (!(message.isMimeType("multipart/*") && (message .getContent() instanceof MimeMultipart))) { diff --git a/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageInputStreamSource.java b/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageInputStreamSource.java index 4ba872108dad..58f4d4e1c6f3 100644 --- a/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageInputStreamSource.java +++ b/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageInputStreamSource.java @@ -33,10 +33,10 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.UnsynchronizedBufferedInputStream; import org.apache.commons.io.output.DeferredFileOutputStream; import org.apache.james.lifecycle.api.Disposable; import org.apache.james.util.SizeFormat; -import org.apache.james.util.io.UnsynchronizedBufferedInputStream; /** * Takes an input stream and creates a repeatable input stream source for a @@ -186,7 +186,10 @@ public InputStream getInputStream() throws IOException { if (getResource().getOut().isInMemory()) { return new ByteArrayInputStream(getResource().getOut().getData()); } else { - InputStream in = new UnsynchronizedBufferedInputStream(new FileInputStream(getResource().getOut().getFile()), 2048); + InputStream in = UnsynchronizedBufferedInputStream.builder() + .setInputStream(new FileInputStream(getResource().getOut().getFile())) + .setBufferSize(2048) + .get(); getResource().streams.add(in); return in; } diff --git a/server/container/util/src/main/java/org/apache/james/util/io/UnsynchronizedBufferedInputStream.java b/server/container/util/src/main/java/org/apache/james/util/io/UnsynchronizedBufferedInputStream.java deleted file mode 100644 index cab709cc83b9..000000000000 --- a/server/container/util/src/main/java/org/apache/james/util/io/UnsynchronizedBufferedInputStream.java +++ /dev/null @@ -1,238 +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.util.io; - -import java.io.FilterInputStream; -import java.io.IOException; -import java.io.InputStream; - -/** - * Copied from {@link java.io.BufferedInputStream} with the following modifications: - * - Removal of 'synchronized' keyword - * - Removal of 'volatile' keyword - * - Removal of Unsafe usages - * - * See https://issues.apache.org/jira/projects/IO/issues/IO-786 for rationals - */ -public class UnsynchronizedBufferedInputStream extends FilterInputStream { - private static int DEFAULT_BUFFER_SIZE = 8192; - private static int MAX_BUFFER_SIZE = 2147483639; - protected byte[] buf; - protected int count; - protected int pos; - protected int markpos; - protected int marklimit; - - private InputStream getInIfOpen() throws IOException { - InputStream input = this.in; - if (input == null) { - throw new IOException("Stream closed"); - } else { - return input; - } - } - - private byte[] getBufIfOpen() throws IOException { - byte[] buffer = this.buf; - if (buffer == null) { - throw new IOException("Stream closed"); - } else { - return buffer; - } - } - - public UnsynchronizedBufferedInputStream(InputStream in) { - this(in, DEFAULT_BUFFER_SIZE); - } - - public UnsynchronizedBufferedInputStream(InputStream in, int size) { - super(in); - this.markpos = -1; - if (size <= 0) { - throw new IllegalArgumentException("Buffer size <= 0"); - } else { - this.buf = new byte[size]; - } - } - - private void fill() throws IOException { - byte[] buffer = this.getBufIfOpen(); - int nsz; - if (this.markpos < 0) { - this.pos = 0; - } else if (this.pos >= buffer.length) { - if (this.markpos > 0) { - nsz = this.pos - this.markpos; - System.arraycopy(buffer, this.markpos, buffer, 0, nsz); - this.pos = nsz; - this.markpos = 0; - } else if (buffer.length >= this.marklimit) { - this.markpos = -1; - this.pos = 0; - } else { - if (buffer.length >= MAX_BUFFER_SIZE) { - throw new OutOfMemoryError("Required array size too large"); - } - - nsz = this.pos <= MAX_BUFFER_SIZE - this.pos ? this.pos * 2 : MAX_BUFFER_SIZE; - if (nsz > this.marklimit) { - nsz = this.marklimit; - } - - byte[] nbuf = new byte[nsz]; - System.arraycopy(buffer, 0, nbuf, 0, this.pos); - - buffer = nbuf; - } - } - - this.count = this.pos; - nsz = this.getInIfOpen().read(buffer, this.pos, buffer.length - this.pos); - if (nsz > 0) { - this.count = nsz + this.pos; - } - - } - - public int read() throws IOException { - if (this.pos >= this.count) { - this.fill(); - if (this.pos >= this.count) { - return -1; - } - } - - return this.getBufIfOpen()[this.pos++] & 255; - } - - private int read1(byte[] b, int off, int len) throws IOException { - int avail = this.count - this.pos; - if (avail <= 0) { - if (len >= this.getBufIfOpen().length && this.markpos < 0) { - return this.getInIfOpen().read(b, off, len); - } - - this.fill(); - avail = this.count - this.pos; - if (avail <= 0) { - return -1; - } - } - - int cnt = avail < len ? avail : len; - System.arraycopy(this.getBufIfOpen(), this.pos, b, off, cnt); - this.pos += cnt; - return cnt; - } - - public int read(byte[] b, int off, int len) throws IOException { - this.getBufIfOpen(); - if ((off | len | off + len | b.length - (off + len)) < 0) { - throw new IndexOutOfBoundsException(); - } else if (len == 0) { - return 0; - } else { - int n = 0; - - InputStream input; - do { - int nread = this.read1(b, off + n, len - n); - if (nread <= 0) { - return n == 0 ? nread : n; - } - - n += nread; - if (n >= len) { - return n; - } - - input = this.in; - } while (input == null || input.available() > 0); - - return n; - } - } - - public long skip(long n) throws IOException { - this.getBufIfOpen(); - if (n <= 0L) { - return 0L; - } else { - long avail = (long)(this.count - this.pos); - if (avail <= 0L) { - if (this.markpos < 0) { - return this.getInIfOpen().skip(n); - } - - this.fill(); - avail = (long)(this.count - this.pos); - if (avail <= 0L) { - return 0L; - } - } - - long skipped = avail < n ? avail : n; - this.pos = (int)((long)this.pos + skipped); - return skipped; - } - } - - public int available() throws IOException { - int n = this.count - this.pos; - int avail = this.getInIfOpen().available(); - return n > 2147483647 - avail ? 2147483647 : n + avail; - } - - public void mark(int readlimit) { - this.marklimit = readlimit; - this.markpos = this.pos; - } - - public void reset() throws IOException { - this.getBufIfOpen(); - if (this.markpos < 0) { - throw new IOException("Resetting to invalid mark"); - } else { - this.pos = this.markpos; - } - } - - public boolean markSupported() { - return true; - } - - public void close() throws IOException { - while (true) { - byte[] buffer; - if ((buffer = this.buf) != null) { - - InputStream input = this.in; - this.in = null; - if (input != null) { - input.close(); - } - - return; - } - - return; - } - } -}