Java Question Implementation Prefix mode [Async]

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hi,

I'm developing a server-client application.
The server is done in Java (PC) and the client in Basic4Android (Android).

I'm having trouble with the following implementation:

Server grabs bitmap -> raw bytes -> TCP -> Client (Async Streams _NewData)

Now the byte array is delivered in multiple packets of different lengths in the NewData event. So to handle this properly, I should use the prefix method. The problem is that i don't know how to implement this method into Java.

B4X:
OutputStream os = socket.getOutputStream();
         os.write(buffer);
         os.flush();

Erel, is there any way you could elaborate on how I would implement the prefix mode on the above java code? (buffer contains the raw bytes of the bitmap.)

Regards,
Tomas

EDIT:

Can i achieve the following for this:
B4X:
/**
       * Sends a screenshot to the client
       * @param message
       * @throws IOException 
       */
      public void sendScreenshot(byte[] buffer) throws IOException {
         OutputStream os = socket.getOutputStream();
         os.write(buffer.length + 1);
         os.write((byte) 0);
         os.write(buffer, 0, buffer.length);
         os.flush();
      }
 
Last edited:

XverhelstX

Well-Known Member
Licensed User
Longtime User
Some more info:

java - Implementation Prefix Mode - Stack Overflow

I tried the first comment on my topic on Stack Overflow:
B4X:
DataOutputStream out = new DataOutputStream(os);
out.writeInt(buffer.length + 1);
// This writes a single byte
out.write(0);
out.write(buffer);
out.flush();

which does the same (multiple packets)

Tomas

EDIT:

Ok i forgot to change initialize to InitializePrefix. How do i know if i should use Little or Big endian?
Now, i receive a toastmessage (probably from AStreams_Error) - NegativeArraySizeException.

Tomas
 
Last edited:
Top