package uk.co.mmscomputing.device.printmonitor; import java.io.*; abstract public class PMOutputStream extends FilterOutputStream{ private String cmd; // buffer for current command sent by printer driver private int count; // count of scan line data still to come protected int pagewidth=0; // physical page width protected int pageheight=0; // physical page height protected int graphicsxres=0; protected int graphicsyres=0; protected int numofdatabytes=0; // number of data send in one block // print driver sends only one scanline per block // =>(numofdatabytes*8<=pagewidth)has to be true public PMOutputStream(OutputStream out){ super(out);cmd="";count=0; } abstract protected void startDoc()throws IOException; abstract protected void startPage()throws IOException; protected void sendBlockData()throws IOException{ count=numofdatabytes; // what follows now is 'numofdatabytes' of image data } abstract protected void writeDataByte(int code)throws IOException; protected void endBlockData()throws IOException{ int len=pagewidth>>3; for(int i=numofdatabytes;ipagewidth){ System.err.println("Number of data bytes ["+numofdatabytes+"*8] is greater than pagewidth ["+pagewidth+"]"); } }else if(cmd.startsWith("SendBlockData")){ sendBlockData(); }else if(cmd.startsWith("EndBlockData")){ endBlockData(); }else if(cmd.startsWith("EndPage")){ // EndPage endPage(); }else if(cmd.startsWith("EndJob")){ // EndJob endJob(); } } protected void addCmdChar(int code)throws IOException{ code&=0x00FF; // otherwise have printer commands if(code==';'){ processCmd(cmd);cmd=""; // System.out.println(new String(cmd.getBytes())); }else{ cmd+=(char)code; } } public void write(int code)throws IOException{ if(count>0){ // if have data bytes write image data if(count<=(pagewidth>>3)){ // if we have more bytes per line than space then skip first bytes of data writeDataByte(code); } count--; }else{ addCmdChar(code); } } }