001package bradleyross.j2ee.servlets;
002import java.io.OutputStream;
003import java.io.IOException;
004import javax.servlet.http.HttpServlet;
005import javax.servlet.http.HttpServletRequest;
006import javax.servlet.http.HttpServletResponse;
007import javax.servlet.ServletException;
008/**
009 * Send a response as an application/octet-stream MIME type.
010 * <p>This will be for testing ability of browsers to read binary
011 *    data using XmlHTTPRequest objects to read binary data.</p>
012 * <p>The first three unsigned integers contain a value of 512.  
013 *    The next group of integers increment by one until
014 *    they reach the end.</p>
015 * <p>The code was modified to have a 512x512 matrix in addition to the first two
016 *    integers in order to present something resembling an actual raster image.</p>
017 * <p>When JavaScript is used with XMLHttpRequest and the ArrayBuffer mode,
018 *    it apparently reads the data in little endian format (least significant first).
019 *    The DataOutputStream class writes the data using big endian format (most
020 *    significant first).</p>   
021 * @author Bradley Ross
022 *
023 */
024@SuppressWarnings("serial")
025public class OctetStream extends HttpServlet {
026        protected static void  writeShort(OutputStream output, int value) throws IOException {
027                output.write(value & 0xff);
028                output.write((value >> 8) & 0xff);
029        }
030        public void service (HttpServletRequest req, HttpServletResponse res) 
031                        throws IOException, ServletException {
032                res.setContentType("application/octet-stream");
033                OutputStream output = res.getOutputStream();
034                writeShort(output, 0x0200);
035                writeShort(output, 0x0200);
036                for (int i = 0; i < 512*512; i++) {
037                        writeShort(output, (0x0200 + i) % 50000);
038                }
039        }
040}