How can you force a flash to an OutputStream object without closing it?

My question lies in the following assumptions, which, I hope, are true, because I believe in it when I read them, while I have problems with Google:

  • Closing a Socket OutputStream also closes a socket.
  • The flush () method for OutputStream does nothing

Therefore, I basically need to somehow wash the data from my OutputStream object for my application to work.

If you are interested in the details, click on the following links:

. Strange behavior: sending an image from an Android phone to a Java server (working with code)

This issue was resolved by closing the OutputStream. This led to the loss of all data to the other end of the socket and made my application work further, but this fix soon caused problem number 2 - the corresponding socket also closes:

. SocketException - "Socket closes" even if isConnected () returns true

+5
source share
7 answers

You can call the flush method on OutputStream and not close. The specific classes that inherit from OutputStream override flush () to do something other than do anything (write data to a file or send over the network).

+6
source

The flush () method for OutputStream does nothing.

This is not true.

, flush(), OutputStream, . , , , . , flush(), , .

, ( Socket), flush() . ( - - , , .)


FYI, , OutputStream flush() -op, :

  • ; ByteArrayOutputStream,
  • , flush() no-op, .

() API- , OutputStream ( flush() ) . API Java 1.0, Java, , API .

+2

Socket OutputStream

True.

flush() OutputStream

False. . . Javadoc FilterOutputStream.flush(), BufferedOutputStream.flush(), ObjectOutputStream.flush(), .

, , "", β„–2.

+1

? , # , android ( ).

, , Android .

OutputStream str = btSocket.getOutputStream();
str.write(data_byte);
// "This implementation does nothing"
str.flush();

, - - , ()!

0

. . - , "" . , . 1- , out.writeInt, . , .. buffer.length == in.readInt() break loop

    ByteArrayOutputStream dataBuffer = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    byte[] fileBytes;
    int n;
    int length;

    try
    {
        size = in.readInt();

        while((n = in.read(buffer)) != -1)
        {
            dataBuffer.write(buffer, 0, n);

            if(dataBuffer.toByteArray().length == length)
            {
                fileBytes = dataBuffer.toByteArray(); break;
            }
        }
    }
0

. "\n" . flush , destinary ,

0

Android-() ( api 23)

public Socket() {
    this.impl = factory != null ? factory.createSocketImpl() : new PlainSocketImpl();
    this.proxy = null;
}

public class PlainSocketImpl extends SocketImpl {

    @Override protected synchronized OutputStream getOutputStream() throws IOException {
        checkNotClosed();
        return new PlainSocketOutputStream(this);
    }


}

private static class PlainSocketOutputStream extends OutputStream {
        // doesn't override base class flush();
}

:

protected void shutdownOutput() throws IOException

- WRITE.

instead of using the output stream, you can write directly to the file descriptor or by creating your own Socket implementation with an OutputStream that will override the flush method (for example, using the Berkeley socket implementation in c (via a native call).

0
source

All Articles