On 8/6/07, leafsax <[EMAIL PROTECTED]> wrote:
>
> Hi,
> I was trying to use MINA to transfer files between machines. It was pretty
> good when I was trying to send some small files, but when the requests were
> larger, (a file > 100M),  I could not send the file correctly. Since I could
> not send the file in just one ByteBuffer, I read some of the file and send,
> then read the rest of the file and send. But, the file received is not equal
> to the file sent by the server. The file length is correct, but the contents
> were interleaved.
>
> The following is the code to send file in server side:
>        private void sendFile(IoSession session, String fileName) throws
> FileNotFoundException {
>                byte[] buffer = new byte[1024];
>                DataInputStream dis = new DataInputStream(new 
> FileInputStream(fileName));
>                int bytesRead = 0;
>                try {
>                        System.out.println("send file");
>                        while ((bytesRead = dis.read(buffer, 0, 1024)) > 0) {
>                                session.write(ByteBuffer.wrap(buffer, 0, 
> bytesRead));
>                        }
>                } catch (IOException e) {
>                        // TODO Auto-generated catch block
>                        e.printStackTrace();
>                }
>        }
> Some questions:
> Is there any misuse of session and request in the code above?
> Is there any good suggestions to do file or large request transfer in MINA?

You need to allocate the buffer for each read.  Otherwise, the data
written will be garbled because the background I/O processor threads
will be accessing the same buffer instance while you read something in
there.

The best practice is to insert a StreamWriteFilter into the filter
chain and write an InputStream of a file like the following:

session.getFilterChain().addLast("streamWriter", new StreamWriteFilter());
...

FileInputStream in = new FileInputStream(....);
session.write(in);

HTH,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Reply via email to