Example usage for org.apache.wicket.util.io ByteArrayOutputStream toByteArray

List of usage examples for org.apache.wicket.util.io ByteArrayOutputStream toByteArray

Introduction

In this page you can find the example usage for org.apache.wicket.util.io ByteArrayOutputStream toByteArray.

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Writes to a byte array.

Usage

From source file:au.org.theark.core.util.CustomFieldImporter.java

License:Open Source License

/**
 * Return the inputstream of the converted workbook as csv
 * //from  w  ww  . ja  v  a  2s . co m
 * @return inputstream of the converted workbook as csv
 */
@Override
public InputStream convertXlsToCsv(Workbook w) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        OutputStreamWriter osw = new OutputStreamWriter(out);

        // Gets first sheet from workbook
        Sheet s = w.getSheet(0);

        Cell[] row = null;

        // Gets the cells from sheet
        for (int i = 0; i < s.getRows(); i++) {
            row = s.getRow(i);

            if (row.length > 0) {
                osw.write(row[0].getContents());
                for (int j = 1; j < row.length; j++) {
                    osw.write(phenotypicDelimChr);
                    osw.write(row[j].getContents());
                }
            }
            osw.write("\n");
        }

        osw.flush();
        osw.close();
    } catch (UnsupportedEncodingException e) {
        System.err.println(e.toString());
    } catch (IOException e) {
        System.err.println(e.toString());
    } catch (Exception e) {
        System.err.println(e.toString());
    }
    return new ByteArrayInputStream(out.toByteArray());
}

From source file:au.org.theark.core.util.XLStoCSV.java

License:Open Source License

/**
 * Return the inputstream of the converted workbook as csv
 * /*from ww  w .  j  av a  2s . c o m*/
 * @return inputstream of the converted workbook as csv
 */
public InputStream convertXlsToCsv(Workbook w) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SimpleDateFormat sdf = new SimpleDateFormat(Constants.DD_MM_YYYY);
    try {
        OutputStreamWriter osw = new OutputStreamWriter(out);
        // Gets first sheet from workbook
        Sheet s = w.getSheet(0);
        Cell[] row = null;

        for (int i = 0; i < s.getRows(); i++) {
            row = s.getRow(i);

            if (row.length > 0) {
                osw.write(row[0].getContents());
                for (int j = 1; j < row.length; j++) {
                    osw.write(delimiterCharacter);
                    Cell cell = row[j];
                    if (row[j].getContents().contains(",")) {
                        osw.write('"');
                        if (cell instanceof DateCell) {
                            DateCell dc = (DateCell) cell;
                            Date d = dc.getDate();
                            osw.write(sdf.format(d));
                        } else {
                            osw.write(cell.getContents());
                        }
                        osw.write('"');
                    } else {
                        if (cell instanceof DateCell) {
                            DateCell dc = (DateCell) cell;
                            Date d = dc.getDate();
                            osw.write(sdf.format(d));
                        } else {
                            osw.write(cell.getContents());
                        }
                    }
                }
            }
            osw.write("\n");
        }

        osw.flush();
        osw.close();
    } catch (UnsupportedEncodingException e) {
        System.err.println(e.toString());
    } catch (IOException e) {
        System.err.println(e.toString());
    } catch (Exception e) {
        System.err.println(e.toString());
    }
    return new ByteArrayInputStream(out.toByteArray());
}

From source file:au.org.theark.core.web.component.button.ArkDownloadAjaxButton.java

License:Open Source License

public byte[] writeOutXlsFileToBytes(String[] xlsHeader) {
    byte[] bytes = null;
    ArkSheetMetaData sheetMetaData = new ArkSheetMetaData();
    sheetMetaData.setRows(1);//from   ww w  .j a v a2 s.c  o  m
    sheetMetaData.setCols(xlsHeader.length);

    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        WritableWorkbook w = Workbook.createWorkbook(output);
        WritableSheet writableSheet = w.createSheet("Sheet", 0);

        for (int row = 0; row < sheetMetaData.getRows(); row++) {
            for (int col = 0; col < sheetMetaData.getCols(); col++) {
                String cellData = xlsHeader[col];
                jxl.write.Label label = new jxl.write.Label(col, row, cellData);
                writableSheet.addCell(label);
            }
        }

        w.write();
        w.close();
        bytes = output.toByteArray();
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:au.org.theark.core.web.component.button.ArkDownloadTemplateButton.java

License:Open Source License

public byte[] writeOutXlsFileToBytes() {
    byte[] bytes = null;

    WritableFont normalFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);
    WritableFont boldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
    WritableCellFormat cellFormat = null;

    try {//from  w w w .j a v  a2  s  .  c  o  m
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        WritableWorkbook w = Workbook.createWorkbook(output);
        WritableSheet writableSheet = w.createSheet("Sheet", 0);

        for (int row = 0; row < getTemplateCells().length; row++) {
            for (int col = 0; col < sheetMetaData.getCols(); col++) {
                String cellData = getTemplateCells()[row][col];
                jxl.write.Label label = new jxl.write.Label(col, row, cellData);

                if (row == 0) {
                    // Header row in bold
                    cellFormat = new WritableCellFormat(boldFont);
                } else {
                    cellFormat = new WritableCellFormat(normalFont);
                }

                label.setCellFormat(cellFormat);
                writableSheet.addCell(label);
            }
        }

        w.write();
        w.close();
        bytes = output.toByteArray();
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:au.org.theark.core.web.component.worksheet.ArkExcelWorkSheetAsGrid.java

License:Open Source License

public byte[] writeOutValidationXlsFileToBytes() {
    byte[] bytes = null;
    try {/*  w  w  w .  ja  v a 2 s. c  o m*/
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        WritableWorkbook w = Workbook.createWorkbook(output);
        WritableSheet writableSheet = w.createSheet("Sheet", 0);
        WritableFont normalFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);
        WritableCellFormat insertCellFormat = new WritableCellFormat(normalFont);
        insertCellFormat.setBackground(Colour.LIGHT_GREEN);
        WritableCellFormat updateCellFormat = new WritableCellFormat(normalFont);
        // Override to light blue
        w.setColourRGB(Colour.LIGHT_BLUE, 173, 216, 230);
        updateCellFormat.setBackground(Colour.LIGHT_BLUE);
        WritableCellFormat errorCellFormat = new WritableCellFormat(normalFont);
        errorCellFormat.setBackground(Colour.RED);
        WritableCellFormat warningCellFormat = new WritableCellFormat(normalFont);
        warningCellFormat.setBackground(Colour.LIGHT_ORANGE);

        for (int row = 0; row < sheetMetaData.getRows(); row++) {
            for (int col = 0; col < sheetMetaData.getCols(); col++) {
                Cell cell = sheet.getCell(col, row);
                String cellData = cell.getContents();
                jxl.write.Label label = new jxl.write.Label(col, row, cellData);

                ArkGridCell gridCell = new ArkGridCell(col, row);
                if (row > 0) {
                    if (errorCells.contains(gridCell)) {
                        label.setCellFormat(errorCellFormat);
                    } else if (warningCells.contains(gridCell)) {
                        label.setCellFormat(warningCellFormat);
                    } else if (updateCells.contains(gridCell)) {
                        label.setCellFormat(updateCellFormat);
                    } else {
                        label.setCellFormat(insertCellFormat);
                    }
                }
                writableSheet.addCell(label);
            }
        }

        w.write();
        w.close();
        bytes = output.toByteArray();
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:au.org.theark.lims.web.component.inventory.panel.box.display.GridBoxPanel.java

License:Open Source License

/**
 * Initialise a WorkBook object representing the Box from the database and store as byte array
 * /*ww w  .  ja va2s.c  om*/
 * @param invCellList
 * @return WorkBook as a byte array
 */
private byte[] createWorkBookAsByteArray(List<InvCell> invCellList) {
    byte[] bytes = null;
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        WritableWorkbook writableWorkBook = Workbook.createWorkbook(output);
        WritableSheet writableSheet = writableWorkBook.createSheet("Sheet", 0);

        int col = 0;
        int row = 0;
        int cellCount = 0;

        for (Iterator<InvCell> iterator = invCellList.iterator(); iterator.hasNext();) {
            InvCell invCell = (InvCell) iterator.next();
            row = invCell.getRowno().intValue();
            col = invCell.getColno().intValue();

            Biospecimen biospecimen = new Biospecimen();
            biospecimen = invCell.getBiospecimen();
            jxl.write.Label label = null;

            if (biospecimen != null) {
                label = new jxl.write.Label(col, row, biospecimen.getBiospecimenUid());
            } else {
                label = new jxl.write.Label(col, row, "");
            }
            writableSheet.addCell(label);
            cellCount = cellCount + 1;
        }

        writableWorkBook.write();
        writableWorkBook.close();
        bytes = output.toByteArray();
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bytes;
}

From source file:com.gitblit.servlet.PtServlet.java

License:Apache License

byte[] readAll(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/*from w ww  .  j  av  a  2 s .c  o  m*/
        byte[] buffer = new byte[4096];
        int len = 0;
        while ((len = is.read(buffer)) > -1) {
            os.write(buffer, 0, len);
        }
        return os.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            os.close();
            is.close();
        } catch (Exception e) {
            // ignore
        }
    }
    return new byte[0];
}

From source file:com.jeroensteenbeeke.hyperion.reflection.PropertyTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/* w  w w.j a v  a  2 s. c om*/
public void testSerialization() throws IOException, ClassNotFoundException {
    Property<PropertyTest> prop = Reflector.getProperty(PropertyTest.class, "field");

    assertNotNull(prop.getter());
    assertNotNull(prop.setter());
    assertNotNull(prop.field());

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(buffer);

    oos.writeObject(prop);

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    Property<PropertyTest> prop2 = (Property<PropertyTest>) ois.readObject();

    assertEquals(prop.getter(), prop2.getter());
    assertEquals(prop.setter(), prop2.setter());
    assertEquals(prop.field(), prop2.field());
    assertEquals(prop.name(), prop2.name());
    assertEquals(prop.owningClass(), prop2.owningClass());

    oos.close();

}

From source file:com.servoy.j2db.debug.SerializingRemoteInvocationHandler.java

License:Open Source License

/**
 * Write the object to an object stream and read it back.
 * @param <O>/*w w  w . j  a  va  2s  . c om*/
 * @param o
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public <O> O serializeAndDeserialize(O o) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(baos);
    os.writeObject(o);
    os.close();

    ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())) {
        @Override
        protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
            String name = desc.getName();
            try {
                return Class.forName(name, false, application.getBeanManager().getClassLoader());
            } catch (ClassNotFoundException e) {
                Debug.error("Could not load class using beanmanager classloader, trying default", e);
                return super.resolveClass(desc);
            }
        }
    };
    @SuppressWarnings("unchecked")
    O o2 = (O) is.readObject();
    is.close();

    return o2;
}

From source file:net.dontdrinkandroot.extensions.wicket.resource.AbstractFileThumbnailResource.java

License:Apache License

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    final File file = this.resolveFile(attributes);

    final Integer width = this.resolveWidth(attributes);

    final byte[] imageBytes;
    try {/* w w  w.  ja  v  a  2 s.c  om*/
        if (width == null) {
            final FileInputStream fis = new FileInputStream(file);
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            IOUtils.copy(fis, imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
            IOUtils.closeQuietly(fis);
        } else {
            final BufferedImage bufferedImage = ImageIO.read(file);
            final int oldWidth = bufferedImage.getWidth();
            final int oldHeight = bufferedImage.getHeight();
            final int newWidth = width;
            final int newHeight = newWidth * oldHeight / oldWidth;
            final BufferedImage scaledImage = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            final Graphics2D g2d = (Graphics2D) scaledImage.getGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(bufferedImage, 0, 0, newWidth, newHeight, 0, 0, oldWidth, oldHeight, null);
            g2d.dispose();
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            ImageIO.write(scaledImage, "jpg", imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
        }
    } catch (final IOException e) {
        throw new WicketRuntimeException(e);
    }

    final ResourceResponse resourceResponse = new ResourceResponse() {
        @Override
        public WriteCallback getWriteCallback() {
            return new WriteCallback() {
                @Override
                public void writeData(final Attributes attributes) {
                    try {
                        this.writeStream(attributes, new ByteArrayInputStream(imageBytes));
                    } catch (IOException e) {
                        throw new WicketRuntimeException(e);
                    }
                }
            };
        }
    };
    resourceResponse.setContentType("image/jpeg");
    resourceResponse.setLastModified(Time.millis(file.lastModified()));
    resourceResponse.setCacheDuration(this.getCacheDuration());
    resourceResponse.setContentLength(imageBytes.length);

    return resourceResponse;
}