Author: mbenson Date: Thu Mar 15 22:14:40 2012 New Revision: 1301242 URL: http://svn.apache.org/viewvc?rev=1301242&view=rev Log: update APIs for RepeatingInputStream and ConcatenatedInputStream for varargs; simplify things using lang3 Validata; some final variables
Modified: commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntitySupport.java commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/PadJustifyFieldSupport.java commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/ConcatenatedInputStream.java commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/RepeatingInputStream.java Modified: commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntitySupport.java URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntitySupport.java?rev=1301242&r1=1301241&r2=1301242&view=diff ============================================================================== --- commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntitySupport.java (original) +++ commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntitySupport.java Thu Mar 15 22:14:40 2012 @@ -33,7 +33,7 @@ public abstract class EntitySupport impl * {@inheritDoc} */ public void fill(byte b) throws IOException { - readFrom(RepeatingInputStream.getInstance(b)); + readFrom(RepeatingInputStream.of(b)); } /** @@ -49,13 +49,14 @@ public abstract class EntitySupport impl /** * {@inheritDoc} + * Naive implementation which can and should be overridden by subclasses. */ public byte[] getValue(int offset, int length) { if (offset + length > length()) { throw new IndexOutOfBoundsException(); } - byte[] value = getValue(); - byte[] result = new byte[length]; + final byte[] value = getValue(); + final byte[] result = new byte[length]; System.arraycopy(value, offset, result, 0, length); return result; } @@ -67,7 +68,7 @@ public abstract class EntitySupport impl if (offset + length > b.length) { throw new IndexOutOfBoundsException(); } - byte[] subset = new byte[length]; + final byte[] subset = new byte[length]; System.arraycopy(b, offset, subset, 0, length); setValue(subset); } Modified: commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/PadJustifyFieldSupport.java URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/PadJustifyFieldSupport.java?rev=1301242&r1=1301241&r2=1301242&view=diff ============================================================================== --- commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/PadJustifyFieldSupport.java (original) +++ commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/PadJustifyFieldSupport.java Thu Mar 15 22:14:40 2012 @@ -18,7 +18,6 @@ package org.apache.commons.flatfile; import java.io.ByteArrayInputStream; import java.io.InputStream; -import java.util.ArrayList; import org.apache.commons.flatfile.util.ConcatenatedInputStream; import org.apache.commons.flatfile.util.RepeatingInputStream; @@ -32,6 +31,11 @@ public abstract class PadJustifyFieldSup private static final long serialVersionUID = -4953059253157670418L; /** + * Default pad. + */ + public static final byte DEFAULT_PAD = 0x20; + + /** * Justify enumerated type. */ public enum Justify implements FieldOption { @@ -41,10 +45,11 @@ public abstract class PadJustifyFieldSup * {@inheritDoc} */ protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) { - InputStream is = new ByteArrayInputStream(src); - int dlen = dest.getPadJustifyLength(); - return dlen <= src.length ? is : new ConcatenatedInputStream(is, - RepeatingInputStream.getInstance(dest.getPad(), dlen - src.length)); + final InputStream is = new ByteArrayInputStream(src); + final int dlen = dest.getPadJustifyLength(); + return dlen <= src.length ? is : new ConcatenatedInputStream( + is, RepeatingInputStream.withLimit(dlen - src.length, + dest.getPad())); } }, @@ -54,13 +59,15 @@ public abstract class PadJustifyFieldSup * {@inheritDoc} */ protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) { - int dlen = dest.getPadJustifyLength(); + final int dlen = dest.getPadJustifyLength(); if (dlen < src.length) { - return new ByteArrayInputStream(src, src.length - dlen, dlen); + return new ByteArrayInputStream(src, src.length - dlen, + dlen); } - InputStream is = new ByteArrayInputStream(src); - return dlen == src.length ? is : new ConcatenatedInputStream(RepeatingInputStream - .getInstance(dest.getPad(), dlen - src.length), is); + final InputStream is = new ByteArrayInputStream(src); + return dlen == src.length ? is : new ConcatenatedInputStream( + RepeatingInputStream.withLimit(dlen - src.length, + dest.getPad()), is); } }, @@ -70,23 +77,23 @@ public abstract class PadJustifyFieldSup * {@inheritDoc} */ protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) { - int dlen = dest.getPadJustifyLength(); + final int dlen = dest.getPadJustifyLength(); // it would be odd and therefore hopefully rare to center a // field that allowed overflow, // but we'll provide for it: if (dlen < src.length) { - return new ByteArrayInputStream(src, (src.length - dlen) / 2, dlen); + return new ByteArrayInputStream(src, + (src.length - dlen) / 2, dlen); } - InputStream is = new ByteArrayInputStream(src); + final InputStream is = new ByteArrayInputStream(src); if (dlen == src.length) { return is; } - ArrayList<InputStream> l = new ArrayList<InputStream>(3); - byte pad = dest.getPad(); - l.add(RepeatingInputStream.getInstance(pad, (dlen - src.length) / 2)); - l.add(is); - l.add(RepeatingInputStream.getInstance(pad)); - return new ConcatenatedInputStream(l); + final byte pad = dest.getPad(); + return new ConcatenatedInputStream( + RepeatingInputStream + .withLimit((dlen - src.length) / 2, pad), + is, RepeatingInputStream.of(pad)); } }; @@ -99,7 +106,7 @@ public abstract class PadJustifyFieldSup protected abstract InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest); } - private byte pad = 0x20; + private byte pad = DEFAULT_PAD; private Justify justify; /** Modified: commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/ConcatenatedInputStream.java URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/ConcatenatedInputStream.java?rev=1301242&r1=1301241&r2=1301242&view=diff ============================================================================== --- commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/ConcatenatedInputStream.java (original) +++ commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/ConcatenatedInputStream.java Thu Mar 15 22:14:40 2012 @@ -18,9 +18,10 @@ package org.apache.commons.flatfile.util import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; import java.util.Iterator; -import org.apache.commons.collections.IteratorUtils; +import org.apache.commons.lang3.Validate; /** * Unified InputStream representation of multiple concatenated InputStreams. @@ -30,14 +31,13 @@ public class ConcatenatedInputStream ext /** EOF */ public static final int EOF = -1; - private static final String NO_NULL = "null arguments are unacceptable"; private static final InputStream AT_EOF = new InputStream() { public int read() throws IOException { return EOF; } }; - private Iterator<InputStream> iter; + private final Iterator<InputStream> iter; private InputStream current; /** @@ -45,26 +45,16 @@ public class ConcatenatedInputStream ext * @param src InputStreams */ public ConcatenatedInputStream(Iterable<InputStream> src) { - if (src == null) { - throw new IllegalArgumentException(NO_NULL); - } - iter = src.iterator(); + this.iter = Validate.notNull(src).iterator(); next(); } /** - * Convenience constructor. - * @param is0 first InputStream - * @param is1 next InputStream + * Create a new ConcatenatedInputStream. + * @param src InputStreams */ - @SuppressWarnings("unchecked") - public ConcatenatedInputStream(InputStream is0, InputStream is1) { - if (is0 == null || is1 == null) { - throw new IllegalArgumentException(NO_NULL); - } - // shortcuts: - current = is0; - iter = IteratorUtils.singletonIterator(is1); + public ConcatenatedInputStream(InputStream... src) { + this(Arrays.asList(Validate.notNull(src))); } /** Modified: commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/RepeatingInputStream.java URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/RepeatingInputStream.java?rev=1301242&r1=1301241&r2=1301242&view=diff ============================================================================== --- commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/RepeatingInputStream.java (original) +++ commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/util/RepeatingInputStream.java Thu Mar 15 22:14:40 2012 @@ -18,24 +18,32 @@ package org.apache.commons.flatfile.util import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; + +import org.apache.commons.lang3.Validate; /** - * Provides factory methods to return InputStreams that return a repeating ordered - * sequence of bytes, optionally limiting output to some maximum total size. - * @version $Revision$ $Date$ + * Provides factory methods to return InputStreams that return a repeating + * ordered sequence of bytes, optionally limiting output to some maximum total + * size. + * + * @version $Revision$ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar + * 2009) $ */ public abstract class RepeatingInputStream { + private RepeatingInputStream() { + } /** * An InputStream that repeats a single byte forever. */ private static class RepeatOneByte extends InputStream { - private byte b; + private final byte b; /** * Create a new RepeatOneByte instance. - * @param b byte to repeat + * + * @param b + * byte to repeat */ private RepeatOneByte(byte b) { this.b = b; @@ -53,12 +61,14 @@ public abstract class RepeatingInputStre * An InputStream that repeats a byte[] forever. */ private static class RepeatArray extends InputStream { - private byte[] b; + private final byte[] b; private int pos; /** * Create a new RepeatArray instance. - * @param b byte[] to repeat + * + * @param b + * byte[] to repeat */ private RepeatArray(byte[] b) { this.b = b; @@ -84,14 +94,17 @@ public abstract class RepeatingInputStre private static class LimitedOutput extends InputStream { private static final int EOF = -1; - private InputStream source; - private int bytesToReturn; + private final InputStream source; + private final int bytesToReturn; private int bytesReturned; /** * Create a new LimitedOutput instance. - * @param source wrapped InputStream - * @param bytesToReturn int max + * + * @param source + * wrapped InputStream + * @param bytesToReturn + * int max */ private LimitedOutput(InputStream source, int bytesToReturn) { this.source = source; @@ -114,55 +127,49 @@ public abstract class RepeatingInputStre } /** - * Holds cached instances of single-byte RepeatingInputStreams. - */ - // go ahead and init b/c we know we'll want a single-byte instance: - private static HashMap<Byte, InputStream> INSTANCE_MAP = new HashMap<Byte, InputStream>(); - - /** - * Get an InputStream that will return the specified byte forever. - * @param b byte to repeat - * @return InputStream + * Holds cached instances of single-byte RepeatingInputStreams, with enough + * space for every unique byte value. */ - public static synchronized InputStream getInstance(byte b) { - Byte bigByte = new Byte(b); - InputStream result = INSTANCE_MAP.get(bigByte); - if (result == null) { - result = new RepeatOneByte(b); - INSTANCE_MAP.put(bigByte, result); - } - return result; - } + private static InputStream[] REPEAT_BYTE = new InputStream[(int) Math.pow( + 2, Byte.SIZE)]; /** * Get an InputStream that will return the specified byte[] forever. - * @param b byte[] to repeat + * + * @param b + * byte[] to repeat * @return InputStream + * @throws NullPointerException + * if {@code b} is {@code null} */ - public static InputStream getInstance(byte[] b) { - return b.length == 1 ? getInstance(b[0]) : new RepeatArray(b); - } - - /** - * Get an InputStream that will return a single byte a limited number of times. - * @param b byte to repeat - * @param bytesToReturn int - * @return InputStream - */ - public static synchronized InputStream getInstance(byte b, int bytesToReturn) { - return new LimitedOutput(getInstance(b), bytesToReturn); + public static InputStream of(byte... b) { + Validate.notNull(b); + if (b.length == 1) { + final int index = 0 | b[0]; + InputStream result = REPEAT_BYTE[index]; + if (result == null) { + result = new RepeatOneByte(b[0]); + REPEAT_BYTE[index] = result; + } + return result; + } + return new RepeatArray(b); } /** - * Get an InputStream that will return the specified byte sequence - * until a total of <code>bytesToReturn</code> bytes have been returned. - * @param b byte[] to repeat - * @param bytesToReturn int + * Get an InputStream that will return the specified byte sequence until a + * total of {@code limit} bytes have been returned. + * + * @param limit + * int + * @param b + * byte[] to repeat * @return InputStream + * @throws NullPointerException + * if {@code b} is {@code null} */ - public static synchronized InputStream getInstance(byte[] b, - int bytesToReturn) { - return new LimitedOutput(getInstance(b), bytesToReturn); + public static synchronized InputStream withLimit(int limit, byte... b) { + return new LimitedOutput(of(b), limit); } }