java - Limit number of bytes written in a PrintStream -
java - Limit number of bytes written in a PrintStream -
i'm running unsecure code have set stdout
, stderr
streams filestream
s wrapped in printstream
s. (standard output/error must redirected.)
is there way configure redirected filestream
s/printstream
s set maximum of 10 mb written, that, example,
while (true) system.out.write("lots of bytes");
doesn't write excessive amounts of info server's disk.
the code does have time limit of 15s, i'd separate guard here.
one way define filteroutputstream
wrap file stream in, keeps internal counter increments on every write
, , after reaching set threshold, starts throwing exceptions
or ignores writes.
something along lines of:
class="lang-java prettyprint-override">import java.io.*; class limitoutputstream extends filteroutputstream{ private long limit; public limitoutputstream(outputstream out,long limit){ super(out); this.limit = limit; } public void write(byte[]b) throws ioexception{ long left = math.min(b.length,limit); if (left<=0) return; limit-=left; out.write(b, 0, (int)left); } public void write(int b) throws ioexception{ if (limit<=0) return; limit--; out.write(b); } public void write(byte[]b,int off, int len) throws ioexception{ long left = math.min(len,limit); if (left<=0) return; limit-=left; out.write(b,off,(int)left); } }
java security
Comments
Post a Comment