Hey there,

        I am trying to output large files and with support for Content-Range 
header. It seems like I need to use 
org.restlet.representation.OutputRepresentation. From what I've read online and 
in the book, I expect this to work, however inefficiently, "out of the box", 
but I did this:

this.getRangeService().setEnabled(true);

in my top-level restlet, just to be sure. Unfortunately, I've found the server 
hangs after serving the required amount of data. I have experimented with just 
supplying the amount of data actually requested, but that causes problems as 
well.

My code looks something like this the code below. Any help appreciated, as I'm 
a bit baffled  and not really sure how to proceed. So far most of my data has 
been automagically converted to XML without much thought, and I'm not really 
sure how this part of the process actually works.

                        List<Range> ranges = getRequest().getRanges();
                        
                        Range ranget;
                        if( ranges.size() == 0 )
                                ranget = null;
                        else if( ranges.size() == 1 )
                                ranget = ranges.get(0);
                        else
                                return // error not supported!!!
                        final Range range = ranget;
                        if( range != null && range.getSize() + range.getIndex() 
> ms.getTotalSize() )
                                return // error: requested out of range

                        //Note: I'm not actually using the range found above, 
but I've been playing with it, so I thought I'd include it.
                        OutputRepresentation os = new OutputRepresentation( new 
MediaType(m.getMimeType()), ms.getTotalSize()) {
                                @Override
                                public void write(OutputStream outputStream) 
throws IOException {
                                        Iterator<MediaChunk> curChunk = 
ms.getChunks().iterator();
                                        byte[] b = new byte[10240];
                                        long cur = 0;
                                        long amt = 0;
                                        try {
                                                while( curChunk.hasNext() ) {
                                                        MediaChunk mc = 
curChunk.next();
                                                        InputStream is = 
mc.openStream();
                                                        try {
                                                                while( true ) {
                                                                        int r;
                                                                        
r=is.read(b);
                                                                        if( r 
== -1 )
                                                                                
break;
                                                                        cur += 
r;
                                                                        amt += 
r;
                                                                        
outputStream.write(b, 0, r); //HANGS HERE!
                                                                }
                                                        } finally {
                                                                is.close();
                                                        }
                                                }
                                        } finally {
                                                outputStream.close();
                                                long s = ms.getTotalSize() ;
                                                if( amt != s )
                                                        throw new IOException( 
"Error reading requested data: " + amt + " : " + s );
                                        }
                                }
                     }

-----------------------------
Bjorn Roche
http://www.xonami.com
Audio Collaboration
http://blog.bjornroche.com

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2946626

Reply via email to