/** * Gets the contents of the given file, as a byte array. * * @param pFilename the name of the file to get content from * @return the content of the file as a byte array. * @throws IOException if the read operation fails */ public static byte[] read(String pFilename) throws IOException { return read(new File(pFilename)); }
/** * Reads the response message from a file in the current web app. * * @param pFileName * @return the message */ private String readMessage(String pFileName) { try { // Read resource from web app InputStream is = getServletContext().getResourceAsStream(pFileName); if (is != null) { return new String(FileUtil.read(is)); } else { log("File not found: " + pFileName); } } catch (IOException ioe) { log("Error reading file: " + pFileName + " (" + ioe.getMessage() + ")"); } return null; }
if (content != null && content.exists()) { byte[] contents = FileUtil.read(content);
protected static void markReset(SeekableInputStream pSeekable) throws IOException { for (int i = 0; i < 10; i++) { pSeekable.mark(); System.out.println(new String(FileUtil.read(pSeekable))); pSeekable.reset(); } System.out.println(); System.out.println("mark/reset ok!"); }
protected static void timeRead(SeekableInputStream pSeekable) throws IOException { for (int i = 0; i < 5000; i++) { pSeekable.mark(); FileUtil.read(pSeekable); pSeekable.reset(); } long start = System.currentTimeMillis(); final int times = 200000; for (int i = 0; i < times; i++) { pSeekable.mark(); FileUtil.read(pSeekable); pSeekable.reset(); } long time = System.currentTimeMillis() - start; System.out.println("Time; " + time + "ms (" + (time / (float) times) + "ms/inv)"); }
protected void testSeekSkip(SeekableInputStream pSeekable, String pStr) throws IOException { System.out.println(); pSeekable.seek(pStr.length()); FileUtil.read(pSeekable); for (int i = 0; i < 10; i++) { byte[] bytes = FileUtil.read(pSeekable); int len = bytes.length; if (len != 0) { System.err.println("Error in buffer length after full read..."); System.err.println("len: " + len); System.err.println("bytes: \"" + new String(bytes) + "\""); break; } } System.out.println(); for (int i = 0; i < 10; i++) { pSeekable.seek(0); int skip = i * 3; //noinspection ResultOfMethodCallIgnored pSeekable.skip(skip); String str = new String(FileUtil.read(pSeekable)); System.out.println(str); if (str.length() != pStr.length() - skip) { throw new Error("Error in buffer length after skip"); } } System.out.println(); System.out.println("seek/skip ok!"); System.out.println(); }
private void runStreamTest(int pLength) throws Exception { byte[] data = createData(pLength); ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); OutputStream out = new EncoderStream(outBytes, createCompatibleEncoder(), true); out.write(data); out.close(); byte[] encoded = outBytes.toByteArray(); byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder())); assertArrayEquals(String.format("Data %d", pLength), data, decoded); InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()); outBytes = new ByteArrayOutputStream(); FileUtil.copy(in, outBytes); outBytes.close(); in.close(); decoded = outBytes.toByteArray(); assertArrayEquals(String.format("Data %d", pLength), data, decoded); }
@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 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")); } }
@Test public void testSplit() throws IOException { InputStream inputStream = getClassLoaderResource("/contrib/tiff/multipage.tif").openStream(); File inputFile = File.createTempFile("imageiotest", "tif"); byte[] data = FileUtil.read(inputStream); FileUtil.write(inputFile, data); inputStream.close(); File outputDirectory = Files.createTempDirectory("imageio").toFile(); TIFFUtilities.split(inputFile, outputDirectory); ImageReader reader = ImageIO.getImageReadersByFormatName("TIF").next(); File[] outputFiles = outputDirectory.listFiles(); Assert.assertEquals(3, outputFiles.length); for (File outputFile : outputFiles) { ImageInputStream iis = ImageIO.createImageInputStream(outputFile); reader.setInput(iis); Assert.assertEquals(1, reader.getNumImages(true)); iis.close(); outputFile.delete(); } outputDirectory.delete(); inputFile.delete(); }
byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder())); assertTrue(Arrays.equals(data, decoded));
@Test public void testApplyOrientation() throws IOException { InputStream inputStream = getClassLoaderResource("/contrib/tiff/multipage.tif").openStream(); File inputFile = File.createTempFile("imageiotest", "tif"); byte[] data = FileUtil.read(inputStream); FileUtil.write(inputFile, data); inputStream.close(); BufferedImage image = ImageIO.read(inputFile); // rotate by 90� BufferedImage image90 = TIFFUtilities.applyOrientation(image, TIFFExtension.ORIENTATION_RIGHTTOP); // rotate by 270� BufferedImage image360 = TIFFUtilities.applyOrientation(image90, TIFFExtension.ORIENTATION_LEFTBOT); byte[] original = ((DataBufferByte) image.getData().getDataBuffer()).getData(); byte[] rotated = ((DataBufferByte) image360.getData().getDataBuffer()).getData(); Assert.assertArrayEquals(original, rotated); }
byte[] data = FileUtil.read(inputStream); FileUtil.write(inputFile, data); inputStream.close();
/** * Gets the contents of the given file, as a byte array. * * @param pFilename the name of the file to get content from * @return the content of the file as a byte array. * @throws IOException if the read operation fails */ public static byte[] read(String pFilename) throws IOException { return read(new File(pFilename)); }
/** * Gets the contents of the given file, as a byte array. * * @param pFilename the name of the file to get content from * @return the content of the file as a byte array. * @throws IOException if the read operation fails */ public static byte[] read(String pFilename) throws IOException { return read(new File(pFilename)); }
@Test public void testSeekSkipRead() throws Exception { SeekableInputStream seekable = makeInputStream(133); int pos = 45; for (int i = 0; i < 10; i++) { seekable.seek(pos); //noinspection ResultOfMethodCallIgnored seekable.skip(i); byte[] bytes = FileUtil.read(seekable); assertEquals(133, seekable.getStreamPosition()); assertEquals(133 - 45- i, bytes.length); } }
@Test public void testCloseUnderlyingStream() throws IOException { final boolean[] closed = new boolean[1]; ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; SeekableInputStream stream = makeInputStream(input); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); }
@Test public void testCloseUnderlyingFile() throws IOException { final boolean[] closed = new boolean[1]; File file = createFileWithContent(new ByteArrayInputStream(makeRandomArray(256))); RandomAccessFile raf = new RandomAccessFile(file, "r") { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; FileSeekableStream stream = new FileSeekableStream(raf); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); } }
public void testSeekSkipRead() throws Exception { SeekableInputStream seekable = makeInputStream(133); int pos = 45; for (int i = 0; i < 10; i++) { seekable.seek(pos); //noinspection ResultOfMethodCallIgnored seekable.skip(i); byte[] bytes = FileUtil.read(seekable); assertEquals(133, seekable.getStreamPosition()); assertEquals(133 - 45- i, bytes.length); } }