Thanks. I was just putting together a small test case to send to you to
demonstrate the problem... but the test case worked as expected! So let
me go back and figure out why my original implementation was behaving
differently.
Just fyi, my test case is included below. The restlet
Application.createRoot() has the expected:
router.attach("/testResource", TestResource.class);
--- cut here ---
package com.foo.bar;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.OutputRepresentation;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.Variant;
public class TestResource extends Resource {
private HSSFWorkbook workbook;
public TestResource(Context context, Request request, Response
response) {
super(context, request, response);
getVariants().add(new Variant(MediaType.APPLICATION_EXCEL));
}
@Override
public Representation getRepresentation(Variant variant) {
OutputRepresentation r = new
OutputRepresentation(MediaType.APPLICATION_EXCEL) {
@Override
public void write(OutputStream stream) throws IOException {
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("test");
HSSFRow row = sheet.createRow(0);
row.createCell((short) 0).setCellValue("Hi mom!");
workbook.write(stream);
}
};
return r;
}
}
Jerome Louvel wrote:
Hi Denis,
Indeed, -1 is used as a marker for the end of an input stream by Restlets
and actually by the JDK, see:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html#read()
The other important point is that bytes are handled by streams as ints
within the 0 to 255 range.
I'm not too familiar with POI, but from what I've seen in the Javadocs, you
do something like this:
- prepare your POI's POIFSDocument or DocumentEntry instance
- create a new org.apache.poi.poifs.filesystem.DocumentInputStream
- create a new org.restlet.resource.InputRepresentation, passing the
DocumentInputStream created before
- set the InputRepresentation instance as the entity of your Restlet
response.
- the rest (actual writing of the document will transparently by handled by
the InputRepresentation class)
Let me know if it doesn't work!
Best regards,
Jerome
-----Message d'origine-----
De : Denis Haskin [mailto:[EMAIL PROTECTED]
Envoyé : jeudi 1 mars 2007 05:39
À : [email protected]
Objet : OutputRepresentation, binary, and -1
I'm wondering if I'm misunderstanding something about
OutputRepresentation and binary content.
I've got a test where I write the contents of a byte array in
my OutputRepresentation.write() method. It appears that this
output is getting truncated when it hits a -1 in the binary content.
For example, with this test:
OutputRepresentation r = new
OutputRepresentation(MediaType.APPLICATION_EXCEL) {
public void write(OutputStream stream) throws
IOException {
byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
stream.write(bytes);
}
};
I get 20 bytes when I read the stream later.
If I do this:
OutputRepresentation r = new
OutputRepresentation(MediaType.APPLICATION_EXCEL) {
public void write(OutputStream stream) throws
IOException {
byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6,
7, 8, 9, -1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
stream.write(bytes);
}
};
I only get 9 bytes when I read it. Inspecting
ByteUtils.getStream indicates this might be true, that -1 is
being used as a flag byte to indicate the end of the stream.
Is that correct?
As you can see fromm this example, what I'm really trying to
do is deliver Excel content to the browser... in this case
it's an in-memory workbook (generated with Poi) and I'm just
sending the bytes down. I'm actually not sure if I need some
encoding or whether just writing the byte stream is sufficient.
Thoughts?
Thanks,
Denis haskin