List of usage examples for org.apache.commons.io IOUtils toByteArray
public static byte[] toByteArray(String input) throws IOException
String
as a byte[]
using the default character encoding of the platform. From source file:com.predic8.membrane.core.multipart.ReassembleTest.java
private Response getResponse() throws IOException { byte[] body = IOUtils.toByteArray(getClass().getResourceAsStream("/multipart/embedded-byte-array.txt")); return Response.ok() .header("Content-Type", "multipart/related; " + "type=\"application/xop+xml\"; " + "boundary=\"uuid:168683dc-43b3-4e71-8e66-efb633ef406b\"; " + "start=\"<root.message@cxf.apache.org>\"; " + "start-info=\"text/xml\"") .header("Content-Length", "" + body.length).body(body).build(); }
From source file:li.poi.services.docx.resource.MailMerge.java
@Path("/test") @GET//from ww w.j a v a 2 s . c om @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public MailMergeResponse test() throws Docx4JException { MailMergeResponse mail = new MailMergeResponse(); File tmp = null; try { WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!"); tmp = File.createTempFile("document", "docx"); wordMLPackage.save(tmp); mail.setDocument(Base64.encodeBase64String(IOUtils.toByteArray(new FileInputStream(tmp)))); mail.setError("success"); mail.setDocumentName(tmp.getName()); } catch (IOException e) { e.printStackTrace(); mail.setError("error... " + e.getMessage()); } finally { if (tmp != null) { boolean deleted = tmp.delete(); if (!deleted) { log.warn("file not deleted " + tmp.getAbsolutePath()); } } } return mail; }
From source file:functionaltests.runasme.TestRunAsMeLinuxKey.java
@BeforeClass public static void startDedicatedScheduler() throws Exception { assumeTrue(OperatingSystem.getOperatingSystem() == OperatingSystem.unix); setupUser();/*from w ww .j a v a 2 s .c o m*/ String keyPath = System.getProperty(RUNASME_KEY_PATH_PROPNAME); assumeNotNull(keyPath); key = IOUtils.toByteArray(new File(keyPath).toURI()); RMFactory.setOsJavaProperty(); // start an empty scheduler and add a node source with modified properties schedulerHelper = new SchedulerTHelper(true, true); List<String> arguments = new ArrayList<>(); arguments.addAll(RMTHelper.setup.getJvmParametersAsList()); arguments.add("-D" + ForkerUtils.FORK_METHOD_KEY + "=" + ForkerUtils.ForkMethod.KEY.toString()); schedulerHelper.createNodeSource("RunAsMeNSKey", 5, arguments); }
From source file:net.bulletin.pdi.xero.step.support.XMLChunkerTest.java
private byte[] readSampleXml() throws IOException { InputStream inputStream = null; try {/*from www .j a v a 2 s.co m*/ inputStream = XMLChunkerTest.class.getResourceAsStream("/sample_xml_a.xml"); if (null == inputStream) { throw new IllegalStateException("the sample xml file was unable to be accessed"); } return IOUtils.toByteArray(inputStream); } finally { if (null != inputStream) { try { inputStream.close(); } catch (IOException ignore) { } } } }
From source file:com.helegris.szorengeteg.ui.MediaLoader.java
/** * Turns a file into a btye array./*ww w. j a va 2s .c o m*/ * * @param file * @return byte array * @throws NotFoundException exception containing file information */ public byte[] loadBytes(File file) throws NotFoundException { try { return IOUtils.toByteArray(new FileInputStream(file)); } catch (FileNotFoundException e) { throw new NotFoundException(file); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.talis.storage.SubmittedItemTest.java
@Test public void getEntityReturnsInputStreamFromConstructorUnread() throws IOException { byte[] data = UUID.randomUUID().toString().getBytes(); InputStream entity = new ByteArrayInputStream(data); SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, entity); assertSame(entity, submittedItem.getEntity()); assertTrue(Arrays.equals(data, IOUtils.toByteArray(submittedItem.getEntity()))); }
From source file:net.sf.clickclick.examples.page.control.DynamicImagePage.java
private byte[] generateChart() { // This method returns a byte array that can be dynamically retrieved // from a database or generated on the fly. For simplicity sake a static // image is looked up try {/* w w w. jav a 2 s . c o m*/ InputStream is = getContext().getServletContext().getResourceAsStream("/assets/images/chart.png"); return IOUtils.toByteArray(is); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
From source file:com.google.mr4c.content.AbstractContentFactory.java
public byte[] readContentAsBytes(URI uri) throws IOException { InputStream input = readContentAsStream(uri); try {// w w w . ja v a2 s .c om return IOUtils.toByteArray(input); } finally { input.close(); } }
From source file:de.qaware.chronix.converter.common.Compression.java
/** * Decompressed the given byte[]/*from w ww . ja v a2 s . c o m*/ * * @param compressed - the compressed byte[] * @return an input stream of the uncompressed byte[] */ public static byte[] decompress(byte[] compressed) { if (compressed == null) { LOGGER.debug("Compressed bytes[] are null. Returning empty byte[]."); return new byte[] {}; } try { InputStream decompressed = decompressToStream(compressed); if (decompressed != null) { return IOUtils.toByteArray(decompressed); } } catch (IOException e) { LOGGER.error("Exception occurred while decompressing gzip stream. Returning empty byte[].", e); } return new byte[] {}; }
From source file:com.woodcomputing.bobbin.format.PESFormat.java
@Override public Design load(File file) { byte[] bytes = null; try (InputStream is = new FileInputStream(file)) { bytes = IOUtils.toByteArray(is); } catch (IOException ex) { log.catching(ex);//w w w.jav a 2 s . co m } ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.LITTLE_ENDIAN); Design design = new Design(); log.debug("Magic: {}{}{}{}", (char) bb.get(), (char) bb.get(), (char) bb.get(), (char) bb.get()); int pecStart = bb.getInt(8); log.debug("PEC Start: {}", pecStart); byte colorCount = bb.get(pecStart + 48); log.debug("Color Count: {}", colorCount); int colors[] = new int[colorCount]; for (int i = 0; i < colorCount; i++) { colors[i] = bb.get() & 0xFF; log.debug("Color[{}] = {}", i, colors[i]); } bb.position(pecStart + 532); int x; int y; int colorChanges = 0; PESColor color = pesColorMap.get(colors[colorChanges++]); StitchGroup stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getColor()); while (true) { x = bb.get() & 0xFF; y = bb.get() & 0xFF; if (x == 0xFF && y == 0x00) { log.debug("End of stitches"); break; } if (x == 0xFE && y == 0xB0) { int colorIndex = bb.get() & 0xFF; log.debug("Color change: {}", colorIndex); color = pesColorMap.get(colors[colorChanges++]); stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getColor()); continue; } if ((x & 0x80) > 0) { log.debug("Testing X: {} - X & 0x80: {}", x, x & 0x80); if ((x & 0x20) > 0) { log.debug("Stich type TRIM"); } if ((x & 0x10) > 0) { log.debug("Stich type JUMP"); } x = ((x & 0x0F) << 8) + y; if ((x & 0x800) > 0) { x -= 0x1000; } y = bb.get() & 0xFF; } else if (x >= 0x40) { x -= 0x80; } if ((y & 0x80) > 0) { log.debug("Testing Y: {} - Y & 0x80: {}", y, y & 0x80); if ((y & 0x20) > 0) { log.debug("Stich type TRIM"); } if ((y & 0x10) > 0) { log.debug("Stich type JUMP"); } y = ((y & 0x0F) << 8) + bb.get() & 0xFF; if ((y & 0x800) > 0) { y -= 0x1000; } } else if (y >= 0x40) { y -= 0x80; } // Stitch designStitch = new Stitch(cx, -cy, nx, -ny); // stitchGroup.getStitches().add(designStitch); log.debug("X: {} Y: {}", x, y); } log.debug("Color Changes: {}", colorChanges); return design; }