| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.flatfile; |
| 18 | |
|
| 19 | |
import java.io.ByteArrayInputStream; |
| 20 | |
import java.io.InputStream; |
| 21 | |
import java.util.ArrayList; |
| 22 | |
|
| 23 | |
import org.apache.commons.flatfile.util.ConcatenatedInputStream; |
| 24 | |
import org.apache.commons.flatfile.util.RepeatingInputStream; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 128 | public abstract class PadJustifyFieldSupport extends FieldSupport { |
| 31 | |
|
| 32 | |
private static final long serialVersionUID = -4953059253157670418L; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 8 | public static enum Justify implements FieldOption { |
| 38 | |
|
| 39 | 1 | LEFT { |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 1 | protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) { |
| 44 | 104 | InputStream is = new ByteArrayInputStream(src); |
| 45 | 104 | int dlen = dest.getPadJustifyLength(); |
| 46 | 104 | return dlen <= src.length ? is : new ConcatenatedInputStream(is, |
| 47 | |
RepeatingInputStream.getInstance(dest.getPad(), dlen - src.length)); |
| 48 | |
} |
| 49 | |
}, |
| 50 | |
|
| 51 | |
|
| 52 | 1 | RIGHT { |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 1 | protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) { |
| 57 | 12 | int dlen = dest.getPadJustifyLength(); |
| 58 | 12 | if (dlen < src.length) { |
| 59 | 3 | return new ByteArrayInputStream(src, src.length - dlen, dlen); |
| 60 | |
} |
| 61 | 9 | InputStream is = new ByteArrayInputStream(src); |
| 62 | 9 | return dlen == src.length ? is : new ConcatenatedInputStream(RepeatingInputStream |
| 63 | |
.getInstance(dest.getPad(), dlen - src.length), is); |
| 64 | |
} |
| 65 | |
}, |
| 66 | |
|
| 67 | |
|
| 68 | 1 | CENTER { |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 1 | protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) { |
| 73 | 5 | int dlen = dest.getPadJustifyLength(); |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 5 | if (dlen < src.length) { |
| 78 | 2 | return new ByteArrayInputStream(src, (src.length - dlen) / 2, dlen); |
| 79 | |
} |
| 80 | 3 | InputStream is = new ByteArrayInputStream(src); |
| 81 | 3 | if (dlen == src.length) { |
| 82 | 0 | return is; |
| 83 | |
} |
| 84 | 3 | ArrayList<InputStream> l = new ArrayList<InputStream>(3); |
| 85 | 3 | byte pad = dest.getPad(); |
| 86 | 3 | l.add(RepeatingInputStream.getInstance(pad, (dlen - src.length) / 2)); |
| 87 | 3 | l.add(is); |
| 88 | 3 | l.add(RepeatingInputStream.getInstance(pad)); |
| 89 | 3 | return new ConcatenatedInputStream(l); |
| 90 | |
} |
| 91 | |
}; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
protected abstract InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest); |
| 100 | |
} |
| 101 | |
|
| 102 | 128 | private byte pad = 0x20; |
| 103 | |
private Justify justify; |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public Justify getJustify() { |
| 110 | 163 | return justify == null ? Justify.LEFT : justify; |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public void setJustify(Justify justify) { |
| 118 | 20 | this.justify = justify; |
| 119 | 20 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public byte getPad() { |
| 126 | 53 | return pad; |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public void setPad(byte pad) { |
| 134 | 16 | this.pad = pad; |
| 135 | 16 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public void setValue(byte[] b) { |
| 142 | 121 | dieOnExceptionRead(getJustify().getInputStream(b, this)); |
| 143 | 121 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
protected abstract int getPadJustifyLength(); |
| 150 | |
} |