niceman
niceman

Reputation: 2673

get number of bytes a DataOutputStream wrapped around Socket.getOutputStream wrote

I have a server-client application and using java Sockets API.

The server will send bytes to the client by calling DataOutputStream's write(bytes[] b), the DataOutputStream is wrapped around client.getOutputStream directly(no Buffer here).

This is a file download functionality And I'm going to support resume, I didn't use any HTTP here, I've implemented my own simple protocol.

I've seen these questions on SO :
1-question1.
2-C# question.
3-FileChannel question.

The first doesn't answer my question, I can't wrap DataOutputStream around ByteArrayOutputStream because the latter can't be wrapped around client.getOutputStream and I don't know how to implement my own write(int i) method(And don't want to use JNI).

the second is C# not java(and it's calling WIN API anyway, I'm a linux user by the way :) ).

The third is talking about FileChannels and HTTP, As I said I'm not using HTTP and I'm using java Socket's API.

So how to get how many bytes were actually written ?

PS (EDIT)

By resume support I mean I'll give the client the ability to stop download at specific byte and then after a while(minutes,hours,days,whatever) he/she can resume download from where it has left.

Upvotes: 1

Views: 1381

Answers (2)

Ravindra babu
Ravindra babu

Reputation: 38950

When you write the data as byte array, write the size of the data too. When you read the data, read the size and construct byte array.

I prefer and use this approach to send large files (> 10 MB) including images from client to server. By writing the size upfront, you know how many bytes to read from byte[]

Have look at this one or this article to convert file into byte[] and back

/*  Convert File to byte[] msgBytes */


/* Write byte array */
OutputStream out = socket.getOutputStream(); 
DataOutputStream dos = new DataOutputStream(out);
int length = msgBytes.length;// it is byte array of message to be sent
dos.writeInt(length);
if (length > 0) {
    dos.write(msgBytes);
}

/*Read byte array */
DataInputStream dis = new DataInputStream(socket.getInputStream());
int length = dis.readInt();                   
if(length>0) {
    byte[] msgBytes = new byte[length];
    dis.readFully(msgBytes, 0, length); 
}

/* Now you can convert byte array back to File*/

EDIT: (for resuming download, which is not part of original post when I answered)

  1. Save the number of bytes you have read from the server at client (use inputStream.read() instead of inputStream.readFully()

  2. Skip that value (bytesAlreadyRead) when you re-connect to server

Upvotes: 1

user207421
user207421

Reputation: 311050

  • The number of byes written by write(int c) is 1.

  • The number of bytes written by write(bye[] buffer) is given by buffer.length.

  • The number of bytes written by write(bye[] buffer, int offset, int length) is given by length.

Just as it says in the Javadoc.

Upvotes: 2

Related Questions