List of usage examples for org.apache.wicket.util.io ByteArrayOutputStream ByteArrayOutputStream
public ByteArrayOutputStream()
From source file:au.org.theark.core.util.CustomFieldImporter.java
License:Open Source License
/** * Return the inputstream of the converted workbook as csv * //w w w . j av a2 s . c o 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 * /*www. j ava2 s. co 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);/*w ww . ja v a 2 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 . jav 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 void initialiseWorkbook(InputStream inputStream, char delimChar) { delimiterType = delimChar;//w ww . j a v a2 s .c om 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 {// ww w . j a v a 2s. co 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 * /*w w w . j a v a2s . 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 {// w w w .j a v a2 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.gitblit.utils.CompressionUtils.java
License:Apache License
/** * Compresses/archives the contents of the tree at the (optionally) * specified revision and the (optionally) specified basepath to the * supplied outputstream.//from ww w . j a v a 2 s . co m * * @param algorithm * compression algorithm for tar (optional) * @param repository * @param basePath * if unspecified, entire repository is assumed. * @param objectId * if unspecified, HEAD is assumed. * @param os * @return true if repository was successfully zipped to supplied output * stream */ private static boolean tar(String algorithm, Repository repository, String basePath, String objectId, OutputStream os) { RevCommit commit = JGitUtils.getCommit(repository, objectId); if (commit == null) { return false; } OutputStream cos = os; if (!StringUtils.isEmpty(algorithm)) { try { cos = new CompressorStreamFactory().createCompressorOutputStream(algorithm, os); } catch (CompressorException e1) { error(e1, repository, "{0} failed to open {1} stream", algorithm); } } boolean success = false; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try { tw.reset(); tw.addTree(commit.getTree()); TarArchiveOutputStream tos = new TarArchiveOutputStream(cos); tos.setAddPaxHeadersForNonAsciiNames(true); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); if (!StringUtils.isEmpty(basePath)) { PathFilter f = PathFilter.create(basePath); tw.setFilter(f); } tw.setRecursive(true); MutableObjectId id = new MutableObjectId(); long modified = commit.getAuthorIdent().getWhen().getTime(); while (tw.next()) { FileMode mode = tw.getFileMode(0); if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } tw.getObjectId(id, 0); ObjectLoader loader = repository.open(id); if (FileMode.SYMLINK == mode) { TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(), TarArchiveEntry.LF_SYMLINK); ByteArrayOutputStream bos = new ByteArrayOutputStream(); loader.copyTo(bos); entry.setLinkName(bos.toString()); entry.setModTime(modified); tos.putArchiveEntry(entry); tos.closeArchiveEntry(); } else { TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString()); entry.setMode(mode.getBits()); entry.setModTime(modified); entry.setSize(loader.getSize()); tos.putArchiveEntry(entry); loader.copyTo(tos); tos.closeArchiveEntry(); } } tos.finish(); tos.close(); cos.close(); success = true; } catch (IOException e) { error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName()); } finally { tw.release(); rw.dispose(); } return success; }
From source file:com.japancuccok.common.infrastructure.gae.GaeDeferredFileOutputStream.java
License:Apache License
/** * Constructs an instance of this class which will trigger an event at the specified threshold, * and save data to a file beyond that point. * // www . j a va2 s . c o m * @param threshold * The number of bytes at which to trigger an event. * @param fileName * The file to which data is saved beyond the threshold. */ public GaeDeferredFileOutputStream(int threshold, String fileName) { super(threshold); this.fileName = fileName; memoryOutputStream = new ByteArrayOutputStream(); currentOutputStream = memoryOutputStream; }