@Test public void testReadWithNonUniformResize() throws IOException { Dimension size = new Dimension(150, 150); // Custom setup HttpServletRequest request = mock(HttpServletRequest.class); when(request.getAttribute(ImageServletResponse.ATTRIB_SIZE)).thenReturn(size); when(request.getAttribute(ImageServletResponse.ATTRIB_SIZE_UNIFORM)).thenReturn(false); when(request.getContextPath()).thenReturn("/ape"); when(request.getRequestURI()).thenReturn("/ape/" + IMAGE_NAME_PNG); FastByteArrayOutputStream out = new FastByteArrayOutputStream(STREAM_DEFAULT_SIZE); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new OutputStreamAdapter(out)); ImageServletResponseImpl imageResponse = new ImageServletResponseImpl(request, response, context); fakeResponse(request, imageResponse); // Make sure image is correctly loaded BufferedImage image = imageResponse.getImage(); assertNotNull(image); assertEquals(size.width, image.getWidth()); assertEquals(size.height, image.getHeight()); // Flush image to wrapped response imageResponse.flush(); assertTrue("Content has no data", out.size() > 0); // Test that image data is still readable BufferedImage outImage = ImageIO.read(out.createInputStream()); assertNotNull(outImage); assertEquals(image.getWidth(), outImage.getWidth()); assertEquals(image.getHeight(), outImage.getHeight()); verify(response).getOutputStream(); verify(response).setContentType(CONTENT_TYPE_PNG); }
@Override public void write(int pByte) { int newCount = count + 1; growIfNeeded(newCount); buf[count] = (byte) pByte; count = newCount; }
@Test public void testCreateInputStream() throws IOException { FastByteArrayOutputStream out = makeObject(); String hello = "Hello World"; out.write(hello.getBytes("UTF-8")); InputStream in = out.createInputStream(); byte[] read = FileUtil.read(in); assertEquals(hello, new String(read, "UTF-8")); } }
private static InputStream createJFIFStream(int bands, int stripTileWidth, int stripTileHeight, byte[][] qTables, byte[][] dcTables, byte[][] acTables, int subsampling) throws IOException { FastByteArrayOutputStream stream = new FastByteArrayOutputStream( 2 + 5 * qTables.length + qTables.length * qTables[0].length + return stream.createInputStream();
@Test public void testNoOpResponse() throws IOException { FastByteArrayOutputStream out = new FastByteArrayOutputStream(STREAM_DEFAULT_SIZE); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new OutputStreamAdapter(out)); ImageServletResponseImpl imageResponse = new ImageServletResponseImpl(request, response, context); fakeResponse(request, imageResponse); // TODO: Is there a way we can avoid calling flush? // Flush image to wrapped response imageResponse.flush(); assertTrue("Content has no data", out.size() > 0); // Test that image data is untouched assertTrue("Data differs", Arrays.equals(FileUtil.read(getClass().getResourceAsStream(IMAGE_NAME_PNG)), out.toByteArray())); verify(response).setContentType(CONTENT_TYPE_PNG); verify(response).getOutputStream(); }
@Override protected OutputStream createOutputStream() throws IOException { if (originalContentLength >= 0) { bufferedOut = new FastByteArrayOutputStream(originalContentLength); } else { bufferedOut = new FastByteArrayOutputStream(0); } return bufferedOut; } };
ArrayList<ArrayList<class2>> obj = null; try { FastByteArrayOutputStream fbos = new FastByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(fbos); out.writeObject(foo); out.flush(); out.close(); ObjectInputStream in = new ObjectInputStream(fbos.getInputStream()); obj = (ArrayList<ArrayList<class2>>) in.readObject(); } catch(IOException e) { e.printStackTrace(); } catch(ClassNotFoundException cnfe) { cnfe.printStackTrace(); } return obj;
InputStream byteStream = bufferedOut.createInputStream(); ImageInputStream input = null; try {
private synchronized void createOutputStream() { ByteArrayOutputStream cache = cachedResponse.content; if (cache == null) { String contentLengthStr = getHeaderValue("Content-Length"); if (contentLengthStr != null) { int contentLength = Integer.parseInt(contentLengthStr); cache = new FastByteArrayOutputStream(contentLength); } else { cache = new FastByteArrayOutputStream(1024); } cachedResponse.content = cache; } }
private static InputStream createJFIFStream(int bands, int stripTileWidth, int stripTileHeight, byte[][] qTables, byte[][] dcTables, byte[][] acTables) throws IOException { FastByteArrayOutputStream stream = new FastByteArrayOutputStream( 2 + 5 * qTables.length + qTables.length * qTables[0].length + out.writeByte(0); return stream.createInputStream();
@Test public void testNoOpResponse() throws IOException { FastByteArrayOutputStream out = new FastByteArrayOutputStream(STREAM_DEFAULT_SIZE); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new OutputStreamAdapter(out)); ImageServletResponseImpl imageResponse = new ImageServletResponseImpl(request, response, context); fakeResponse(request, imageResponse); // TODO: Is there a way we can avoid calling flush? // Flush image to wrapped response imageResponse.flush(); assertTrue("Content has no data", out.size() > 0); // Test that image data is untouched assertTrue("Data differs", Arrays.equals(FileUtil.read(getClass().getResourceAsStream(IMAGE_NAME_PNG)), out.toByteArray())); verify(response).setContentType(CONTENT_TYPE_PNG); verify(response).getOutputStream(); }
@Test public void testReadWithNonSquareSourceRegion() throws IOException { Rectangle sourceRegion = new Rectangle(100, 100, 100, 80); // Custom setup HttpServletRequest request = mock(HttpServletRequest.class); when(request.getAttribute(ImageServletResponse.ATTRIB_AOI)).thenReturn(sourceRegion); when(request.getContextPath()).thenReturn("/ape"); when(request.getRequestURI()).thenReturn("/ape/" + IMAGE_NAME_PNG); FastByteArrayOutputStream out = new FastByteArrayOutputStream(STREAM_DEFAULT_SIZE); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new OutputStreamAdapter(out)); ImageServletResponseImpl imageResponse = new ImageServletResponseImpl(request, response, context); fakeResponse(request, imageResponse); // Make sure image is correctly loaded BufferedImage image = imageResponse.getImage(); assertNotNull(image); assertEquals(sourceRegion.width, image.getWidth()); assertEquals(sourceRegion.height, image.getHeight()); // Flush image to wrapped response imageResponse.flush(); assertTrue("Content has no data", out.size() > 0); // Test that image data is still readable BufferedImage outImage = ImageIO.read(out.createInputStream()); assertNotNull(outImage); assertEquals(image.getWidth(), outImage.getWidth()); assertEquals(image.getHeight(), outImage.getHeight()); verify(response).getOutputStream(); verify(response).setContentType(CONTENT_TYPE_PNG); }
CachedResponseImpl(final int pStatus, final LinkedHashMap<String, List<String>> pHeaders, final int pHeaderSize, final byte[] pContent) { status = pStatus; headers = Validate.notNull(pHeaders, "headers"); headersSize = pHeaderSize; content = new FastByteArrayOutputStream(pContent); }
public void testCreateInputStream() throws IOException { FastByteArrayOutputStream out = makeObject(); String hello = "Hello World"; out.write(hello.getBytes("UTF-8")); InputStream in = out.createInputStream(); byte[] read = FileUtil.read(in); assertEquals(hello, new String(read, "UTF-8")); } }
@Override public void write(byte pBytes[], int pOffset, int pLength) { if ((pOffset < 0) || (pOffset > pBytes.length) || (pLength < 0) || ((pOffset + pLength) > pBytes.length) || ((pOffset + pLength) < 0)) { throw new IndexOutOfBoundsException(); } else if (pLength == 0) { return; } int newCount = count + pLength; growIfNeeded(newCount); System.arraycopy(pBytes, pOffset, buf, count, pLength); count = newCount; }
when(request.getRequestURI()).thenReturn("/ape/" + IMAGE_NAME_PNG); FastByteArrayOutputStream out = new FastByteArrayOutputStream(STREAM_DEFAULT_SIZE); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new OutputStreamAdapter(out)); assertTrue("Content has no data", out.size() > 0); BufferedImage outImage = ImageIO.read(out.createInputStream()); assertNotNull(outImage); assertEquals(image.getWidth(), outImage.getWidth());
protected FastByteArrayOutputStream makeObject() { return new FastByteArrayOutputStream(256); }
@Test public void testCreateInputStream() throws IOException { FastByteArrayOutputStream out = makeObject(); String hello = "Hello World"; out.write(hello.getBytes("UTF-8")); InputStream in = out.createInputStream(); byte[] read = FileUtil.read(in); assertEquals(hello, new String(read, "UTF-8")); } }
@Override public void write(int pByte) { int newCount = count + 1; growIfNeeded(newCount); buf[count] = (byte) pByte; count = newCount; }
when(request.getRequestURI()).thenReturn("/ape/" + IMAGE_NAME_PNG); FastByteArrayOutputStream out = new FastByteArrayOutputStream(STREAM_DEFAULT_SIZE); HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new OutputStreamAdapter(out)); assertTrue("Content has no data", out.size() > 0); BufferedImage outImage = ImageIO.read(out.createInputStream()); assertNotNull(outImage); assertEquals(image.getWidth(), outImage.getWidth());