| 1 | 0 | |
| 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.ByteArrayOutputStream; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.InputStream; |
| 23 | |
import java.io.OutputStream; |
| 24 | |
import java.util.Iterator; |
| 25 | |
|
| 26 | |
import org.apache.commons.lang3.ArrayUtils; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 536 | public abstract class EntityCollectionSupport extends EntitySupport implements EntityCollection { |
| 33 | |
|
| 34 | |
private static final long serialVersionUID = 5902476686737324304L; |
| 35 | |
|
| 36 | 6 | private static final byte[] DEFAULT_DELIMITER = ArrayUtils.EMPTY_BYTE_ARRAY; |
| 37 | |
|
| 38 | 402 | private byte[] delim = DEFAULT_DELIMITER; |
| 39 | 402 | private boolean delimAfter = true; |
| 40 | 402 | private boolean suppressEmptyChildren = true; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public final synchronized int length() { |
| 46 | 1344 | int result = 0; |
| 47 | 5084 | for (Iterator<Entity> it = getChildren().iterator(); it.hasNext();) { |
| 48 | 9876 | Entity e = it.next(); |
| 49 | 9876 | if (shouldSuppress(e)) { |
| 50 | 18 | continue; |
| 51 | |
} |
| 52 | 9858 | result += e.length(); |
| 53 | 9858 | if (it.hasNext() || isDelimAfter()) { |
| 54 | 9786 | result += getDelim().length; |
| 55 | |
} |
| 56 | 6572 | } |
| 57 | 1344 | return result; |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public final synchronized void readFrom(InputStream is) throws IOException { |
| 64 | 472 | for (Iterator<Entity> it = getChildren().iterator(); it.hasNext();) { |
| 65 | 936 | Entity e = it.next(); |
| 66 | 936 | if (shouldSuppress(e)) { |
| 67 | 0 | continue; |
| 68 | |
} |
| 69 | 936 | e.readFrom(is); |
| 70 | 936 | if (it.hasNext() || isDelimAfter()) { |
| 71 | 936 | is.skip(getDelim().length); |
| 72 | |
} |
| 73 | 624 | } |
| 74 | 120 | } |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public final synchronized void writeTo(OutputStream os) throws IOException { |
| 80 | 838 | for (Iterator<Entity> it = getChildren().iterator(); it.hasNext();) { |
| 81 | 1152 | Entity e = it.next(); |
| 82 | 1152 | if (shouldSuppress(e)) { |
| 83 | 84 | continue; |
| 84 | |
} |
| 85 | 1068 | e.writeTo(os); |
| 86 | 1068 | if (it.hasNext() || isDelimAfter()) { |
| 87 | 1032 | os.write(getDelim()); |
| 88 | |
} |
| 89 | 712 | } |
| 90 | 336 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public final synchronized byte[] getValue() { |
| 96 | 288 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 97 | |
try { |
| 98 | 288 | writeTo(baos); |
| 99 | 94 | } catch (IOException e) { |
| 100 | 0 | throw new RuntimeException(e); |
| 101 | 188 | } |
| 102 | 282 | return baos.toByteArray(); |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public final synchronized void setValue(byte[] b) { |
| 109 | |
try { |
| 110 | 30 | readFrom(new ByteArrayInputStream(b)); |
| 111 | 10 | } catch (IOException e) { |
| 112 | 0 | throw new RuntimeException(e); |
| 113 | 20 | } |
| 114 | 30 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public final void setValue(byte[] b, int offset, int length) { |
| 120 | |
try { |
| 121 | 0 | readFrom(new ByteArrayInputStream(b, offset, length)); |
| 122 | 0 | } catch (IOException e) { |
| 123 | 0 | throw new RuntimeException(e); |
| 124 | 0 | } |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public boolean isDelimAfter() { |
| 132 | 1854 | return delimAfter; |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public void setDelimAfter(boolean delimAfter) { |
| 141 | 24 | this.delimAfter = delimAfter; |
| 142 | 24 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public byte[] getDelim() { |
| 149 | 11994 | return delim; |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public synchronized void setDelim(byte[] delim) { |
| 157 | 60 | this.delim = delim == null ? DEFAULT_DELIMITER : delim; |
| 158 | 60 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public boolean isSuppressEmptyChildren() { |
| 165 | 12012 | return suppressEmptyChildren; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public void setSuppressEmptyChildren(boolean suppressEmptyChildren) { |
| 173 | 12 | this.suppressEmptyChildren = suppressEmptyChildren; |
| 174 | 12 | } |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
@Override |
| 180 | |
public EntityCollectionSupport clone() { |
| 181 | 690 | return (EntityCollectionSupport) super.clone(); |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
protected boolean shouldSuppress(Entity child) { |
| 190 | 11964 | return isSuppressEmptyChildren() && child.length() == 0; |
| 191 | |
} |
| 192 | |
|
| 193 | |
} |