public static byte[] encodeImage( final Image image, final String mimeType, final float quality, final boolean alpha ) throws UnsupportedEncoderException, IOException { final MemoryByteArrayOutputStream byteOut = new MemoryByteArrayOutputStream( 65536, 65536 * 2 ); encodeImage( byteOut, image, mimeType, quality, alpha ); return byteOut.toByteArray(); }
/** * Converts a SQL-Clob object into a String. If the Clob is larger than 2^31 characters, we cannot convert it. If * there are errors converting it, this method will log the cause and return null. * * @param clob the clob to be read as string. * @return the string or null in case of errors. */ public byte[] readBlob( final Blob clob ) throws IOException, SQLException { final long length = clob.length(); if ( length > Integer.MAX_VALUE ) { logger.warn( "This CLOB contains more than 2^31 characters. We cannot handle that." ); throw new IOException( "This BLOB contains more than 2^31 characters. We cannot handle that." ); } final InputStream inStream = clob.getBinaryStream(); final MemoryByteArrayOutputStream outStream = new MemoryByteArrayOutputStream( (int) length, 65536 ); try { IOUtils.getInstance().copyStreams( inStream, outStream ); } finally { try { inStream.close(); } catch ( IOException e ) { logger.warn( "Failed to close input stream. No worries, we will be alright anyway.", e ); } } return outStream.toByteArray(); } }
return outStream.toByteArray();
/** * @noinspection IOResourceOpenedButNotSafelyClosed */ private byte[] extractData( final InputSource input ) throws IOException { final InputStream byteStream = input.getByteStream(); if ( byteStream != null ) { try { final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(); IOUtils.getInstance().copyStreams( byteStream, bout ); return bout.toByteArray(); } finally { byteStream.close(); } } final Reader characterStream = input.getCharacterStream(); if ( characterStream == null ) { throw new IOException( "InputSource has neither an Byte nor a CharacterStream" ); } try { final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(); final OutputStreamWriter owriter = new OutputStreamWriter( bout ); IOUtils.getInstance().copyWriter( characterStream, owriter ); owriter.close(); return bout.toByteArray(); } finally { characterStream.close(); } }
final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(); IOUtils.getInstance().copyStreams( postResult, bout ); return bout.toByteArray(); } finally { postResult.close();
protected void handleXmlContent( final byte[] reportOutput, final File goldSample ) throws Exception { final byte[] goldData; final InputStream goldInput = new BufferedInputStream( new FileInputStream( goldSample ) ); final MemoryByteArrayOutputStream goldByteStream = new MemoryByteArrayOutputStream( Math.min( 1024 * 1024, (int) goldSample.length() ), 1024 * 1024 ); try { IOUtils.getInstance().copyStreams( goldInput, goldByteStream ); goldData = goldByteStream.toByteArray(); if ( Arrays.equals( goldData, reportOutput ) ) { return; } } finally { goldInput.close(); } final Reader reader = new InputStreamReader( new ByteArrayInputStream( goldData ), "UTF-8" ); final ByteArrayInputStream inputStream = new ByteArrayInputStream( reportOutput ); final Reader report = new InputStreamReader( inputStream, "UTF-8" ); try { XMLAssert.assertXMLEqual( "File " + goldSample + " failed", new Diff( reader, report ), true ); } catch ( AssertionFailedError afe ) { debugOutput( reportOutput, goldSample ); throw afe; } finally { reader.close(); } }
private MasterReport saveAndLoad( final MasterReport originalReport ) throws Exception { final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(); BundleWriter.writeReportToZipStream( originalReport, bout ); assertTrue( bout.getLength() > 0 ); /* * final File f = File.createTempFile("test-output-", ".prpt", new File ("test-output")); final FileOutputStream * outputStream = new FileOutputStream(f); outputStream.write(bout.toByteArray()); outputStream.close(); */ final ResourceManager mgr = new ResourceManager(); final Resource reportRes = mgr.createDirectly( bout.toByteArray(), MasterReport.class ); return (MasterReport) reportRes.getResource(); }
public static MasterReport saveAndLoad( MasterReport report ) throws Exception { final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(); BundleWriter.writeReportToZipStream( report, bout ); final ResourceManager mgr = new ResourceManager(); mgr.registerDefaults(); final Resource resource = mgr.createDirectly( bout.toByteArray(), MasterReport.class ); return (MasterReport) resource.getResource(); } }
protected byte[] executePageable( final MasterReport report ) throws IOException, ReportProcessingException { final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream(); try { final XmlPageOutputProcessor outputProcessor = new XmlPageOutputProcessor( outputStream, new XmlPageOutputProcessorMetaData( localFontRegistry ) ); final PageableReportProcessor streamReportProcessor = new PageableReportProcessor( report, outputProcessor ); try { streamReportProcessor.processReport(); } finally { streamReportProcessor.close(); } } finally { outputStream.close(); } return ( outputStream.toByteArray() ); }
public static DataFactory loadAndSaveOnReport( final DataFactory dataFactory ) throws IOException, BundleWriterException, ContentIOException, ResourceException { final MasterReport report = new MasterReport(); report.setDataFactory( dataFactory ); final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(); BundleWriter.writeReportToZipStream( report, bout ); final ResourceManager mgr = new ResourceManager(); mgr.registerDefaults(); final Resource resource = mgr.createDirectly( bout.toByteArray(), MasterReport.class ); final MasterReport r2 = (MasterReport) resource.getResource(); return r2.getDataFactory(); }
protected byte[] executeTablePage( final MasterReport report ) throws IOException, ReportProcessingException { final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream(); try { final XmlTableOutputProcessor outputProcessor = new XmlTableOutputProcessor( outputStream, new XmlTableOutputProcessorMetaData( XmlTableOutputProcessorMetaData.PAGINATION_FULL, localFontRegistry ) ); final ReportProcessor streamReportProcessor = new PageableReportProcessor( report, outputProcessor ); try { streamReportProcessor.processReport(); } finally { streamReportProcessor.close(); } } finally { outputStream.close(); } return ( outputStream.toByteArray() ); }
protected byte[] executeTableStream( final MasterReport report ) throws IOException, ReportProcessingException { final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream(); try { final XmlTableOutputProcessor outputProcessor = new XmlTableOutputProcessor( outputStream, new XmlTableOutputProcessorMetaData( XmlTableOutputProcessorMetaData.PAGINATION_NONE, localFontRegistry ) ); final ReportProcessor streamReportProcessor = new StreamReportProcessor( report, outputProcessor ); try { streamReportProcessor.processReport(); } finally { streamReportProcessor.close(); } } finally { outputStream.close(); } return ( outputStream.toByteArray() ); }
protected byte[] executeTableFlow( final MasterReport report ) throws IOException, ReportProcessingException { final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream(); try { final XmlTableOutputProcessor outputProcessor = new XmlTableOutputProcessor( outputStream, new XmlTableOutputProcessorMetaData( XmlTableOutputProcessorMetaData.PAGINATION_MANUAL, localFontRegistry ) ); final ReportProcessor streamReportProcessor = new FlowReportProcessor( report, outputProcessor ); try { streamReportProcessor.processReport(); } finally { streamReportProcessor.close(); } } finally { outputStream.close(); } return ( outputStream.toByteArray() ); }
public static byte[] createXmlTablePageable( final MasterReport report ) throws IOException, ReportProcessingException { final MemoryByteArrayOutputStream outputStream = new MemoryByteArrayOutputStream(); try { final LocalFontRegistry localFontRegistry = new LocalFontRegistry(); localFontRegistry.initialize(); final XmlTableOutputProcessor outputProcessor = new XmlTableOutputProcessor( outputStream, new XmlTableOutputProcessorMetaData( XmlTableOutputProcessorMetaData.PAGINATION_FULL, localFontRegistry ) ); final ReportProcessor streamReportProcessor = new PageableReportProcessor( report, outputProcessor ); try { streamReportProcessor.processReport(); } finally { streamReportProcessor.close(); } } finally { outputStream.close(); } return ( outputStream.toByteArray() ); }
protected void runSaveAndLoadForSubReports( final String[][] queriesAndResults ) throws Exception { if ( queriesAndResults.length == 0 ) { return; } for ( int i = 0; i < queriesAndResults.length; i++ ) { final String query = queriesAndResults[i][0]; final String resultFile = queriesAndResults[i][1]; final DataFactory dataFactory = createDataFactory( query ); SubReport subReport = new SubReport(); subReport.setDataFactory( dataFactory ); final MasterReport report = new MasterReport(); report.getReportHeader().addSubReport( subReport ); final MemoryByteArrayOutputStream bout = new MemoryByteArrayOutputStream(); BundleWriter.writeReportToZipStream( report, bout ); final ResourceManager mgr = new ResourceManager(); mgr.registerDefaults(); final Resource resource = mgr.createDirectly( bout.toByteArray(), MasterReport.class ); final MasterReport r2 = (MasterReport) resource.getResource(); final SubReport sr2 = r2.getReportHeader().getSubReport( 0 ); final DataFactory e2 = sr2.getDataFactory(); assertNotNull( e2 ); // cannot assert equals, as this is not implemented ... initializeDataFactory( e2 ); final String queryResult = performQueryTest( e2 ); compareLineByLine( resultFile, queryResult ); } }