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