importjava.io.IOException;importjava.io.InputStream;/**
* Read fixed number of bytes from a file
* @author Ethicist
*/publicclassFirstByteReader{privateStringpath;InputStreamstream=null;publicbyte[]readBytes(Stringpath,intsize,booleanallowless){bytetemp[]=newbyte[size];intlength=0;this.path=path;stream=this.getClass().getResourceAsStream(path);try{intread=stream.read();while(length<size){read=stream.read(temp,length,size-length);if(read==-1){if(allowless){bytebuffer[]=newbyte[length];System.arraycopy(temp,0,buffer,0,length);// return a smaller array, so the caller// can tell how much they really gotreturnbuffer;}else{// something goes wrongif(length>0){echo("EOF while reading data");}else{thrownewIOException();}}}length+=read;}}catch(IOExceptioni){showExceptionMessage(i,"FileInputReader: readBytes IOException: ");}echo("result is: "+newString(temp));returntemp;}protectedvoidecho(Strings){System.out.println(s);}publicstaticFirstByteReadergetInstance(){returnnewFirstByteReader();}privatevoidshowExceptionMessage(Exceptione,Stringmessage){System.err.println("O b o s r a l s y a !");Stringerr=e.getMessage()+"\n"+e.toString();System.out.println(message+err+"\n");e.printStackTrace();}}