You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2009/07/23 03:46:50 UTC
svn commit: r796922 [1/2] - in /commons/proper/codec/trunk/src:
java/org/apache/commons/codec/binary/ test/org/apache/commons/codec/
test/org/apache/commons/codec/binary/
Author: niallp
Date: Thu Jul 23 01:46:50 2009
New Revision: 796922
URL: http://svn.apache.org/viewvc?rev=796922&view=rev
Log:
set svn:eol-style to native
Modified:
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java (contents, props changed)
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java (contents, props changed)
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java (contents, props changed)
commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java (contents, props changed)
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java (contents, props changed)
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java (contents, props changed)
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64TestData.java (contents, props changed)
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/StringBytesUtilsTest.java (contents, props changed)
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java?rev=796922&r1=796921&r2=796922&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java Thu Jul 23 01:46:50 2009
@@ -1,165 +1,165 @@
-/*
- * 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.commons.codec.binary;
-
-import java.io.FilterInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * Provides Base64 encoding and decoding in a streaming fashion (unlimited size).
- * When encoding the default lineLength is 76 characters and the default
- * lineEnding is CRLF, but these can be overridden by using the appropriate
- * constructor.
- * <p>
- * The default behaviour of the Base64InputStream is to DECODE, whereas the
- * default behaviour of the Base64OutputStream is to ENCODE, but this
- * behaviour can be overridden by using a different constructor.
- * </p><p>
- * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
- * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
- * </p><p>
- * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
- * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
- * </p>
- * @author Apache Software Foundation
- * @version $Id $
- * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
- * @since 1.4
- */
-public class Base64InputStream extends FilterInputStream {
-
- private final boolean doEncode;
- private final Base64 base64;
- private final byte[] singleByte = new byte[1];
-
- /**
- * Creates a Base64InputStream such that all data read is Base64-decoded
- * from the original provided InputStream.
- *
- * @param in InputStream to wrap.
- */
- public Base64InputStream(InputStream in) {
- this(in, false);
- }
-
- /**
- * Creates a Base64InputStream such that all data read is either
- * Base64-encoded or Base64-decoded from the original provided InputStream.
- *
- * @param in InputStream to wrap.
- * @param doEncode true if we should encode all data read from us,
- * false if we should decode.
- */
- public Base64InputStream(InputStream in, boolean doEncode) {
- super(in);
- this.doEncode = doEncode;
- this.base64 = new Base64();
- }
-
- /**
- * Creates a Base64InputStream such that all data read is either
- * Base64-encoded or Base64-decoded from the original provided InputStream.
- *
- * @param in InputStream to wrap.
- * @param doEncode true if we should encode all data read from us,
- * false if we should decode.
- * @param lineLength If doEncode is true, each line of encoded
- * data will contain lineLength characters.
- * If lineLength <=0, the encoded data is not divided into lines.
- * If doEncode is false, lineLength is ignored.
- * @param lineSeparator If doEncode is true, each line of encoded
- * data will be terminated with this byte sequence (e.g. \r\n).
- * If lineLength <= 0, the lineSeparator is not used.
- * If doEncode is false lineSeparator is ignored.
- */
- public Base64InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator) {
- super(in);
- this.doEncode = doEncode;
- this.base64 = new Base64(lineLength, lineSeparator);
- }
-
- /**
- * Reads one <code>byte</code> from this input stream.
- *
- * @return the byte as an integer in the range 0 to 255
- * Returns -1 if EOF has been reached.
- */
- public int read() throws IOException {
- int r = read(singleByte, 0, 1);
- while (r == 0) {
- r = read(singleByte, 0, 1);
- }
- if (r > 0) {
- return singleByte[0] < 0 ? 256 + singleByte[0] : singleByte[0];
- }
- return -1;
- }
-
- /**
- * Attempts to read <code>len</code> bytes into the specified
- * <code>b</code> array starting at <code>offset</code> from
- * this InputStream.
- *
- * @param b destination byte array
- * @param offset where to start writing the bytes
- * @param len maximum number of bytes to read
- *
- * @return number of bytes read
- * @throws IOException if an I/O error occurs.
- * @throws NullPointerException if the byte array parameter is null
- * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid
- */
- public int read(byte b[], int offset, int len) throws IOException {
- if (b == null) {
- throw new NullPointerException();
- } else if (offset < 0 || len < 0) {
- throw new IndexOutOfBoundsException();
- } else if (offset > b.length || offset + len > b.length) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return 0;
- } else {
- if (!base64.hasData()) {
- byte[] buf = new byte[doEncode ? 4096 : 8192];
- int c = in.read(buf);
-
- // A little optimization to avoid System.arraycopy()
- // when possible.
- if (c > 0 && b.length == len) {
- base64.setInitialBuffer(b, offset, len);
- }
-
- if (doEncode) {
- base64.encode(buf, 0, c);
- } else {
- base64.decode(buf, 0, c);
- }
- }
- return base64.readResults(b, offset, len);
- }
- }
-
- /**
- * {@inheritDoc}
- * @return false
- */
- public boolean markSupported() {
- return false; // not an easy job to support marks
- }
-}
+/*
+ * 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.commons.codec.binary;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Provides Base64 encoding and decoding in a streaming fashion (unlimited size).
+ * When encoding the default lineLength is 76 characters and the default
+ * lineEnding is CRLF, but these can be overridden by using the appropriate
+ * constructor.
+ * <p>
+ * The default behaviour of the Base64InputStream is to DECODE, whereas the
+ * default behaviour of the Base64OutputStream is to ENCODE, but this
+ * behaviour can be overridden by using a different constructor.
+ * </p><p>
+ * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
+ * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
+ * </p><p>
+ * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
+ * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
+ * </p>
+ * @author Apache Software Foundation
+ * @version $Id $
+ * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
+ * @since 1.4
+ */
+public class Base64InputStream extends FilterInputStream {
+
+ private final boolean doEncode;
+ private final Base64 base64;
+ private final byte[] singleByte = new byte[1];
+
+ /**
+ * Creates a Base64InputStream such that all data read is Base64-decoded
+ * from the original provided InputStream.
+ *
+ * @param in InputStream to wrap.
+ */
+ public Base64InputStream(InputStream in) {
+ this(in, false);
+ }
+
+ /**
+ * Creates a Base64InputStream such that all data read is either
+ * Base64-encoded or Base64-decoded from the original provided InputStream.
+ *
+ * @param in InputStream to wrap.
+ * @param doEncode true if we should encode all data read from us,
+ * false if we should decode.
+ */
+ public Base64InputStream(InputStream in, boolean doEncode) {
+ super(in);
+ this.doEncode = doEncode;
+ this.base64 = new Base64();
+ }
+
+ /**
+ * Creates a Base64InputStream such that all data read is either
+ * Base64-encoded or Base64-decoded from the original provided InputStream.
+ *
+ * @param in InputStream to wrap.
+ * @param doEncode true if we should encode all data read from us,
+ * false if we should decode.
+ * @param lineLength If doEncode is true, each line of encoded
+ * data will contain lineLength characters.
+ * If lineLength <=0, the encoded data is not divided into lines.
+ * If doEncode is false, lineLength is ignored.
+ * @param lineSeparator If doEncode is true, each line of encoded
+ * data will be terminated with this byte sequence (e.g. \r\n).
+ * If lineLength <= 0, the lineSeparator is not used.
+ * If doEncode is false lineSeparator is ignored.
+ */
+ public Base64InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator) {
+ super(in);
+ this.doEncode = doEncode;
+ this.base64 = new Base64(lineLength, lineSeparator);
+ }
+
+ /**
+ * Reads one <code>byte</code> from this input stream.
+ *
+ * @return the byte as an integer in the range 0 to 255
+ * Returns -1 if EOF has been reached.
+ */
+ public int read() throws IOException {
+ int r = read(singleByte, 0, 1);
+ while (r == 0) {
+ r = read(singleByte, 0, 1);
+ }
+ if (r > 0) {
+ return singleByte[0] < 0 ? 256 + singleByte[0] : singleByte[0];
+ }
+ return -1;
+ }
+
+ /**
+ * Attempts to read <code>len</code> bytes into the specified
+ * <code>b</code> array starting at <code>offset</code> from
+ * this InputStream.
+ *
+ * @param b destination byte array
+ * @param offset where to start writing the bytes
+ * @param len maximum number of bytes to read
+ *
+ * @return number of bytes read
+ * @throws IOException if an I/O error occurs.
+ * @throws NullPointerException if the byte array parameter is null
+ * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid
+ */
+ public int read(byte b[], int offset, int len) throws IOException {
+ if (b == null) {
+ throw new NullPointerException();
+ } else if (offset < 0 || len < 0) {
+ throw new IndexOutOfBoundsException();
+ } else if (offset > b.length || offset + len > b.length) {
+ throw new IndexOutOfBoundsException();
+ } else if (len == 0) {
+ return 0;
+ } else {
+ if (!base64.hasData()) {
+ byte[] buf = new byte[doEncode ? 4096 : 8192];
+ int c = in.read(buf);
+
+ // A little optimization to avoid System.arraycopy()
+ // when possible.
+ if (c > 0 && b.length == len) {
+ base64.setInitialBuffer(b, offset, len);
+ }
+
+ if (doEncode) {
+ base64.encode(buf, 0, c);
+ } else {
+ base64.decode(buf, 0, c);
+ }
+ }
+ return base64.readResults(b, offset, len);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @return false
+ */
+ public boolean markSupported() {
+ return false; // not an easy job to support marks
+ }
+}
Propchange: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64InputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java?rev=796922&r1=796921&r2=796922&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java Thu Jul 23 01:46:50 2009
@@ -1,182 +1,182 @@
-/*
- * 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.commons.codec.binary;
-
-import java.io.FilterOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-
-/**
- * Provides Base64 encoding and decoding in a streaming fashion (unlimited size).
- * When encoding the default lineLength is 76 characters and the default
- * lineEnding is CRLF, but these can be overridden by using the appropriate
- * constructor.
- * <p>
- * The default behaviour of the Base64OutputStream is to ENCODE, whereas the
- * default behaviour of the Base64InputStream is to DECODE. But this behaviour
- * can be overridden by using a different constructor.
- * </p><p>
- * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
- * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
- * </p><p>
- * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
- * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
- * </p>
- * @author Apache Software Foundation
- * @version $Id $
- * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
- * @since 1.4
- */
-public class Base64OutputStream extends FilterOutputStream {
- private final boolean doEncode;
- private final Base64 base64;
- private final byte[] singleByte = new byte[1];
-
- /**
- * Creates a Base64OutputStream such that all data written is Base64-encoded
- * to the original provided OutputStream.
- *
- * @param out OutputStream to wrap.
- */
- public Base64OutputStream(OutputStream out) {
- this(out, true);
- }
-
- /**
- * Creates a Base64OutputStream such that all data written is either
- * Base64-encoded or Base64-decoded to the original provided OutputStream.
- *
- * @param out OutputStream to wrap.
- * @param doEncode true if we should encode all data written to us,
- * false if we should decode.
- */
- public Base64OutputStream(OutputStream out, boolean doEncode) {
- super(out);
- this.doEncode = doEncode;
- this.base64 = new Base64();
- }
-
- /**
- * Creates a Base64OutputStream such that all data written is either
- * Base64-encoded or Base64-decoded to the original provided OutputStream.
- *
- * @param out OutputStream to wrap.
- * @param doEncode true if we should encode all data written to us,
- * false if we should decode.
- * @param lineLength If doEncode is true, each line of encoded
- * data will contain lineLength characters.
- * If lineLength <=0, the encoded data is not divided into lines.
- * If doEncode is false, lineLength is ignored.
- * @param lineSeparator If doEncode is true, each line of encoded
- * data will be terminated with this byte sequence (e.g. \r\n).
- * If lineLength <= 0, the lineSeparator is not used.
- * If doEncode is false lineSeparator is ignored.
- */
- public Base64OutputStream(OutputStream out, boolean doEncode, int lineLength, byte[] lineSeparator) {
- super(out);
- this.doEncode = doEncode;
- this.base64 = new Base64(lineLength, lineSeparator);
- }
-
- /**
- * Writes the specified <code>byte</code> to this output stream.
- */
- public void write(int i) throws IOException {
- singleByte[0] = (byte) i;
- write(singleByte, 0, 1);
- }
-
- /**
- * Writes <code>len</code> bytes from the specified
- * <code>b</code> array starting at <code>offset</code> to
- * this output stream.
- *
- * @param b source byte array
- * @param offset where to start reading the bytes
- * @param len maximum number of bytes to write
- *
- * @throws IOException if an I/O error occurs.
- * @throws NullPointerException if the byte array parameter is null
- * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid
- */
- public void write(byte b[], int offset, int len) throws IOException {
- if (b == null) {
- throw new NullPointerException();
- } else if (offset < 0 || len < 0) {
- throw new IndexOutOfBoundsException();
- } else if (offset > b.length || offset + len > b.length) {
- throw new IndexOutOfBoundsException();
- } else if (len > 0) {
- if (doEncode) {
- base64.encode(b, offset, len);
- } else {
- base64.decode(b, offset, len);
- }
- flush(false);
- }
- }
-
- /**
- * Flushes this output stream and forces any buffered output bytes
- * to be written out to the stream. If propogate is true, the wrapped
- * stream will also be flushed.
- *
- * @param propogate boolean flag to indicate whether the wrapped
- * OutputStream should also be flushed.
- * @throws IOException if an I/O error occurs.
- */
- private void flush(boolean propogate) throws IOException {
- int avail = base64.avail();
- if (avail > 0) {
- byte[] buf = new byte[avail];
- int c = base64.readResults(buf, 0, avail);
- if (c > 0) {
- out.write(buf, 0, c);
- }
- }
- if (propogate) {
- out.flush();
- }
- }
-
- /**
- * Flushes this output stream and forces any buffered output bytes
- * to be written out to the stream.
- *
- * @throws IOException if an I/O error occurs.
- */
- public void flush() throws IOException {
- flush(true);
- }
-
- /**
- * Closes this output stream and releases any system resources
- * associated with the stream.
- */
- public void close() throws IOException {
- // Notify encoder of EOF (-1).
- if (doEncode) {
- base64.encode(singleByte, 0, -1);
- } else {
- base64.decode(singleByte, 0, -1);
- }
- flush();
- out.close();
- }
-
-}
+/*
+ * 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.commons.codec.binary;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * Provides Base64 encoding and decoding in a streaming fashion (unlimited size).
+ * When encoding the default lineLength is 76 characters and the default
+ * lineEnding is CRLF, but these can be overridden by using the appropriate
+ * constructor.
+ * <p>
+ * The default behaviour of the Base64OutputStream is to ENCODE, whereas the
+ * default behaviour of the Base64InputStream is to DECODE. But this behaviour
+ * can be overridden by using a different constructor.
+ * </p><p>
+ * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
+ * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
+ * </p><p>
+ * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
+ * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
+ * </p>
+ * @author Apache Software Foundation
+ * @version $Id $
+ * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
+ * @since 1.4
+ */
+public class Base64OutputStream extends FilterOutputStream {
+ private final boolean doEncode;
+ private final Base64 base64;
+ private final byte[] singleByte = new byte[1];
+
+ /**
+ * Creates a Base64OutputStream such that all data written is Base64-encoded
+ * to the original provided OutputStream.
+ *
+ * @param out OutputStream to wrap.
+ */
+ public Base64OutputStream(OutputStream out) {
+ this(out, true);
+ }
+
+ /**
+ * Creates a Base64OutputStream such that all data written is either
+ * Base64-encoded or Base64-decoded to the original provided OutputStream.
+ *
+ * @param out OutputStream to wrap.
+ * @param doEncode true if we should encode all data written to us,
+ * false if we should decode.
+ */
+ public Base64OutputStream(OutputStream out, boolean doEncode) {
+ super(out);
+ this.doEncode = doEncode;
+ this.base64 = new Base64();
+ }
+
+ /**
+ * Creates a Base64OutputStream such that all data written is either
+ * Base64-encoded or Base64-decoded to the original provided OutputStream.
+ *
+ * @param out OutputStream to wrap.
+ * @param doEncode true if we should encode all data written to us,
+ * false if we should decode.
+ * @param lineLength If doEncode is true, each line of encoded
+ * data will contain lineLength characters.
+ * If lineLength <=0, the encoded data is not divided into lines.
+ * If doEncode is false, lineLength is ignored.
+ * @param lineSeparator If doEncode is true, each line of encoded
+ * data will be terminated with this byte sequence (e.g. \r\n).
+ * If lineLength <= 0, the lineSeparator is not used.
+ * If doEncode is false lineSeparator is ignored.
+ */
+ public Base64OutputStream(OutputStream out, boolean doEncode, int lineLength, byte[] lineSeparator) {
+ super(out);
+ this.doEncode = doEncode;
+ this.base64 = new Base64(lineLength, lineSeparator);
+ }
+
+ /**
+ * Writes the specified <code>byte</code> to this output stream.
+ */
+ public void write(int i) throws IOException {
+ singleByte[0] = (byte) i;
+ write(singleByte, 0, 1);
+ }
+
+ /**
+ * Writes <code>len</code> bytes from the specified
+ * <code>b</code> array starting at <code>offset</code> to
+ * this output stream.
+ *
+ * @param b source byte array
+ * @param offset where to start reading the bytes
+ * @param len maximum number of bytes to write
+ *
+ * @throws IOException if an I/O error occurs.
+ * @throws NullPointerException if the byte array parameter is null
+ * @throws IndexOutOfBoundsException if offset, len or buffer size are invalid
+ */
+ public void write(byte b[], int offset, int len) throws IOException {
+ if (b == null) {
+ throw new NullPointerException();
+ } else if (offset < 0 || len < 0) {
+ throw new IndexOutOfBoundsException();
+ } else if (offset > b.length || offset + len > b.length) {
+ throw new IndexOutOfBoundsException();
+ } else if (len > 0) {
+ if (doEncode) {
+ base64.encode(b, offset, len);
+ } else {
+ base64.decode(b, offset, len);
+ }
+ flush(false);
+ }
+ }
+
+ /**
+ * Flushes this output stream and forces any buffered output bytes
+ * to be written out to the stream. If propogate is true, the wrapped
+ * stream will also be flushed.
+ *
+ * @param propogate boolean flag to indicate whether the wrapped
+ * OutputStream should also be flushed.
+ * @throws IOException if an I/O error occurs.
+ */
+ private void flush(boolean propogate) throws IOException {
+ int avail = base64.avail();
+ if (avail > 0) {
+ byte[] buf = new byte[avail];
+ int c = base64.readResults(buf, 0, avail);
+ if (c > 0) {
+ out.write(buf, 0, c);
+ }
+ }
+ if (propogate) {
+ out.flush();
+ }
+ }
+
+ /**
+ * Flushes this output stream and forces any buffered output bytes
+ * to be written out to the stream.
+ *
+ * @throws IOException if an I/O error occurs.
+ */
+ public void flush() throws IOException {
+ flush(true);
+ }
+
+ /**
+ * Closes this output stream and releases any system resources
+ * associated with the stream.
+ */
+ public void close() throws IOException {
+ // Notify encoder of EOF (-1).
+ if (doEncode) {
+ base64.encode(singleByte, 0, -1);
+ } else {
+ base64.decode(singleByte, 0, -1);
+ }
+ flush();
+ out.close();
+ }
+
+}
Propchange: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64OutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java?rev=796922&r1=796921&r2=796922&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java Thu Jul 23 01:46:50 2009
@@ -1,272 +1,272 @@
-/*
- * 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.commons.codec.binary;
-
-import java.io.UnsupportedEncodingException;
-
-import org.apache.commons.codec.RequiredCharsetNames;
-
-/**
- * Converts String to bytes using the encodings required by the Java specification.
- *
- * @see RequiredCharsetNames
- * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
- * @version $Id: $
- */
-public class StringBytesUtils {
-
- /**
- * Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new
- * byte array.
- *
- * @param string
- * the String to encode
- * @return encoded bytes
- * @throws IllegalStateException
- * Thrown when the charset is missing, which should be never according the the Java specification.
- * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
- * @see #getSupportedBytes(String, String)
- */
- public static byte[] getBytesIso8859_1(String string) {
- return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.ISO_8859_1);
- }
-
- /**
- * Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte
- * array.
- *
- * @param string
- * the String to encode
- * @return encoded bytes
- * @throws IllegalStateException
- * Thrown when the charset is missing, which should be never according the the Java specification.
- * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
- * @see #getSupportedBytes(String, String)
- */
- public static byte[] getBytesUsAscii(String string) {
- return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.US_ASCII);
- }
-
- /**
- * Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte
- * array.
- *
- * @param string
- * the String to encode
- * @return encoded bytes
- * @throws IllegalStateException
- * Thrown when the charset is missing, which should be never according the the Java specification.
- * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
- * @see #getSupportedBytes(String, String)
- */
- public static byte[] getBytesUtf16(String string) {
- return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16);
- }
-
- /**
- * Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte
- * array.
- *
- * @param string
- * the String to encode
- * @return encoded bytes
- * @throws IllegalStateException
- * Thrown when the charset is missing, which should be never according the the Java specification.
- * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
- * @see #getSupportedBytes(String, String)
- */
- public static byte[] getBytesUtf16Be(String string) {
- return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16BE);
- }
-
- /**
- * Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte
- * array.
- *
- * @param string
- * the String to encode
- * @return encoded bytes
- * @throws IllegalStateException
- * Thrown when the charset is missing, which should be never according the the Java specification.
- * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
- * @see #getSupportedBytes(String, String)
- */
- public static byte[] getBytesUtf16Le(String string) {
- return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16LE);
- }
-
- /**
- * Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte
- * array.
- *
- * @param string
- * the String to encode
- * @return encoded bytes
- * @throws IllegalStateException
- * Thrown when the charset is missing, which should be never according the the Java specification.
- * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
- * @see #getSupportedBytes(String, String)
- */
- public static byte[] getBytesUtf8(String string) {
- return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_8);
- }
-
- /**
- * Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte
- * array.
- * <p>
- * This method catches {@link UnsupportedEncodingException} and rethrows it as {@link IllegalStateException}, which
- * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
- * </p>
- *
- * @param string
- * the String to encode
- * @param charsetName
- * The name of a required {@link java.nio.charset.Charset}
- * @return encoded bytes
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
- * required charset name.
- * @see RequiredCharsetNames
- * @see String#getBytes(String)
- */
- public static byte[] getSupportedBytes(String string, String charsetName) {
- try {
- return string.getBytes(charsetName);
- } catch (UnsupportedEncodingException e) {
- throw StringBytesUtils.newIllegalStateException(charsetName, e);
- }
- }
-
- private static IllegalStateException newIllegalStateException(String charsetName, UnsupportedEncodingException e) {
- return new IllegalStateException(charsetName + ": " + e);
- }
-
- /**
- * Constructs a new <code>String</code> by decoding the specified array of bytes using the given charset.
- * <p>
- * This method catches {@link UnsupportedEncodingException} and re-throws it as {@link IllegalStateException}, which
- * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
- * </p>
- *
- * @param bytes
- * The bytes to be decoded into characters
- * @param charsetName
- * The name of a required {@link java.nio.charset.Charset}
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
- * required charset name.
- * @see RequiredCharsetNames
- * @see String#String(byte[], String)
- */
- public static String newString(byte[] bytes, String charsetName) {
- try {
- return new String(bytes, charsetName);
- } catch (UnsupportedEncodingException e) {
- throw StringBytesUtils.newIllegalStateException(charsetName, e);
- }
- }
-
- /**
- * Constructs a new <code>String</code> by decoding the specified array of bytes using the ISO-8859-1 charset.
- *
- * @param bytes
- * The bytes to be decoded into characters
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
- * charset is required.
- */
- public static String newStringIso8859_1(byte[] bytes) {
- return StringBytesUtils.newString(bytes, RequiredCharsetNames.ISO_8859_1);
- }
-
- /**
- * Constructs a new <code>String</code> by decoding the specified array of bytes using the US-ASCII charset.
- *
- * @param bytes
- * The bytes to be decoded into characters
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
- * charset is required.
- */
- public static String newStringUsAscii(byte[] bytes) {
- return StringBytesUtils.newString(bytes, RequiredCharsetNames.US_ASCII);
- }
-
- /**
- * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16 charset.
- *
- * @param bytes
- * The bytes to be decoded into characters
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
- * charset is required.
- */
- public static String newStringUtf16(byte[] bytes) {
- return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16);
- }
-
- /**
- * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16BE charset.
- *
- * @param bytes
- * The bytes to be decoded into characters
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
- * charset is required.
- */
- public static String newStringUtf16Be(byte[] bytes) {
- return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16BE);
- }
-
- /**
- * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16LE charset.
- *
- * @param bytes
- * The bytes to be decoded into characters
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
- * charset is required.
- */
- public static String newStringUtf16Le(byte[] bytes) {
- return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16LE);
- }
-
- /**
- * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset.
- *
- * @param bytes
- * The bytes to be decoded into characters
- * @throws IllegalStateException
- * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
- * charset is required.
- */
- public static String newStringUtf8(byte[] bytes) {
- return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_8);
- }
-
- private StringBytesUtils() {
- // noop, cannot instantiate.
- }
-}
+/*
+ * 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.commons.codec.binary;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.commons.codec.RequiredCharsetNames;
+
+/**
+ * Converts String to bytes using the encodings required by the Java specification.
+ *
+ * @see RequiredCharsetNames
+ * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
+ * @version $Id: $
+ */
+public class StringBytesUtils {
+
+ /**
+ * Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new
+ * byte array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesIso8859_1(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.ISO_8859_1);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUsAscii(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.US_ASCII);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf16(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf16Be(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16BE);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf16Le(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16LE);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf8(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_8);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte
+ * array.
+ * <p>
+ * This method catches {@link UnsupportedEncodingException} and rethrows it as {@link IllegalStateException}, which
+ * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
+ * </p>
+ *
+ * @param string
+ * the String to encode
+ * @param charsetName
+ * The name of a required {@link java.nio.charset.Charset}
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
+ * required charset name.
+ * @see RequiredCharsetNames
+ * @see String#getBytes(String)
+ */
+ public static byte[] getSupportedBytes(String string, String charsetName) {
+ try {
+ return string.getBytes(charsetName);
+ } catch (UnsupportedEncodingException e) {
+ throw StringBytesUtils.newIllegalStateException(charsetName, e);
+ }
+ }
+
+ private static IllegalStateException newIllegalStateException(String charsetName, UnsupportedEncodingException e) {
+ return new IllegalStateException(charsetName + ": " + e);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the given charset.
+ * <p>
+ * This method catches {@link UnsupportedEncodingException} and re-throws it as {@link IllegalStateException}, which
+ * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
+ * </p>
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @param charsetName
+ * The name of a required {@link java.nio.charset.Charset}
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
+ * required charset name.
+ * @see RequiredCharsetNames
+ * @see String#String(byte[], String)
+ */
+ public static String newString(byte[] bytes, String charsetName) {
+ try {
+ return new String(bytes, charsetName);
+ } catch (UnsupportedEncodingException e) {
+ throw StringBytesUtils.newIllegalStateException(charsetName, e);
+ }
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the ISO-8859-1 charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringIso8859_1(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.ISO_8859_1);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the US-ASCII charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUsAscii(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.US_ASCII);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16 charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf16(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16BE charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf16Be(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16BE);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16LE charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf16Le(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16LE);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf8(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_8);
+ }
+
+ private StringBytesUtils() {
+ // noop, cannot instantiate.
+ }
+}
Propchange: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java?rev=796922&r1=796921&r2=796922&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java Thu Jul 23 01:46:50 2009
@@ -1,46 +1,46 @@
-/*
- * Copyright (C) 1993-2003 SEAGULL
- *
- * RequiredCharsetNamesTest.java
- * Created on Jul 20, 2009, 6:08:58 PM
- *
- */
-
-package org.apache.commons.codec;
-
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
-/**
- * Sanity checks.
- *
- * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
- * @version $Id: $
- */
-public class RequiredCharsetNamesTest extends TestCase {
-
- public void testIso8859_1() {
- Assert.assertEquals("ISO-8859-1", RequiredCharsetNames.ISO_8859_1);
- }
-
- public void testUsAscii() {
- Assert.assertEquals("US-ASCII", RequiredCharsetNames.US_ASCII);
- }
-
- public void testUtf16() {
- Assert.assertEquals("UTF-16", RequiredCharsetNames.UTF_16);
- }
-
- public void testUtf16Be() {
- Assert.assertEquals("UTF-16BE", RequiredCharsetNames.UTF_16BE);
- }
-
- public void testUtf16Le() {
- Assert.assertEquals("UTF-16LE", RequiredCharsetNames.UTF_16LE);
- }
-
- public void testUtf8() {
- Assert.assertEquals("UTF-8", RequiredCharsetNames.UTF_8);
- }
-
-}
+/*
+ * Copyright (C) 1993-2003 SEAGULL
+ *
+ * RequiredCharsetNamesTest.java
+ * Created on Jul 20, 2009, 6:08:58 PM
+ *
+ */
+
+package org.apache.commons.codec;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * Sanity checks.
+ *
+ * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
+ * @version $Id: $
+ */
+public class RequiredCharsetNamesTest extends TestCase {
+
+ public void testIso8859_1() {
+ Assert.assertEquals("ISO-8859-1", RequiredCharsetNames.ISO_8859_1);
+ }
+
+ public void testUsAscii() {
+ Assert.assertEquals("US-ASCII", RequiredCharsetNames.US_ASCII);
+ }
+
+ public void testUtf16() {
+ Assert.assertEquals("UTF-16", RequiredCharsetNames.UTF_16);
+ }
+
+ public void testUtf16Be() {
+ Assert.assertEquals("UTF-16BE", RequiredCharsetNames.UTF_16BE);
+ }
+
+ public void testUtf16Le() {
+ Assert.assertEquals("UTF-16LE", RequiredCharsetNames.UTF_16LE);
+ }
+
+ public void testUtf8() {
+ Assert.assertEquals("UTF-8", RequiredCharsetNames.UTF_8);
+ }
+
+}
Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java?rev=796922&r1=796921&r2=796922&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java Thu Jul 23 01:46:50 2009
@@ -1,332 +1,332 @@
-/*
- * 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.commons.codec.binary;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.util.Arrays;
-
-import junit.framework.TestCase;
-
-/**
- * @author Apache Software Foundation
- * @version $Id $
- * @since 1.4
- */
-public class Base64InputStreamTest extends TestCase {
-
- private final static byte[] CRLF = {(byte) '\r', (byte) '\n'};
-
- private final static byte[] LF = {(byte) '\n'};
-
- private static final String STRING_FIXTURE = "Hello World";
-
- /**
- * Construct a new instance of this test case.
- *
- * @param name
- * Name of the test case
- */
- public Base64InputStreamTest(String name) {
- super(name);
- }
-
- /**
- * Tests the Base64InputStream implementation against empty input.
- *
- * @throws Exception
- * for some failure scenarios.
- */
- public void testBase64EmptyInputStream() throws Exception {
- byte[] emptyEncoded = new byte[0];
- byte[] emptyDecoded = new byte[0];
- testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
- testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
- }
-
- /**
- * Tests the Base64InputStream implementation.
- *
- * @throws Exception
- * for some failure scenarios.
- */
- public void testBase64InputStreamByChunk() throws Exception {
- // Hello World test.
- byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
- byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
- testByChunk(encoded, decoded, 76, CRLF);
-
- // Single Byte test.
- encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
- decoded = new byte[]{(byte) 0};
- testByChunk(encoded, decoded, 76, CRLF);
-
- // OpenSSL interop test.
- encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
- decoded = Base64TestData.DECODED;
- testByChunk(encoded, decoded, 64, LF);
-
- // Single Line test.
- String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
- encoded = StringBytesUtils.getBytesUtf8(singleLine);
- decoded = Base64TestData.DECODED;
- testByChunk(encoded, decoded, 0, LF);
-
- // test random data of sizes 0 thru 150
- for (int i = 0; i <= 150; i++) {
- byte[][] randomData = Base64TestData.randomData(i, false);
- encoded = randomData[1];
- decoded = randomData[0];
- testByChunk(encoded, decoded, 0, LF);
- }
- }
-
- /**
- * Tests the Base64InputStream implementation.
- *
- * @throws Exception
- * for some failure scenarios.
- */
- public void testBase64InputStreamByteByByte() throws Exception {
- // Hello World test.
- byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
- byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
- testByteByByte(encoded, decoded, 76, CRLF);
-
- // Single Byte test.
- encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
- decoded = new byte[]{(byte) 0};
- testByteByByte(encoded, decoded, 76, CRLF);
-
- // OpenSSL interop test.
- encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
- decoded = Base64TestData.DECODED;
- testByteByByte(encoded, decoded, 64, LF);
-
- // Single Line test.
- String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
- encoded = StringBytesUtils.getBytesUtf8(singleLine);
- decoded = Base64TestData.DECODED;
- testByteByByte(encoded, decoded, 0, LF);
-
- // test random data of sizes 0 thru 150
- for (int i = 0; i <= 150; i++) {
- byte[][] randomData = Base64TestData.randomData(i, false);
- encoded = randomData[1];
- decoded = randomData[0];
- testByteByByte(encoded, decoded, 0, LF);
- }
- }
-
- /**
- * Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
- * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
- * <p/>
- * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64InputStream wraps itself in encode and decode mode
- * over and over again.
- *
- * @param encoded
- * base64 encoded data
- * @param decoded
- * the data from above, but decoded
- * @param chunkSize
- * chunk size (line-length) of the base64 encoded data.
- * @param seperator
- * Line separator in the base64 encoded data.
- * @throws Exception
- * Usually signifies a bug in the Base64 commons-codec implementation.
- */
- private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
-
- // Start with encode.
- InputStream in = new ByteArrayInputStream(decoded);
- in = new Base64InputStream(in, true, chunkSize, seperator);
- byte[] output = Base64TestData.streamToBytes(in);
-
- assertEquals("EOF", -1, in.read());
- assertEquals("Still EOF", -1, in.read());
- assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
-
- // Now let's try decode.
- in = new ByteArrayInputStream(encoded);
- in = new Base64InputStream(in);
- output = Base64TestData.streamToBytes(in);
-
- assertEquals("EOF", -1, in.read());
- assertEquals("Still EOF", -1, in.read());
- assertTrue("Streaming base64 decode", Arrays.equals(output, decoded));
-
- // I always wanted to do this! (wrap encoder with decoder etc etc).
- in = new ByteArrayInputStream(decoded);
- for (int i = 0; i < 10; i++) {
- in = new Base64InputStream(in, true, chunkSize, seperator);
- in = new Base64InputStream(in, false);
- }
- output = Base64TestData.streamToBytes(in);
-
- assertEquals("EOF", -1, in.read());
- assertEquals("Still EOF", -1, in.read());
- assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
- }
-
- /**
- * Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
- * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
- * <p/>
- * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64InputStream wraps itself in encode and decode mode
- * over and over again.
- *
- * @param encoded
- * base64 encoded data
- * @param decoded
- * the data from above, but decoded
- * @param chunkSize
- * chunk size (line-length) of the base64 encoded data.
- * @param seperator
- * Line separator in the base64 encoded data.
- * @throws Exception
- * Usually signifies a bug in the Base64 commons-codec implementation.
- */
- private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
-
- // Start with encode.
- InputStream in = new ByteArrayInputStream(decoded);
- in = new Base64InputStream(in, true, chunkSize, seperator);
- byte[] output = new byte[encoded.length];
- for (int i = 0; i < output.length; i++) {
- output[i] = (byte) in.read();
- }
-
- assertEquals("EOF", -1, in.read());
- assertEquals("Still EOF", -1, in.read());
- assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
-
- // Now let's try decode.
- in = new ByteArrayInputStream(encoded);
- in = new Base64InputStream(in);
- output = new byte[decoded.length];
- for (int i = 0; i < output.length; i++) {
- output[i] = (byte) in.read();
- }
-
- assertEquals("EOF", -1, in.read());
- assertEquals("Still EOF", -1, in.read());
- assertTrue("Streaming base64 decode", Arrays.equals(output, decoded));
-
- // I always wanted to do this! (wrap encoder with decoder etc etc).
- in = new ByteArrayInputStream(decoded);
- for (int i = 0; i < 10; i++) {
- in = new Base64InputStream(in, true, chunkSize, seperator);
- in = new Base64InputStream(in, false);
- }
- output = new byte[decoded.length];
- for (int i = 0; i < output.length; i++) {
- output[i] = (byte) in.read();
- }
-
- assertEquals("EOF", -1, in.read());
- assertEquals("Still EOF", -1, in.read());
- assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
- }
-
- /**
- * Tests markSupported.
- *
- * @throws Exception
- */
- public void testMarkSupported() throws Exception {
- byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
- ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
- Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
- // Always returns false for now.
- assertFalse("Base64InputStream.markSupported() is false", in.markSupported());
- }
-
- /**
- * Tests read returning 0
- *
- * @throws Exception
- */
- public void testRead0() throws Exception {
- byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
- byte[] buf = new byte[1024];
- int bytesRead = 0;
- ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
- Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
- bytesRead = in.read(buf, 0, 0);
- assertEquals("Base64InputStream.read(buf, 0, 0) returns 0", 0, bytesRead);
- }
-
- /**
- * Tests read with null.
- *
- * @throws Exception
- * for some failure scenarios.
- */
- public void testReadNull() throws Exception {
- byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
- ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
- Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
- try {
- in.read(null, 0, 0);
- fail("Base64InputStream.read(null, 0, 0) to throw a NullPointerException");
- } catch (NullPointerException e) {
- // Expected
- }
- }
-
- /**
- * Tests read throwing IndexOutOfBoundsException
- *
- * @throws Exception
- */
- public void testReadOutOfBounds() throws Exception {
- byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
- byte[] buf = new byte[1024];
- ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
- Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
-
- try {
- in.read(buf, -1, 0);
- fail("Expected Base64InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, 0, -1);
- fail("Expected Base64InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length + 1, 0);
- fail("Base64InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length - 1, 2);
- fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
- } catch (IndexOutOfBoundsException e) {
- // Expected
- }
- }
-}
+/*
+ * 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.commons.codec.binary;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Apache Software Foundation
+ * @version $Id $
+ * @since 1.4
+ */
+public class Base64InputStreamTest extends TestCase {
+
+ private final static byte[] CRLF = {(byte) '\r', (byte) '\n'};
+
+ private final static byte[] LF = {(byte) '\n'};
+
+ private static final String STRING_FIXTURE = "Hello World";
+
+ /**
+ * Construct a new instance of this test case.
+ *
+ * @param name
+ * Name of the test case
+ */
+ public Base64InputStreamTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Tests the Base64InputStream implementation against empty input.
+ *
+ * @throws Exception
+ * for some failure scenarios.
+ */
+ public void testBase64EmptyInputStream() throws Exception {
+ byte[] emptyEncoded = new byte[0];
+ byte[] emptyDecoded = new byte[0];
+ testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
+ testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
+ }
+
+ /**
+ * Tests the Base64InputStream implementation.
+ *
+ * @throws Exception
+ * for some failure scenarios.
+ */
+ public void testBase64InputStreamByChunk() throws Exception {
+ // Hello World test.
+ byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
+ testByChunk(encoded, decoded, 76, CRLF);
+
+ // Single Byte test.
+ encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
+ decoded = new byte[]{(byte) 0};
+ testByChunk(encoded, decoded, 76, CRLF);
+
+ // OpenSSL interop test.
+ encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
+ decoded = Base64TestData.DECODED;
+ testByChunk(encoded, decoded, 64, LF);
+
+ // Single Line test.
+ String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
+ encoded = StringBytesUtils.getBytesUtf8(singleLine);
+ decoded = Base64TestData.DECODED;
+ testByChunk(encoded, decoded, 0, LF);
+
+ // test random data of sizes 0 thru 150
+ for (int i = 0; i <= 150; i++) {
+ byte[][] randomData = Base64TestData.randomData(i, false);
+ encoded = randomData[1];
+ decoded = randomData[0];
+ testByChunk(encoded, decoded, 0, LF);
+ }
+ }
+
+ /**
+ * Tests the Base64InputStream implementation.
+ *
+ * @throws Exception
+ * for some failure scenarios.
+ */
+ public void testBase64InputStreamByteByByte() throws Exception {
+ // Hello World test.
+ byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
+ testByteByByte(encoded, decoded, 76, CRLF);
+
+ // Single Byte test.
+ encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
+ decoded = new byte[]{(byte) 0};
+ testByteByByte(encoded, decoded, 76, CRLF);
+
+ // OpenSSL interop test.
+ encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
+ decoded = Base64TestData.DECODED;
+ testByteByByte(encoded, decoded, 64, LF);
+
+ // Single Line test.
+ String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
+ encoded = StringBytesUtils.getBytesUtf8(singleLine);
+ decoded = Base64TestData.DECODED;
+ testByteByByte(encoded, decoded, 0, LF);
+
+ // test random data of sizes 0 thru 150
+ for (int i = 0; i <= 150; i++) {
+ byte[][] randomData = Base64TestData.randomData(i, false);
+ encoded = randomData[1];
+ decoded = randomData[0];
+ testByteByByte(encoded, decoded, 0, LF);
+ }
+ }
+
+ /**
+ * Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
+ * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
+ * <p/>
+ * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64InputStream wraps itself in encode and decode mode
+ * over and over again.
+ *
+ * @param encoded
+ * base64 encoded data
+ * @param decoded
+ * the data from above, but decoded
+ * @param chunkSize
+ * chunk size (line-length) of the base64 encoded data.
+ * @param seperator
+ * Line separator in the base64 encoded data.
+ * @throws Exception
+ * Usually signifies a bug in the Base64 commons-codec implementation.
+ */
+ private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
+
+ // Start with encode.
+ InputStream in = new ByteArrayInputStream(decoded);
+ in = new Base64InputStream(in, true, chunkSize, seperator);
+ byte[] output = Base64TestData.streamToBytes(in);
+
+ assertEquals("EOF", -1, in.read());
+ assertEquals("Still EOF", -1, in.read());
+ assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
+
+ // Now let's try decode.
+ in = new ByteArrayInputStream(encoded);
+ in = new Base64InputStream(in);
+ output = Base64TestData.streamToBytes(in);
+
+ assertEquals("EOF", -1, in.read());
+ assertEquals("Still EOF", -1, in.read());
+ assertTrue("Streaming base64 decode", Arrays.equals(output, decoded));
+
+ // I always wanted to do this! (wrap encoder with decoder etc etc).
+ in = new ByteArrayInputStream(decoded);
+ for (int i = 0; i < 10; i++) {
+ in = new Base64InputStream(in, true, chunkSize, seperator);
+ in = new Base64InputStream(in, false);
+ }
+ output = Base64TestData.streamToBytes(in);
+
+ assertEquals("EOF", -1, in.read());
+ assertEquals("Still EOF", -1, in.read());
+ assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
+ }
+
+ /**
+ * Tests method does three tests on the supplied data: 1. encoded ---[DECODE]--> decoded 2. decoded ---[ENCODE]-->
+ * encoded 3. decoded ---[WRAP-WRAP-WRAP-etc...] --> decoded
+ * <p/>
+ * By "[WRAP-WRAP-WRAP-etc...]" we mean situation where the Base64InputStream wraps itself in encode and decode mode
+ * over and over again.
+ *
+ * @param encoded
+ * base64 encoded data
+ * @param decoded
+ * the data from above, but decoded
+ * @param chunkSize
+ * chunk size (line-length) of the base64 encoded data.
+ * @param seperator
+ * Line separator in the base64 encoded data.
+ * @throws Exception
+ * Usually signifies a bug in the Base64 commons-codec implementation.
+ */
+ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
+
+ // Start with encode.
+ InputStream in = new ByteArrayInputStream(decoded);
+ in = new Base64InputStream(in, true, chunkSize, seperator);
+ byte[] output = new byte[encoded.length];
+ for (int i = 0; i < output.length; i++) {
+ output[i] = (byte) in.read();
+ }
+
+ assertEquals("EOF", -1, in.read());
+ assertEquals("Still EOF", -1, in.read());
+ assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
+
+ // Now let's try decode.
+ in = new ByteArrayInputStream(encoded);
+ in = new Base64InputStream(in);
+ output = new byte[decoded.length];
+ for (int i = 0; i < output.length; i++) {
+ output[i] = (byte) in.read();
+ }
+
+ assertEquals("EOF", -1, in.read());
+ assertEquals("Still EOF", -1, in.read());
+ assertTrue("Streaming base64 decode", Arrays.equals(output, decoded));
+
+ // I always wanted to do this! (wrap encoder with decoder etc etc).
+ in = new ByteArrayInputStream(decoded);
+ for (int i = 0; i < 10; i++) {
+ in = new Base64InputStream(in, true, chunkSize, seperator);
+ in = new Base64InputStream(in, false);
+ }
+ output = new byte[decoded.length];
+ for (int i = 0; i < output.length; i++) {
+ output[i] = (byte) in.read();
+ }
+
+ assertEquals("EOF", -1, in.read());
+ assertEquals("Still EOF", -1, in.read());
+ assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
+ }
+
+ /**
+ * Tests markSupported.
+ *
+ * @throws Exception
+ */
+ public void testMarkSupported() throws Exception {
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
+ ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+ Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
+ // Always returns false for now.
+ assertFalse("Base64InputStream.markSupported() is false", in.markSupported());
+ }
+
+ /**
+ * Tests read returning 0
+ *
+ * @throws Exception
+ */
+ public void testRead0() throws Exception {
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
+ byte[] buf = new byte[1024];
+ int bytesRead = 0;
+ ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+ Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
+ bytesRead = in.read(buf, 0, 0);
+ assertEquals("Base64InputStream.read(buf, 0, 0) returns 0", 0, bytesRead);
+ }
+
+ /**
+ * Tests read with null.
+ *
+ * @throws Exception
+ * for some failure scenarios.
+ */
+ public void testReadNull() throws Exception {
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
+ ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+ Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
+ try {
+ in.read(null, 0, 0);
+ fail("Base64InputStream.read(null, 0, 0) to throw a NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ }
+
+ /**
+ * Tests read throwing IndexOutOfBoundsException
+ *
+ * @throws Exception
+ */
+ public void testReadOutOfBounds() throws Exception {
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
+ byte[] buf = new byte[1024];
+ ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
+ Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
+
+ try {
+ in.read(buf, -1, 0);
+ fail("Expected Base64InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ try {
+ in.read(buf, 0, -1);
+ fail("Expected Base64InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ try {
+ in.read(buf, buf.length + 1, 0);
+ fail("Base64InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+
+ try {
+ in.read(buf, buf.length - 1, 2);
+ fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
+ } catch (IndexOutOfBoundsException e) {
+ // Expected
+ }
+ }
+}
Propchange: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
------------------------------------------------------------------------------
svn:eol-style = native