I'm implementing a logging system that needs to encode the log messages with GZIP and send them off by UDP.
What I've got so far is:
Initialization:
DatagramSocket sock = new DatagramSocket();
baos = new ByteArrayOutputStream();
printStream = new PrintStream(new GZIPOutputStream(baos));
This printStream is then passed out of the logger - messages will arrive through it
Then every time a message arrives:
byte[] d = baos.toByteArray();
DatagramPacket dp = new DatagramPacket(d,d.length,host,port);
sock.send(dp);
What stumps me currently is that I can't find a way to remove the data from the ByteArrayOutputStream (toByteArray() only takes a copy) and I'm afraid that recreating all three stream objects every time will be inefficient.
Is there some way to remove sent data from the stream? Or should I look in another direction entirely?