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

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

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

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 w  w  w.  j a v  a  2s  . co 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 {//w  w  w.  j a va2 s.  co  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 void initialiseWorkbook(InputStream inputStream, char delimChar) {
    delimiterType = delimChar;/*w ww  . j  av  a  2s. c o  m*/
    if (fileFormat.equalsIgnoreCase("XLS")) {
        try {
            // Try to get the XLS workbook/sheet
            // Streams directly from inputStream into Workbook.getWorkbook(Inputstream)
            Workbook wkb = Workbook.getWorkbook(inputStream);
            sheet = wkb.getSheet(0); // get First Work Sheet
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
    } else {
        // Error when reading XLS file type, so must be CSV or TXT
        // Thus attempt a convert from csv or text to xls format
        try {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            CsvReader csvReader = new CsvReader(inputStreamReader, delimiterType);
            WritableWorkbook writableWorkBook = Workbook.createWorkbook(output);
            jxl.write.WritableSheet wsheet = writableWorkBook.createSheet("Sheet", 0);
            int row = 0;

            WritableFont normalFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
            WritableCellFormat insertCellFormat = new WritableCellFormat(normalFont);
            insertCellFormat.setBackground(Colour.LIGHT_GREEN);

            WritableCellFormat updateCellFormat = new WritableCellFormat(normalFont);
            updateCellFormat.setBackground(Colour.LIGHT_BLUE);

            WritableFont errorFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
            WritableCellFormat errorCellFormat = new WritableCellFormat(errorFont);
            errorCellFormat.setBackground(Colour.RED);

            // Loop through all rows in file
            while (csvReader.readRecord()) {
                String[] stringArray = csvReader.getValues();

                for (int col = 0; col < stringArray.length; col++) {
                    jxl.write.Label label = new jxl.write.Label(col, row, stringArray[col]);

                    if (!errorCells.isEmpty()) {
                        ArkGridCell cell = new ArkGridCell(col, row);
                        if (errorCells.contains(cell)) {
                            label.setCellFormat(errorCellFormat);
                        } else if (updateRows.contains(row)) {
                            label.setCellFormat(updateCellFormat);
                        } else {
                            label.setCellFormat(insertCellFormat);
                        }
                    }
                    wsheet.addCell(label);
                }
                row++;
            }

            // All sheets and cells added. Now write out the workbook
            writableWorkBook.write();

            sheet = writableWorkBook.getSheet(0); // get First Work Sheet to display in webpage

            writableWorkBook.close();
            output.flush();
            inputStream.close();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

    // Store validated/formated workbook as bytes[] for download
    workBookAsBytes = writeOutValidationXlsFileToBytes();

    /*
     * Sets Sheet meta data. The HTML table creation needs this object to know about the rows and columns
     */
    sheetMetaData.setRows(sheet.getRows());
    sheetMetaData.setCols(sheet.getColumns());
}

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

License:Open Source License

public byte[] writeOutValidationXlsFileToBytes() {
    byte[] bytes = null;
    try {/*from   w w  w. j a va2s  .  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
 * //from   w  ww  .j a  v a 2 s  .  c o m
 * @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 w  w  .j  a  v 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];
}