/**
*
* This class treats a sequence of placeholder StringInfos like a continuous * data area that can be filled out regarding the boundaries. *
* * @author wprinz */ public class SplitStrings { /** * The byte used to fill unused bytes in the placeholders. */ public static final byte FILL_BYTE = 0; /** * The underlying PDF. */ protected byte[] pdf = null; /** * The strings to be filled out. */ protected StringInfo[] strings = null; /** * The current string which is written to. */ protected int cur_string = 0; /** * The current write position within the current string. */ protected int cur_pos = 0; /** * Constructor. * * @param pdf * The underlying PDF. * @param strings * The strings to be filled out. */ public SplitStrings(byte[] pdf, List strings) { this.pdf = pdf; this.strings = new StringInfo[strings.size()]; for (int i = 0; i < strings.size(); i++) { StringInfo si = (StringInfo) strings.get(i); this.strings[i] = si; } } /** * Returns how many bytes are still available in the current string. * * @return Returns the number of bytes that are still available. (positive * integer, or zero if none are available) */ public int getAvailable() { return this.strings[this.cur_string].string_length - this.cur_pos; } /** * Tells, if the whole data would fit into the current string. * * @param data * The data to be matched for fitting * @return Returns true, if the whole data fits, false otherwise. */ public boolean fits(byte[] data) { return getAvailable() >= data.length; } /** * Writes the data into the current string. * ** Note that the data must fit in. *
* @param data The data to be written. */ public void write(byte[] data) { if (!fits(data)) { throw new IllegalArgumentException("The data doesn't fit in."); } System.arraycopy(data, 0, pdf, this.strings[this.cur_string].string_start + this.cur_pos, data.length); this.cur_pos += data.length; } /** * Fills the current string with the fill character and moves on to the next * string. * */ public void newline() { int end = this.strings[this.cur_string].string_start + this.strings[this.cur_string].string_length; for (int i = this.strings[this.cur_string].string_start + this.cur_pos; i < end; i++) { pdf[i] = FILL_BYTE; } this.cur_string++; this.cur_pos = 0; } /** * Fills all rest bytes with the fill character. * ** This should be called when everything is finished to fill all strings properly. *
*/ public void fillRest() { while (this.cur_string < this.strings.length) { newline(); } } /** * Tells, if the current line is valid. * @return Returns true, if this is a line that can be written to. */ public boolean isValidLine () { return this.cur_string < this.strings.length; } }