On 8/6/07, mat <[EMAIL PROTECTED]> wrote:
> On 8/6/07, Trustin Lee <[EMAIL PROTECTED]> wrote:
> >
> > 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);
>
> So the mina core will take care of "in"? For instance, cut the "in" into
> piece to send one by one?
StreamWriteFilter will take care of 'in'.
> Otherwise, the data written will still be garbled for large data?
Yes, only if you reuse your buffer. You are expected not to reuse
buffers because all I/O operations in MINA are *asynchronous*. As
long as you don't reuse the buffers, there won't be no garbled data at
all.
HTH,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6