package QuickVerifyCrap; import com.fazecast.jSerialComm.SerialPort; import javax.sql.rowset.serial.SQLOutputImpl; import java.io.*; enum NextChunkAction { WAIT, REPEAT, CONTINUE, FINISHED } public class SerialSpammer { static InputStream in = null; static OutputStream o = null; static NextChunkAction nextChunkReady = NextChunkAction.WAIT; static int waitCtr = 0; static Object workaround = new Object(); public static void main(String[] args) { boolean startedStreamPlayback = false; System.out.println("Gobblygob"); RandomAccessFile streamfile = null; int sendOffset = 0; SerialPort comPort = SerialPort.getCommPort("COM20"); comPort.openPort(); comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0); in = comPort.getInputStream(); o = comPort.getOutputStream(); try { new Thread(()-> { System.out.println("MunchMunchMunch"); try { while (true) { if(in.available()<1) { continue; } int read = in.read(); if (read == '~') { System.out.println("RSME"); nextChunkReady = NextChunkAction.CONTINUE; synchronized (workaround) { System.out.println("POKE"); workaround.notify(); } } if (read == '<') { nextChunkReady = NextChunkAction.REPEAT; synchronized (workaround) { System.out.println("POKE"); workaround.notify(); } } System.out.write(read); System.out.flush(); } } catch (IOException e) { e.printStackTrace(); } }).start(); while(true) { if(System.in.available()>0) { int chara = System.in.read(); if(chara ==-1) { System.out.println("ZĂ–INKS"); return; } if(chara == '>') { char[] filepath = new char[4096]; int pathptr = 0; chara = System.in.read(); while(chara != '\n'&&pathptr<4096) { filepath[pathptr]=(char)chara; pathptr++; chara = System.in.read(); } if(pathptr==4096) { System.out.println("Zarf, pathdork"); } else { String path = new String(filepath).split("\u0000")[0]; System.out.println("Streaming: "+path); if(new File(path).exists()) { streamfile = new RandomAccessFile(path,"r"); nextChunkReady=NextChunkAction.CONTINUE; synchronized (workaround) { System.out.println("POKE"); workaround.notify(); } } } } else { //System.out.println("Fuick"); o.write(chara); o.flush(); if(chara=='\n') { System.out.println("waitctr="+waitCtr); } //System.out.println("ZilseZilse"); } } if(streamfile!=null) { if(streamfile.getFilePointer()>=streamfile.length()) { System.out.println("FINI"); System.err.println("FINI"); streamfile.close(); streamfile=null; startedStreamPlayback=false; } else { if((!startedStreamPlayback)&&(streamfile.getFilePointer()==8192||streamfile.getFilePointer()==streamfile.length())) { System.out.println("PLAY"); o.write('|'); startedStreamPlayback=true; } switch(nextChunkReady) { case REPEAT: System.out.println("RTRY=("+streamfile.getFilePointer()+")"); waitCtr=0; if(streamfile.getFilePointer()>=64) { streamfile.seek(streamfile.getFilePointer()-64); } case CONTINUE: waitCtr=0; System.out.println("STR=("+streamfile.getFilePointer()+")"); byte[] readbfr = new byte[64]; streamfile.read(readbfr); o.write('>'); nextChunkReady=NextChunkAction.WAIT; o.write(readbfr); break; } synchronized (workaround) { if(nextChunkReady==NextChunkAction.WAIT) { System.out.println("GOTO WAIT"); workaround.wait(); } } } } } } catch (Exception e) { e.printStackTrace(); } comPort.closePort(); } }