- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastIO.java
More file actions
Latest commit
38 lines (38 loc) · 3.46 KB
/
Copy pathFastIO.java
File metadata and controls
38 lines (38 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
importjava.nio.*;
importjava.nio.channels.*;
//Fast IO Template with dependencies
classFSUimplementsFlushable,Closeable{publicReadableByteChannelinputStream;publicWritableByteChanneloutputStream;publicfinalintbufferSize;publicByteBufferreadBuffer;publicByteBufferwriteBuffer;privateintbytesAvailable=0;privatebooleanclosed=false;publicFSU(){this(4096);}
publicFSU(intbuffer){this(System.in,System.out,buffer);}
publicFSU(InputStreamistream,OutputStreamostream,intbuffer){inputStream=Channels.newChannel(istream);outputStream=Channels.newChannel(ostream);bufferSize=buffer;readBuffer=ByteBuffer.allocateDirect(buffer);writeBuffer=ByteBuffer.allocateDirect(buffer);}
publicvoidprintAll(Object...validObjects)throwsIOException{for(inti=0;i<validObjects.length;i++){print(validObjects[i]);}}
publicvoidprintAllLines(Object...validObjects)throwsIOException{for(inti=0;i<validObjects.length;i++){print(validObjects[i]);println();}}
publicvoidflushBuffer()throwsIOException{writeBuffer.flip();outputStream.write(writeBuffer);writeBuffer.clear();}
publicvoidfillBuffer()throwsIOException{readBuffer.clear();bytesAvailable=inputStream.read(readBuffer);readBuffer.position(0);}
publicbytenextByte()throwsIOException{if(closed)thrownewIOException("FastStreamUtilities Is Already Closed");if(readBuffer.position()>=bytesAvailable)fillBuffer();if(bytesAvailable==0)return-1;returnreadBuffer.get();}
publicStringnextToken()throwsIOException{StringBuildersb=newStringBuilder();bytec=nextByte();while(c<=' ')c=nextByte();do{sb.append((char)c);}while((c=nextByte())>' ');returnsb.toString();}
publiclongnextLong()throwsIOException{longret=0;bytec=nextByte();while(c<=' ')
c=nextByte();booleanneg=(c=='-');if(neg)
c=nextByte();do{ret=ret*10+c-'0';}while((c=nextByte())>='0'&&c<='9');if(neg)
return-ret;returnret;}
publicdoublenextDouble()throwsIOException{doubleret=0,div=1;bytec=nextByte();while(c<=' ')
c=nextByte();booleanneg=(c=='-');if(neg)
c=nextByte();do{ret=ret*10+c-'0';}while((c=nextByte())>='0'&&c<='9');if(c=='.'){while((c=nextByte())>='0'&&c<='9'){ret+=(c-'0')/(div*=10);}}
if(neg)
return-ret;returnret;}
publicintnextInt()throwsIOException{intret=0;bytec=nextByte();while(c<=' ')
c=nextByte();booleanneg=(c=='-');if(neg)
c=nextByte();do{ret=ret*10+c-'0';}while((c=nextByte())>='0'&&c<='9');if(neg)
return-ret;returnret;}
publiccharnextChar()throwsIOException{return(char)nextByte();}
publicStringnextLine()throwsIOException{StringBuildersb=newStringBuilder();bytec;while((c=nextByte())!=-1){if(c==10)
break;sb.append((char)c);}
returnsb.toString();}
publicvoidwriteByte(byteb)throwsIOException{if(writeBuffer.position()>=bufferSize){flushBuffer();}
writeBuffer.put(b);}
publicvoidwriteBytes(byte[]arr)throwsIOException{if(writeBuffer.remaining()<arr.length){for(inti=0;i<arr.length;i++){writeByte(arr[i]);}}else{writeBuffer.put(arr);}}
publicvoidprint(Objects)throwsIOException{writeBytes(s.toString().getBytes());}
publicvoidprintln(Objects)throwsIOException{writeBytes(s.toString().getBytes());writeByte((byte)10);}
publicvoidprintln()throwsIOException{writeByte((byte)10);}@Override
publicvoidflush()throwsIOException{flushBuffer();}@Override
publicvoidclose()throwsIOException{flush();inputStream.close();outputStream.close();closed=true;}
}