开发者

How to delete buffered data in a buffered OutputStream?

开发者 https://www.devze.com 2023-03-24 00:51 出处:网络
It is possible to skip data from an InputStream in.skip(in.available()); but if you want to do something similar with OutputStream I\'ve found

It is possible to skip data from an InputStream

in.skip(in.available());

but if you want to do something similar with OutputStream I've found

socket.getOutputStream().flush();

But that's not the same, flush will transmit the buffered data instead of ignoring it.

Is there any possibility of deleting buffered data?

Thanks

EDIT

The situation is a client-server application, when a new command is send (from client) it (try) to be sure that the answer read will correspond to the last command sent.

Some commands are sent by (human-fired) events, and others are sent by automatic threads. If a command is on buffer and a new one is send then the answer will be for the first one, cau开发者_如何学编程sing desynchronization.

Of course a synchronized method plus a flag called "waitingCommand" could be the safer approach but as the communication is not reliable, this approach is slow (depends on timeouts ). That's why I've asked for the skip method.


You can't remove data you could have sent. You can write the data into an in-memory OutputStream like ByteArrayOutputStream and copy only the portions you want.


I'm no sure if it makes sense, but you can try:

class MyBufferedOutputStream extends java.io.BufferedOutputStream {

    public MyBufferedOutputStream(OutputStream out) {
        super(out);
    }

    /** throw away everything in a buffer without writing it */
    public synchronized void skip() {
        count = 0; 
    }

}


What does it mean to "skip" outputting data?

Once the data is in the buffer, there's no way to get it back or remove it. I suggest checking if you want to skip the data before you write it to the OutputStream. Either that, or have your own secondary buffer that you can modify at will.


This question doesn't make any sense. Throwing away pending requests will just make your application protocol problem worse. What happens to the guy that is waiting for the response to the request that got deleted? What happened to the functionality that that request was supposed to implement? You need to rethink all this from another point of view. If you have a single connection to a server that is executing request/response transactions for this client, the protocol is already sequential. You will have to synchronize on e.g. the socket at the point of writing & flushing the request and reading the response, but you're not losing any performance by this as the processing at the other end is sequentialized anyway. You don't need a 'waitingCommand' flag as well, just synchronization.


Since you are controlling the data written to OutputStream, just don't write pieces that you don't need. OutputStream by contract, does not ensure when data is actually written, so it doesn't make much sense to have skip method.


The best you can do to "ignore" output data, is not to write it at first.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号