/** * */ package at.gv.egiz.pdfas.impl.input; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import at.gv.egiz.pdfas.framework.input.TextDataSource; /** * A TextDataSource that keeps the text in memory. * *
* Keeping the text in memory is fast as long as the text is short, but may * result in bad memory performance when the text is longer. Use a FileBased * TextDataSource instead if memory is an issue. *
* * @author wprinz */ public class TextDataSourceImpl implements TextDataSource { /** * The text. */ protected String text = null; /** * Constructor that sets the text. * * @param text * The text. */ public TextDataSourceImpl(String text) { this.text = text; } /** * @see at.gv.egiz.pdfas.framework.input.TextDataSource#getText() */ public String getText() { return this.text; } /** * @see at.gv.egiz.pdfas.framework.input.DataSource#createInputStream() */ public InputStream createInputStream() { try { byte[] data = getText().getBytes("UTF-8"); // PERF: if memory is an issue (e.g. in web), use a FileBased TextDataSource instead. return new ByteArrayInputStream(data); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } /** * @see at.gv.egiz.pdfas.framework.input.DataSource#getLength() */ public int getLength() { try { byte[] data = getText().getBytes("UTF-8"); return data.length; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } /** * @see at.gv.egiz.pdfas.framework.input.DataSource#getAsByteArray() */ public byte[] getAsByteArray() { try { byte[] data = getText().getBytes("UTF-8"); return data; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } }