Return-Path: X-Original-To: apmail-james-mime4j-dev-archive@minotaur.apache.org Delivered-To: apmail-james-mime4j-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9BB4DF4D8 for ; Fri, 10 May 2013 17:26:32 +0000 (UTC) Received: (qmail 47818 invoked by uid 500); 10 May 2013 17:26:32 -0000 Delivered-To: apmail-james-mime4j-dev-archive@james.apache.org Received: (qmail 47792 invoked by uid 500); 10 May 2013 17:26:32 -0000 Mailing-List: contact mime4j-dev-help@james.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: mime4j-dev@james.apache.org Delivered-To: mailing list mime4j-dev@james.apache.org Received: (qmail 47781 invoked by uid 99); 10 May 2013 17:26:32 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 May 2013 17:26:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 May 2013 17:26:28 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 300CE238889B; Fri, 10 May 2013 17:26:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1481113 [1/2] - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/codec/ main/java/org/apache/james/mime4j/io/ main/java/org/apache/james/mime4j/util/ test/java/org/apache/james/mime4j/codec/ test/java/org/apache/james/mim... Date: Fri, 10 May 2013 17:26:07 -0000 To: mime4j-dev@james.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130510172608.300CE238889B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Fri May 10 17:26:02 2013 New Revision: 1481113 URL: http://svn.apache.org/r1481113 Log: Use more efficient implementations for byte array and string backed input stream implementations that attempt to minimize intermediate copying while streaming data Added: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java (with props) james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java (with props) james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java (with props) Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/CodecUtilTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamBufferTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/EOLConvertingInputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LimitedInputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineNumberInputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/MimeBoundaryInputStreamTest.java james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/PositionInputStreamTest.java Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java (original) +++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java Fri May 10 17:26:02 2013 @@ -19,14 +19,14 @@ package org.apache.james.mime4j.codec; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.james.mime4j.io.InputStreams; +import org.apache.james.mime4j.util.ByteArrayBuffer; import org.apache.james.mime4j.util.CharsetUtil; /** @@ -44,24 +44,23 @@ public class DecoderUtil { * @return the decoded bytes. */ private static byte[] decodeQuotedPrintable(String s, DecodeMonitor monitor) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - byte[] bytes = s.getBytes("US-ASCII"); - QuotedPrintableInputStream is = new QuotedPrintableInputStream( - new ByteArrayInputStream(bytes), monitor); - - int b; - while ((b = is.read()) != -1) { - baos.write(b); + InputStreams.createAscii(s), monitor); + try { + ByteArrayBuffer buf = new ByteArrayBuffer(s.length()); + int b; + while ((b = is.read()) != -1) { + buf.append(b); + } + return buf.toByteArray(); + } finally { + is.close(); } - } catch (IOException e) { + } catch (IOException ex) { // This should never happen! - throw new IllegalStateException(e); + throw new Error(ex); } - - return baos.toByteArray(); } /** @@ -72,24 +71,23 @@ public class DecoderUtil { * @return the decoded bytes. */ private static byte[] decodeBase64(String s, DecodeMonitor monitor) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - byte[] bytes = s.getBytes("US-ASCII"); - Base64InputStream is = new Base64InputStream( - new ByteArrayInputStream(bytes), monitor); - - int b; - while ((b = is.read()) != -1) { - baos.write(b); + InputStreams.createAscii(s), monitor); + try { + ByteArrayBuffer buf = new ByteArrayBuffer(s.length()); + int b; + while ((b = is.read()) != -1) { + buf.append(b); + } + return buf.toByteArray(); + } finally { + is.close(); } - } catch (IOException e) { + } catch (IOException ex) { // This should never happen! - throw new IllegalStateException(e); + throw new Error(ex); } - - return baos.toByteArray(); } /** Added: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java?rev=1481113&view=auto ============================================================================== --- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java (added) +++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java Fri May 10 17:26:02 2013 @@ -0,0 +1,92 @@ +/**************************************************************** + * 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.mime4j.io; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; + +/** + * {@link InputStream} backed by byte array. + */ +class BinaryInputStream extends InputStream { + + private final ByteBuffer bbuf; + + BinaryInputStream(final ByteBuffer b) { + super(); + this.bbuf = b; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (b == null) { + throw new NullPointerException(); + } + if (off < 0 || len < 0 || off + len > b.length) { + throw new IndexOutOfBoundsException(); + } + if (len == 0) { + return 0; + } + if (this.bbuf.hasRemaining()) { + int chunk = Math.min(this.bbuf.remaining(), len); + this.bbuf.get(b, off, chunk); + return chunk; + } else { + return -1; + } + } + + @Override + public int read() throws IOException { + if (this.bbuf.hasRemaining()) { + return this.bbuf.get() & 0xFF; + } else { + return -1; + } + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public long skip(long n) throws IOException { + int skipped = 0; + while (n > 0 && this.bbuf.hasRemaining()) { + this.bbuf.get(); + n--; + skipped++; + } + return skipped; + } + + @Override + public int available() throws IOException { + return this.bbuf.remaining(); + } + + @Override + public void close() throws IOException { + } + +} Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/BinaryInputStream.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java?rev=1481113&view=auto ============================================================================== --- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java (added) +++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java Fri May 10 17:26:02 2013 @@ -0,0 +1,80 @@ +/**************************************************************** + * 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.mime4j.io; + +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +import org.apache.james.mime4j.util.ByteArrayBuffer; +import org.apache.james.mime4j.util.CharsetUtil; + +/** + * Factory methods for {@link InputStream} instances backed by binary or textual data that attempt + * to minimize intermediate copying while streaming data. + */ +public final class InputStreams { + + private InputStreams() { + } + + public static InputStream create(final byte[] b, int off, int len) { + if (b == null) { + throw new IllegalArgumentException("Byte array may not be null"); + } + return new BinaryInputStream(ByteBuffer.wrap(b, off, len)); + } + + public static InputStream create(final byte[] b) { + if (b == null) { + throw new IllegalArgumentException("Byte array may not be null"); + } + return new BinaryInputStream(ByteBuffer.wrap(b)); + } + + public static InputStream create(final ByteArrayBuffer b) { + if (b == null) { + throw new IllegalArgumentException("Byte array may not be null"); + } + return new BinaryInputStream(ByteBuffer.wrap(b.buffer(), 0, b.length())); + } + + public static InputStream create(final ByteBuffer b) { + if (b == null) { + throw new IllegalArgumentException("Byte array may not be null"); + } + return new BinaryInputStream(b); + } + + public static InputStream createAscii(final String s) { + if (s == null) { + throw new IllegalArgumentException("String may not be null"); + } + return new TextInputStream(s, CharsetUtil.US_ASCII, 1024); + } + + public static InputStream create(final String s, final Charset charset) { + if (s == null) { + throw new IllegalArgumentException("String may not be null"); + } + return new TextInputStream(s, charset != null ? charset : CharsetUtil.DEFAULT_CHARSET, 1024); + } + +} Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/InputStreams.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java?rev=1481113&view=auto ============================================================================== --- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java (added) +++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java Fri May 10 17:26:02 2013 @@ -0,0 +1,131 @@ +/**************************************************************** + * 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.mime4j.io; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; +import java.nio.charset.CodingErrorAction; + +/** + * {@link InputStream} backed by {@link CharSequence}. + */ +class TextInputStream extends InputStream { + + private final CharsetEncoder encoder; + private final CharBuffer cbuf; + private final ByteBuffer bbuf; + + TextInputStream(final CharSequence s, final Charset charset, int bufferSize) { + super(); + this.encoder = charset.newEncoder() + .onMalformedInput(CodingErrorAction.REPLACE) + .onUnmappableCharacter(CodingErrorAction.REPLACE); + this.bbuf = ByteBuffer.allocate(bufferSize); + this.bbuf.flip(); + this.cbuf = CharBuffer.wrap(s); + } + + private void fillBuffer() throws CharacterCodingException { + this.bbuf.compact(); + CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); + if (result.isError()) { + result.throwException(); + } + this.bbuf.flip(); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (b == null) { + throw new NullPointerException(); + } + if (off < 0 || len < 0 || off + len > b.length) { + throw new IndexOutOfBoundsException(); + } + if (len == 0) { + return 0; + } + if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { + return -1; + } + int bytesRead = 0; + while (len > 0) { + if (this.bbuf.hasRemaining()) { + int chunk = Math.min(this.bbuf.remaining(), len); + this.bbuf.get(b, off, chunk); + off += chunk; + len -= chunk; + bytesRead += chunk; + } else { + fillBuffer(); + if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { + break; + } + } + } + return bytesRead == 0 && !this.cbuf.hasRemaining() ? -1 : bytesRead; + } + + @Override + public int read() throws IOException { + for (;;) { + if (this.bbuf.hasRemaining()) { + return this.bbuf.get() & 0xFF; + } else { + fillBuffer(); + if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { + return -1; + } + } + } + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public long skip(long n) throws IOException { + int skipped = 0; + while (n > 0 && this.cbuf.hasRemaining()) { + this.cbuf.get(); + n--; + skipped++; + } + return skipped; + } + + @Override + public int available() throws IOException { + return this.cbuf.remaining(); + } + + @Override + public void close() throws IOException { + } + +} Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java (original) +++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java Fri May 10 17:26:02 2013 @@ -19,6 +19,7 @@ package org.apache.james.mime4j.util; +import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; @@ -162,4 +163,52 @@ public class ContentUtil { .toString(); } + public static byte[] toAsciiByteArray(final String s) { + if (s == null) { + return null; + } + try { + return s.getBytes(CharsetUtil.US_ASCII.name()); + } catch (UnsupportedEncodingException ex) { + // Should never happen + throw new Error(ex); + } + } + + public static String toAsciiString(final byte[] b) { + if (b == null) { + return null; + } + try { + return new String(b, CharsetUtil.US_ASCII.name()); + } catch (UnsupportedEncodingException ex) { + // Should never happen + throw new Error(ex); + } + } + + public static String toAsciiString(final byte[] b, int off, int len) { + if (b == null) { + return null; + } + try { + return new String(b, off, len, CharsetUtil.US_ASCII.name()); + } catch (UnsupportedEncodingException ex) { + // Should never happen + throw new Error(ex); + } + } + + public static String toAsciiString(final ByteArrayBuffer b) { + if (b == null) { + return null; + } + try { + return new String(b.buffer(), 0, b.length(), CharsetUtil.US_ASCII.name()); + } catch (UnsupportedEncodingException ex) { + // Should never happen + throw new Error(ex); + } + } + } Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/Base64InputStreamTest.java Fri May 10 17:26:02 2013 @@ -19,61 +19,69 @@ package org.apache.james.mime4j.codec; -import org.apache.commons.io.output.NullOutputStream; -import org.junit.Assert; -import static org.junit.Assert.fail; -import org.junit.Test; - -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; import java.util.Random; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.output.NullOutputStream; +import org.apache.james.mime4j.io.InputStreams; +import org.apache.james.mime4j.util.ContentUtil; +import org.junit.Assert; +import org.junit.Test; + public class Base64InputStreamTest { + private static Base64InputStream create(final String s) { + return new Base64InputStream(InputStreams.createAscii(s)); + } + + private static Base64InputStream createStrict(final String s) { + return new Base64InputStream(InputStreams.createAscii(s), true); + } + + private static Base64InputStream create(final byte[] b) { + return new Base64InputStream(InputStreams.create(b)); + } + + private static byte[] readBin(final InputStream is) throws IOException { + return IOUtils.toByteArray(is); + } + + private static String readText(final InputStream is) throws IOException { + return ContentUtil.toAsciiString(IOUtils.toByteArray(is)); + } + @Test public void testDecode() throws IOException { - ByteArrayInputStream bis; Base64InputStream decoder; - byte[] bytes; /* * Simple initial test. */ - bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); - decoder = new Base64InputStream(bis); - Assert.assertEquals("This is the plain text message!", toString(read(decoder))); + decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ=="); + Assert.assertEquals("This is the plain text message!", readText(decoder)); /* * Test encoded text padded once, twice and not at all. */ - bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl" + decoder = create(("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl" + "IHBhZGRlZCBvbmNlLi4=")); - decoder = new Base64InputStream(bis); - Assert.assertEquals("This is a text which has to be padded once..", toString(read(decoder))); - bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl" + Assert.assertEquals("This is a text which has to be padded once..", readText(decoder)); + decoder = create(("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl" + "IHBhZGRlZCB0d2ljZQ==")); - decoder = new Base64InputStream(bis); - Assert.assertEquals("This is a text which has to be padded twice", toString(read(decoder))); - bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggd2lsbCBub3Qg" + Assert.assertEquals("This is a text which has to be padded twice", readText(decoder)); + decoder = create(("VGhpcyBpcyBhIHRleHQgd2hpY2ggd2lsbCBub3Qg" + "YmUgcGFkZGVk")); - decoder = new Base64InputStream(bis); - Assert.assertEquals("This is a text which will not be padded", toString(read(decoder))); + Assert.assertEquals("This is a text which will not be padded", readText(decoder)); /* * Test that non base64 characters are ignored. */ - bis = new ByteArrayInputStream( - fromString(" &% VGhp\r\ncyBp\r\ncyB0aGUgcGxhaW4g " + decoder = create((" &% VGhp\r\ncyBp\r\ncyB0aGUgcGxhaW4g " + " \tdGV4dCBtZ?!XNzY*WdlIQ==")); - decoder = new Base64InputStream(bis); - Assert.assertEquals("This is the plain text message!", toString(read(decoder))); + Assert.assertEquals("This is the plain text message!", readText(decoder)); /* * Test that the bytes 0-255 shifted 0, 1 and 2 positions are @@ -103,25 +111,24 @@ public class Base64InputStreamTest { + "BwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5u" + "fo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQ=="; - bis = new ByteArrayInputStream(fromString(s1)); - decoder = new Base64InputStream(bis); - bytes = read(decoder); + byte[] bytes; + + decoder = create(s1); + bytes = readBin(decoder); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals("Position " + i, bytes[i], (byte) i); } - bis = new ByteArrayInputStream(fromString(s2)); - decoder = new Base64InputStream(bis); - bytes = read(decoder); + decoder = create(s2); + bytes = readBin(decoder); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals("Position " + i, bytes[i], (byte) (i + 1)); } - bis = new ByteArrayInputStream(fromString(s3)); - decoder = new Base64InputStream(bis); - bytes = read(decoder); + decoder = create(s3); + bytes = readBin(decoder); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals("Position " + i, bytes[i], (byte) (i + 2)); @@ -130,12 +137,7 @@ public class Base64InputStreamTest { @Test public void testDecodePrematureClose() throws IOException { - ByteArrayInputStream bis; - Base64InputStream decoder; - - bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); - decoder = new Base64InputStream(bis); + Base64InputStream decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ=="); Assert.assertEquals('T', decoder.read()); Assert.assertEquals('h', decoder.read()); decoder.close(); @@ -153,12 +155,11 @@ public class Base64InputStreamTest { new Random(0).nextBytes(data); ByteArrayOutputStream eOut = new ByteArrayOutputStream(); - CodecUtil.encodeBase64(new ByteArrayInputStream(data), eOut); + CodecUtil.encodeBase64(InputStreams.create(data), eOut); byte[] encoded = eOut.toByteArray(); for (int bufferSize = 1; bufferSize <= 1009; bufferSize++) { - ByteArrayInputStream bis = new ByteArrayInputStream(encoded); - Base64InputStream decoder = new Base64InputStream(bis); + Base64InputStream decoder = create(encoded); ByteArrayOutputStream dOut = new ByteArrayOutputStream(); final byte[] buffer = new byte[bufferSize]; @@ -182,9 +183,7 @@ public class Base64InputStreamTest { */ @Test public void testReadInt() throws Exception { - ByteArrayInputStream bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); - Base64InputStream decoder = new Base64InputStream(bis); + Base64InputStream decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ=="); ByteArrayOutputStream out = new ByteArrayOutputStream(); while (true) { @@ -195,8 +194,8 @@ public class Base64InputStreamTest { } decoder.close(); - Assert.assertEquals("This is the plain text message!", toString(out - .toByteArray())); + Assert.assertEquals("This is the plain text message!", + ContentUtil.toAsciiString(out.toByteArray())); } /** @@ -204,10 +203,7 @@ public class Base64InputStreamTest { */ @Test public void testReadOffset() throws Exception { - ByteArrayInputStream bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); - Base64InputStream decoder = new Base64InputStream(bis); - + Base64InputStream decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ=="); byte[] data = new byte[36]; for (int i = 0; ; ) { int bytes = decoder.read(data, i, 5); @@ -218,14 +214,12 @@ public class Base64InputStreamTest { decoder.close(); Assert.assertEquals("This is the plain text message!\0\0\0\0\0", - toString(data)); + ContentUtil.toAsciiString(data)); } @Test public void testStrictUnexpectedEof() throws Exception { - ByteArrayInputStream bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI")); - Base64InputStream decoder = new Base64InputStream(bis, true); + Base64InputStream decoder = createStrict("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI"); try { CodecUtil.copy(decoder, new NullOutputStream()); Assert.fail(); @@ -237,20 +231,16 @@ public class Base64InputStreamTest { @Test public void testLenientUnexpectedEof() throws Exception { - ByteArrayInputStream bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI")); - Base64InputStream decoder = new Base64InputStream(bis, false); + Base64InputStream decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI"); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.copy(decoder, out); - Assert.assertEquals("This is the plain text message", toString(out - .toByteArray())); + Assert.assertEquals("This is the plain text message", ContentUtil.toAsciiString( + out.toByteArray())); } @Test public void testStrictUnexpectedPad() throws Exception { - ByteArrayInputStream bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI=")); - Base64InputStream decoder = new Base64InputStream(bis, true); + Base64InputStream decoder = createStrict("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI="); try { CodecUtil.copy(decoder, new NullOutputStream()); Assert.fail(); @@ -261,39 +251,11 @@ public class Base64InputStreamTest { @Test public void testLenientUnexpectedPad() throws Exception { - ByteArrayInputStream bis = new ByteArrayInputStream( - fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI=")); - Base64InputStream decoder = new Base64InputStream(bis, false); + Base64InputStream decoder = create("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI="); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.copy(decoder, out); - Assert.assertEquals("This is the plain text message", toString(out - .toByteArray())); + Assert.assertEquals("This is the plain text message", ContentUtil.toAsciiString( + out.toByteArray())); } - private byte[] read(InputStream is) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - int b; - while ((b = is.read()) != -1) { - bos.write(b); - } - return bos.toByteArray(); - } - - private byte[] fromString(String s) { - try { - return s.getBytes("US-ASCII"); - } catch (UnsupportedEncodingException e) { - fail(e.getMessage()); - return null; - } - } - - private String toString(byte[] b) { - try { - return new String(b, "US-ASCII"); - } catch (UnsupportedEncodingException e) { - fail(e.getMessage()); - return null; - } - } } Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/CodecUtilTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/CodecUtilTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/CodecUtilTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/CodecUtilTest.java Fri May 10 17:26:02 2013 @@ -19,24 +19,27 @@ package org.apache.james.mime4j.codec; -import org.apache.james.mime4j.ExampleMail; -import org.junit.Assert; import static org.junit.Assert.assertArrayEquals; -import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; + +import org.apache.james.mime4j.ExampleMail; +import org.apache.james.mime4j.io.InputStreams; +import org.apache.james.mime4j.util.CharsetUtil; +import org.apache.james.mime4j.util.ContentUtil; +import org.junit.Assert; +import org.junit.Test; public class CodecUtilTest { @Test public void testCopy() throws Exception { byte[] content = ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES; - ByteArrayInputStream in = new ByteArrayInputStream(content); ByteArrayOutputStream out = new ByteArrayOutputStream(); - CodecUtil.copy(in, out); + CodecUtil.copy(InputStreams.create(content), out); assertArrayEquals(content, out.toByteArray()); } @@ -48,17 +51,17 @@ public class CodecUtilTest { } String expected = sb.toString().replaceAll("(\\d{75})", "$1=\r\n"); - InputStream in = new ByteArrayInputStream(sb.toString().getBytes("US-ASCII")); + InputStream in = InputStreams.createAscii(sb.toString()); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.encodeQuotedPrintableBinary(in, out); - String actual = new String(out.toByteArray(), "US-ASCII"); + String actual = ContentUtil.toAsciiString(out.toByteArray()); Assert.assertEquals(expected, actual); } @Test public void testEncodeQuotedPrintableNonAsciiChars() throws Exception { String s = "7bit content with euro \u20AC symbol"; - InputStream in = new ByteArrayInputStream(s.getBytes("iso-8859-15")); + InputStream in = InputStreams.create(s, Charset.forName("iso-8859-15")); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.encodeQuotedPrintableBinary(in, out); String actual = new String(out.toByteArray(), "US-ASCII"); @@ -79,11 +82,11 @@ public class CodecUtilTest { private String roundtripUsingOutputStream(String input) throws IOException { ByteArrayOutputStream out2 = new ByteArrayOutputStream(); Base64OutputStream outb64 = new Base64OutputStream(out2, 76); - CodecUtil.copy(new ByteArrayInputStream(input.getBytes()), outb64); + CodecUtil.copy(InputStreams.create(input, CharsetUtil.ISO_8859_1), outb64); outb64.flush(); outb64.close(); - InputStream is = new Base64InputStream(new ByteArrayInputStream(out2.toByteArray())); + InputStream is = new Base64InputStream(InputStreams.create(out2.toByteArray())); ByteArrayOutputStream outRoundtrip = new ByteArrayOutputStream(); CodecUtil.copy(is, outRoundtrip); return new String(outRoundtrip.toByteArray()); @@ -105,9 +108,9 @@ public class CodecUtilTest { private String roundtripUsingEncoder(String input) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); - CodecUtil.encodeBase64(new ByteArrayInputStream(input.getBytes()), out); + CodecUtil.encodeBase64(InputStreams.createAscii(input), out); - InputStream is = new Base64InputStream(new ByteArrayInputStream(out.toByteArray())); + InputStream is = new Base64InputStream(InputStreams.create(out.toByteArray())); ByteArrayOutputStream outRoundtrip = new ByteArrayOutputStream(); CodecUtil.copy(is, outRoundtrip); return new String(outRoundtrip.toByteArray()); Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableEncodeTest.java Fri May 10 17:26:02 2013 @@ -20,11 +20,11 @@ package org.apache.james.mime4j.codec; import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.io.InputStreams; import org.apache.james.mime4j.util.CharsetUtil; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.nio.charset.Charset; @@ -110,18 +110,18 @@ public class QuotedPrintableEncodeTest { } private void checkRoundtrip(byte[] content) throws Exception { - InputStream in = new ByteArrayInputStream(content); + InputStream in = InputStreams.create(content); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.encodeQuotedPrintableBinary(in, out); // read back through decoder - in = new QuotedPrintableInputStream(new ByteArrayInputStream(out.toByteArray())); + in = new QuotedPrintableInputStream(InputStreams.create(out.toByteArray())); out = new ByteArrayOutputStream(); IOUtils.copy(in, out); assertEquals(content, out.toByteArray()); } private void check(byte[] content, byte[] expected) throws Exception { - ByteArrayInputStream in = new ByteArrayInputStream(content); + InputStream in = InputStreams.create(content); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.encodeQuotedPrintableBinary(in, out); assertEquals(expected, out.toByteArray()); Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java Fri May 10 17:26:02 2013 @@ -19,88 +19,92 @@ package org.apache.james.mime4j.codec; -import org.junit.Assert; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.io.InputStreams; +import org.apache.james.mime4j.util.CharsetUtil; +import org.junit.Assert; +import org.junit.Test; + public class QuotedPrintableInputStreamTest { + private static String readText(final InputStream is) throws IOException { + return new String(IOUtils.toByteArray(is), CharsetUtil.ISO_8859_1.name()); + } + @Test public void testBasicDecode() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("=e1=e2=E3=E4\r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("=e1=e2=E3=E4\r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("\u00e1\u00e2\u00e3\u00e4\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("\u00e1\u00e2\u00e3\u00e4\r\n", readText(decoder)); } @Test public void testDecodeBufferWrapping() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream( - "=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n=e1=e2=E3=E4\r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); Assert.assertEquals("\u00e1\u00e2\u00e3\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n\u00e1\u00e2\u00e3" + - "\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n", new String(read(decoder), "ISO8859-1")); + "\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n\u00e1\u00e2\u00e3\u00e4\r\n", readText(decoder)); } @Test public void testInvalidValueDecode() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("=e1=g2=E3=E4\r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("=e1=g2=E3=E4\r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("\u00e1=g2\u00e3\u00e4\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("\u00e1=g2\u00e3\u00e4\r\n", readText(decoder)); } @Test public void testDecodeTrailingBlanks() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream(" =e1 =e2 =E3\t=E4 \t \t \r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii(" =e1 =e2 =E3\t=E4 \t \t \r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals(" \u00e1 \u00e2 \u00e3\t\u00e4\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals(" \u00e1 \u00e2 \u00e3\t\u00e4\r\n", readText(decoder)); } @Test public void testCanonicalSoftBreakDecode() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("Soft line =\r\nHard line \r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("Soft line =\r\nHard line \r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("Soft line Hard line\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("Soft line Hard line\r\n", readText(decoder)); } @Test public void testInvalidCR() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("Invalid=\rCR\rHard line \r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("Invalid=\rCR\rHard line \r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); // TODO is this what we really expect from decoding a stream including CR with no LF? - Assert.assertEquals("Invalid=\rCR\rHard line\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("Invalid=\rCR\rHard line\r\n", readText(decoder)); } @Test public void testSoftBreakLoneLFDecode() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("Soft line =\nHard line \r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("Soft line =\nHard line \r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("Soft line Hard line\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("Soft line Hard line\r\n", readText(decoder)); } @Test public void testSoftBreakTrailingBalnksDecode() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("Soft line = \t \r\nHard line \r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("Soft line = \t \r\nHard line \r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("Soft line Hard line\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("Soft line Hard line\r\n", readText(decoder)); } @Test public void testBrokenSoftBreakDecode() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("Soft line =\rHard line \r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("Soft line =\rHard line \r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("Soft line =\rHard line\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("Soft line =\rHard line\r\n", readText(decoder)); } @Test public void testEscapedEQDecode() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("width==340 height=3d200\r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("width==340 height=3d200\r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("width=340 height=200\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("width=340 height=200\r\n", readText(decoder)); // TODO this could be even decoded as width=40 height=200. } @@ -110,68 +114,65 @@ public class QuotedPrintableInputStreamT * This isn't valid qp (==) but it is known to occur in certain * messages, especially spam. */ - ByteArrayInputStream bis = new ByteArrayInputStream("width==\r\n340 height=3d200\r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("width==\r\n340 height=3d200\r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("width=340 height=200\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("width=340 height=200\r\n", readText(decoder)); } @Test public void testSpacesBeforeEOL() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("some \r\n spaced\t\r\ncontent \t \r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("some \r\n spaced\t\r\ncontent \t \r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); - Assert.assertEquals("some\r\n spaced\r\ncontent\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("some\r\n spaced\r\ncontent\r\n", readText(decoder)); } @Test public void testDecodeEndOfStream1() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("01234567".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("01234567"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); - Assert.assertEquals("01234567", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("01234567", readText(decoder)); } @Test public void testDecodeEndOfStream2() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("012345\r".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("012345\r"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); - Assert.assertEquals("012345", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("012345", readText(decoder)); } @Test public void testDecodeEndOfStream3() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("012345\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("012345\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); - Assert.assertEquals("012345\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("012345\r\n", readText(decoder)); } @Test public void testDecodeEndOfStream4() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("01234= ".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("01234= "); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); - Assert.assertEquals("01234", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("01234", readText(decoder)); } @Test public void testDecodeEndOfStream5() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("01234=\r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("01234=\r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); - Assert.assertEquals("01234", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("01234", readText(decoder)); } @Test public void testDecodeEndOfStream6() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis = new ByteArrayInputStream("01234\r\n".getBytes("US-ASCII")); + InputStream bis = InputStreams.createAscii("01234\r\n"); QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(6, bis, false); - Assert.assertEquals("01234\r\n", new String(read(decoder), "ISO8859-1")); + Assert.assertEquals("01234\r\n", readText(decoder)); } @Test public void testDecodePrematureClose() throws IOException, UnsupportedEncodingException { - ByteArrayInputStream bis; - QuotedPrintableInputStream decoder; - - bis = new ByteArrayInputStream("=e1=e2=E3=E4\r\n".getBytes("US-ASCII")); - decoder = new QuotedPrintableInputStream(bis); + InputStream bis = InputStreams.createAscii("=e1=e2=E3=E4\r\n"); + QuotedPrintableInputStream decoder = new QuotedPrintableInputStream(bis); Assert.assertEquals('\u00e1', decoder.read()); Assert.assertEquals('\u00e2', decoder.read()); decoder.close(); @@ -183,12 +184,4 @@ public class QuotedPrintableInputStreamT } } - private byte[] read(InputStream is) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - int b; - while ((b = is.read()) != -1) { - bos.write(b); - } - return bos.toByteArray(); - } } Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableTextEncodeTest.java Fri May 10 17:26:02 2013 @@ -20,11 +20,11 @@ package org.apache.james.mime4j.codec; import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.io.InputStreams; import org.apache.james.mime4j.util.CharsetUtil; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.nio.charset.Charset; @@ -153,11 +153,11 @@ public class QuotedPrintableTextEncodeTe } private void checkRoundtrip(byte[] content) throws Exception { - InputStream in = new ByteArrayInputStream(content); + InputStream in = InputStreams.create(content); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.encodeQuotedPrintable(in, out); // read back through decoder - in = new QuotedPrintableInputStream(new ByteArrayInputStream(out.toByteArray())); + in = new QuotedPrintableInputStream(InputStreams.create(out.toByteArray())); out = new ByteArrayOutputStream(); IOUtils.copy(in, out); assertEquals(content, out.toByteArray()); @@ -170,7 +170,7 @@ public class QuotedPrintableTextEncodeTe private void check(byte[] content, byte[] expected) throws Exception { - ByteArrayInputStream in = new ByteArrayInputStream(content); + InputStream in = InputStreams.create(content); ByteArrayOutputStream out = new ByteArrayOutputStream(); CodecUtil.encodeQuotedPrintable(in, out); assertEquals(expected, out.toByteArray()); Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamBufferTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamBufferTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamBufferTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamBufferTest.java Fri May 10 17:26:02 2013 @@ -19,21 +19,26 @@ package org.apache.james.mime4j.io; +import org.apache.james.mime4j.util.ContentUtil; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; - public class BufferedLineReaderInputStreamBufferTest { + private static BufferedLineReaderInputStream create(final String s) { + return new BufferedLineReaderInputStream(InputStreams.createAscii(s), 4096); + } + + private static BufferedLineReaderInputStream create(final byte[] b) { + return new BufferedLineReaderInputStream(InputStreams.create(b), 4096); + } + @Test public void testInvalidInput() throws Exception { String text = "blah blah yada yada"; - byte[] b1 = text.getBytes("US-ASCII"); String pattern = "blah"; - byte[] b2 = pattern.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + byte[] b2 = ContentUtil.toAsciiByteArray(pattern); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); Assert.assertEquals('b', inbuffer.read()); @@ -97,9 +102,7 @@ public class BufferedLineReaderInputStre @Test public void testBasicOperations() throws Exception { String text = "bla bla yada yada haha haha"; - byte[] b1 = text.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); Assert.assertEquals(0, inbuffer.pos()); Assert.assertEquals(27, inbuffer.limit()); @@ -138,10 +141,8 @@ public class BufferedLineReaderInputStre public void testPatternMatching1() throws Exception { String text = "blabla d is the word"; String pattern = "d"; - byte[] b1 = text.getBytes("US-ASCII"); - byte[] b2 = pattern.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + byte[] b2 = ContentUtil.toAsciiByteArray(pattern); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); int i = inbuffer.indexOf(b2); Assert.assertEquals(7, i); @@ -153,10 +154,8 @@ public class BufferedLineReaderInputStre public void testPatternMatching2() throws Exception { String text = "disddisdissdsidsidsiid"; String pattern = "siid"; - byte[] b1 = text.getBytes("US-ASCII"); - byte[] b2 = pattern.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + byte[] b2 = ContentUtil.toAsciiByteArray(pattern); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); int i = inbuffer.indexOf(b2); Assert.assertEquals(18, i); @@ -168,10 +167,8 @@ public class BufferedLineReaderInputStre public void testPatternMatching3() throws Exception { String text = "bla bla yada yada haha haha"; String pattern = "blah"; - byte[] b1 = text.getBytes("US-ASCII"); - byte[] b2 = pattern.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + byte[] b2 = ContentUtil.toAsciiByteArray(pattern); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); int i = inbuffer.indexOf(b2); Assert.assertEquals(-1, i); @@ -183,10 +180,8 @@ public class BufferedLineReaderInputStre public void testPatternMatching4() throws Exception { String text = "bla bla yada yada haha haha"; String pattern = "bla"; - byte[] b1 = text.getBytes("US-ASCII"); - byte[] b2 = pattern.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + byte[] b2 = ContentUtil.toAsciiByteArray(pattern); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); int i = inbuffer.indexOf(b2); Assert.assertEquals(0, i); @@ -198,10 +193,8 @@ public class BufferedLineReaderInputStre public void testPatternOutOfBound() throws Exception { String text = "bla bla yada yada haha haha"; String pattern1 = "bla bla"; - byte[] b1 = text.getBytes("US-ASCII"); - byte[] b2 = pattern1.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + byte[] b2 = ContentUtil.toAsciiByteArray(pattern1); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); byte[] tmp = new byte[3]; inbuffer.read(tmp); @@ -216,9 +209,7 @@ public class BufferedLineReaderInputStre @Test public void testCharOutOfBound() throws Exception { String text = "zzz blah blah blah ggg"; - byte[] b1 = text.getBytes("US-ASCII"); - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + BufferedLineReaderInputStream inbuffer = create(text); inbuffer.fillBuffer(); byte[] tmp = new byte[3]; inbuffer.read(tmp); @@ -234,8 +225,7 @@ public class BufferedLineReaderInputStre public void test0xFFInBinaryStream() throws Exception { byte[] b1 = new byte[]{1, 2, 3, (byte) 0xff, 10, 1, 2, 3}; byte[] b2 = new byte[]{10}; - BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + BufferedLineReaderInputStream inbuffer = create(b1); inbuffer.fillBuffer(); int i = inbuffer.indexOf(b2); Assert.assertEquals(4, i); Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamTest.java Fri May 10 17:26:02 2013 @@ -20,20 +20,28 @@ package org.apache.james.mime4j.io; import org.apache.james.mime4j.util.ByteArrayBuffer; +import org.apache.james.mime4j.util.ContentUtil; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - public class BufferedLineReaderInputStreamTest { + private static BufferedLineReaderInputStream create(final String s, int bufSize) { + return new BufferedLineReaderInputStream(InputStreams.createAscii(s), bufSize); + } + + private static BufferedLineReaderInputStream create(final byte[] b, int bufSize) { + return new BufferedLineReaderInputStream(InputStreams.create(b), bufSize); + } + + private static BufferedLineReaderInputStream create(final byte[] b, int bufSize, int maxLen) { + return new BufferedLineReaderInputStream(InputStreams.create(b), bufSize, maxLen); + } + @Test public void testBasicOperations() throws Exception { String text = "ah blahblah"; - byte[] b1 = text.getBytes("US-ASCII"); - BufferedLineReaderInputStream instream = new BufferedLineReaderInputStream( - new ByteArrayInputStream(b1), 4096); + BufferedLineReaderInputStream instream = create(text, 32); Assert.assertEquals((byte) 'a', instream.read()); Assert.assertEquals((byte) 'h', instream.read()); @@ -67,21 +75,21 @@ public class BufferedLineReaderInputStre teststrs[3] = "\r\n"; teststrs[4] = "And goodbye\r\n"; - ByteArrayOutputStream outstream = new ByteArrayOutputStream(); + ByteArrayBuffer buf = new ByteArrayBuffer(128); for (String teststr : teststrs) { - outstream.write(teststr.getBytes("US-ASCII")); + byte[] b = ContentUtil.toAsciiByteArray(teststr); + buf.append(b, 0, b.length); } - byte[] raw = outstream.toByteArray(); + byte[] raw = buf.toByteArray(); - BufferedLineReaderInputStream instream = new BufferedLineReaderInputStream( - new ByteArrayInputStream(raw), 16); + BufferedLineReaderInputStream instream = create(raw, 16); ByteArrayBuffer linebuf = new ByteArrayBuffer(8); for (String teststr : teststrs) { linebuf.clear(); instream.readLine(linebuf); - String s = new String(linebuf.toByteArray(), "US-ASCII"); + String s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals(teststr, s); } Assert.assertEquals(-1, instream.readLine(linebuf)); @@ -94,55 +102,54 @@ public class BufferedLineReaderInputStre public void testReadEmptyLine() throws Exception { String teststr = "\n\n\r\n\r\r\n\n\n\n\n\n"; - byte[] raw = teststr.getBytes("US-ASCII"); + byte[] raw = ContentUtil.toAsciiByteArray(teststr); - LineReaderInputStream instream = new BufferedLineReaderInputStream( - new ByteArrayInputStream(raw), 4); + BufferedLineReaderInputStream instream = create(raw, 4); ByteArrayBuffer linebuf = new ByteArrayBuffer(8); linebuf.clear(); instream.readLine(linebuf); - String s = new String(linebuf.toByteArray(), "US-ASCII"); + String s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\r\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\r\r\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); Assert.assertEquals(-1, instream.readLine(linebuf)); @@ -155,17 +162,15 @@ public class BufferedLineReaderInputStre public void testReadEmptyLineMaxLimit() throws Exception { String teststr = "1234567890\r\n"; - byte[] raw = teststr.getBytes("US-ASCII"); + byte[] raw = ContentUtil.toAsciiByteArray(teststr); - LineReaderInputStream instream1 = new BufferedLineReaderInputStream( - new ByteArrayInputStream(raw), 1024, 13); + LineReaderInputStream instream1 = create(raw, 1024, 13); ByteArrayBuffer linebuf = new ByteArrayBuffer(8); linebuf.clear(); instream1.readLine(linebuf); instream1.close(); - LineReaderInputStream instream2 = new BufferedLineReaderInputStream( - new ByteArrayInputStream(raw), 1024, 12); + LineReaderInputStream instream2 = create(raw, 1024, 12); linebuf.clear(); try { instream2.readLine(linebuf); Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/EOLConvertingInputStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/EOLConvertingInputStreamTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/EOLConvertingInputStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/EOLConvertingInputStreamTest.java Fri May 10 17:26:02 2013 @@ -19,14 +19,12 @@ package org.apache.james.mime4j.io; +import java.io.IOException; + +import org.apache.james.mime4j.util.ContentUtil; import org.junit.Assert; -import static org.junit.Assert.fail; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; - public class EOLConvertingInputStreamTest { @Test @@ -67,12 +65,11 @@ public class EOLConvertingInputStreamTes private void testConvertBoth(String s1, String s2) throws IOException { byte[] bytes = new byte[1024]; - ByteArrayInputStream bais = new ByteArrayInputStream(fromString(s1)); EOLConvertingInputStream in = - new EOLConvertingInputStream(bais, + new EOLConvertingInputStream(InputStreams.createAscii(s1), EOLConvertingInputStream.CONVERT_BOTH); int n = in.read(bytes); - Assert.assertEquals(s2, toString(bytes, n)); + Assert.assertEquals(s2, ContentUtil.toAsciiString(bytes, 0, n > 0 ? n : 0)); in.close(); } @@ -80,12 +77,11 @@ public class EOLConvertingInputStreamTes private void testConvertCR(String s1, String s2) throws IOException { byte[] bytes = new byte[1024]; - ByteArrayInputStream bais = new ByteArrayInputStream(fromString(s1)); EOLConvertingInputStream in = - new EOLConvertingInputStream(bais, + new EOLConvertingInputStream(InputStreams.createAscii(s1), EOLConvertingInputStream.CONVERT_CR); int n = in.read(bytes); - Assert.assertEquals(s2, toString(bytes, n)); + Assert.assertEquals(s2, ContentUtil.toAsciiString(bytes, 0, n > 0 ? n : 0)); in.close(); } @@ -93,34 +89,13 @@ public class EOLConvertingInputStreamTes private void testConvertLF(String s1, String s2) throws IOException { byte[] bytes = new byte[1024]; - ByteArrayInputStream bais = new ByteArrayInputStream(fromString(s1)); EOLConvertingInputStream in = - new EOLConvertingInputStream(bais, + new EOLConvertingInputStream(InputStreams.createAscii(s1), EOLConvertingInputStream.CONVERT_LF); int n = in.read(bytes); - Assert.assertEquals(s2, toString(bytes, n)); + Assert.assertEquals(s2, ContentUtil.toAsciiString(bytes, 0, n > 0 ? n : 0)); in.close(); } - private String toString(byte[] b, int len) { - try { - if (len == -1) { - return ""; - } - return new String(b, 0, len, "US-ASCII"); - } catch (UnsupportedEncodingException e) { - fail(e.getMessage()); - return null; - } - } - - private byte[] fromString(String s) { - try { - return s.getBytes("US-ASCII"); - } catch (UnsupportedEncodingException e) { - fail(e.getMessage()); - return null; - } - } } Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LimitedInputStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LimitedInputStreamTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LimitedInputStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LimitedInputStreamTest.java Fri May 10 17:26:02 2013 @@ -22,7 +22,6 @@ package org.apache.james.mime4j.io; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.IOException; public class LimitedInputStreamTest { @@ -30,8 +29,7 @@ public class LimitedInputStreamTest { @Test public void testUpToLimitRead() throws IOException { byte[] data = new byte[]{'0', '1', '2', '3', '4', '5', '6'}; - ByteArrayInputStream instream = new ByteArrayInputStream(data); - LimitedInputStream limitedStream = new LimitedInputStream(instream, 3); + LimitedInputStream limitedStream = new LimitedInputStream(InputStreams.create(data), 3); Assert.assertEquals(0, limitedStream.getPosition()); Assert.assertTrue(limitedStream.read() != -1); Assert.assertEquals(1, limitedStream.getPosition()); Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineNumberInputStreamTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineNumberInputStreamTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineNumberInputStreamTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineNumberInputStreamTest.java Fri May 10 17:26:02 2013 @@ -22,7 +22,6 @@ package org.apache.james.mime4j.io; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.IOException; public class LineNumberInputStreamTest { @@ -32,8 +31,7 @@ public class LineNumberInputStreamTest { @Test public void testReadSingleByte() throws IOException { String s = "Yada\r\nyada\r\nyada\r\n"; - LineNumberInputStream is = new LineNumberInputStream( - new ByteArrayInputStream(s.getBytes())); + LineNumberInputStream is = new LineNumberInputStream(InputStreams.createAscii(s)); for (int i = 0; i < 6; i++) { Assert.assertEquals(1, is.getLineNumber()); @@ -63,8 +61,7 @@ public class LineNumberInputStreamTest { @Test public void testReadManyBytes() throws IOException { String s = "Yada\r\nyada\r\nyada\r\n"; - LineNumberInputStream is = new LineNumberInputStream( - new ByteArrayInputStream(s.getBytes())); + LineNumberInputStream is = new LineNumberInputStream(InputStreams.createAscii(s)); byte[] buf = new byte[4]; Assert.assertEquals(1, is.getLineNumber()); Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java?rev=1481113&r1=1481112&r2=1481113&view=diff ============================================================================== --- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java (original) +++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptorTest.java Fri May 10 17:26:02 2013 @@ -20,21 +20,28 @@ package org.apache.james.mime4j.io; import org.apache.james.mime4j.util.ByteArrayBuffer; +import org.apache.james.mime4j.util.ContentUtil; import org.junit.Assert; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; - public class LineReaderInputStreamAdaptorTest { + private static LineReaderInputStreamAdaptor create(final String s) { + return new LineReaderInputStreamAdaptor(InputStreams.createAscii(s)); + } + + private static LineReaderInputStreamAdaptor create(final byte[] b) { + return new LineReaderInputStreamAdaptor(InputStreams.create(b)); + } + + private static LineReaderInputStreamAdaptor create(final byte[] b, int bufSize) { + return new LineReaderInputStreamAdaptor(InputStreams.create(b), bufSize); + } + @Test public void testBasicOperations() throws Exception { String text = "ah blahblah"; - byte[] b1 = text.getBytes("US-ASCII"); - - LineReaderInputStreamAdaptor instream = new LineReaderInputStreamAdaptor( - new ByteArrayInputStream(b1)); + LineReaderInputStreamAdaptor instream = create(text); Assert.assertEquals((byte) 'a', instream.read()); Assert.assertEquals((byte) 'h', instream.read()); @@ -68,21 +75,21 @@ public class LineReaderInputStreamAdapto teststrs[3] = "\r\n"; teststrs[4] = "And goodbye\r\n"; - ByteArrayOutputStream outstream = new ByteArrayOutputStream(); + ByteArrayBuffer buf = new ByteArrayBuffer(128); for (String teststr : teststrs) { - outstream.write(teststr.getBytes("US-ASCII")); + byte[] b = ContentUtil.toAsciiByteArray(teststr); + buf.append(b, 0, b.length); } - byte[] raw = outstream.toByteArray(); + byte[] raw = buf.toByteArray(); - LineReaderInputStreamAdaptor instream = new LineReaderInputStreamAdaptor( - new ByteArrayInputStream(raw)); + LineReaderInputStreamAdaptor instream = create(raw); ByteArrayBuffer linebuf = new ByteArrayBuffer(8); for (String teststr : teststrs) { linebuf.clear(); instream.readLine(linebuf); - String s = new String(linebuf.toByteArray(), "US-ASCII"); + String s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals(teststr, s); } Assert.assertEquals(-1, instream.readLine(linebuf)); @@ -97,53 +104,52 @@ public class LineReaderInputStreamAdapto String teststr = "\n\n\r\n\r\r\n\n\n\n\n\n"; byte[] raw = teststr.getBytes("US-ASCII"); - LineReaderInputStreamAdaptor instream = new LineReaderInputStreamAdaptor( - new ByteArrayInputStream(raw)); + LineReaderInputStreamAdaptor instream = create(raw); ByteArrayBuffer linebuf = new ByteArrayBuffer(8); linebuf.clear(); instream.readLine(linebuf); - String s = new String(linebuf.toByteArray(), "US-ASCII"); + String s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\r\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\r\r\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); linebuf.clear(); instream.readLine(linebuf); - s = new String(linebuf.toByteArray(), "US-ASCII"); + s = ContentUtil.toAsciiString(linebuf); Assert.assertEquals("\n", s); Assert.assertEquals(-1, instream.readLine(linebuf)); @@ -158,16 +164,14 @@ public class LineReaderInputStreamAdapto String teststr = "1234567890\r\n"; byte[] raw = teststr.getBytes("US-ASCII"); - LineReaderInputStreamAdaptor instream1 = new LineReaderInputStreamAdaptor( - new ByteArrayInputStream(raw), 13); + LineReaderInputStreamAdaptor instream1 = create(raw, 13); ByteArrayBuffer linebuf = new ByteArrayBuffer(8); linebuf.clear(); instream1.readLine(linebuf); instream1.close(); - LineReaderInputStreamAdaptor instream2 = new LineReaderInputStreamAdaptor( - new ByteArrayInputStream(raw), 12); + LineReaderInputStreamAdaptor instream2 = create(raw, 12); linebuf.clear(); try { instream2.readLine(linebuf);