private void convertToLocalTempDataBean(DataBean bean) throws IOException { try { // copy contents to new file File newFile = this.createNewRepositoryFile(bean.getName()); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile)); ChecksumInputStream inputStream = getContentStream(bean, DataNotAvailableHandling.EXCEPTION_ON_NA); BufferedInputStream in = new BufferedInputStream(inputStream); try { IOUtils.copy(in, out); setOrVerifyChecksum(bean, inputStream.verifyChecksums()); } finally { IOUtils.closeIfPossible(in); IOUtils.closeIfPossible(out); } // update url, type and handler in the bean URL newURL = newFile.toURI().toURL(); addContentLocationForDataBean(bean, StorageMethod.LOCAL_TEMP, newURL); } catch (ChecksumException | ContentLengthException e) { // corrupted data throw new IOException(); } }
/** * Convenience method for creating a local temporary file DataBean with content. Content stream is * read into a temp file and location of the file is stored to DataBean. * * @throws IOException */ public DataBean createDataBean(String name, InputStream content) throws MicroarrayException, IOException { // copy the data from the input stream to the file in repository File contentFile; contentFile = createNewRepositoryFile(name); InputStream input = new BufferedInputStream(content); OutputStream output = new BufferedOutputStream(new FileOutputStream(contentFile)); try { IO.copy(input, output); output.flush(); } finally { IOUtils.closeIfPossible(input); IOUtils.closeIfPossible(output); } // create and return the bean DataBean bean = createDataBean(name); try { addContentLocationForDataBean(bean, StorageMethod.LOCAL_TEMP, contentFile.toURI().toURL()); } catch (MalformedURLException e) { throw new MicroarrayException(e); } catch (ContentLengthException e) { // shouldn't happen, because newly created bean doesn't have size set logger.error(e, e); } return bean; }
private void parseMetadata() throws ZipException, IOException, JAXBException, SAXException { ZipFile zipFile = null; try { // get the session.xml zip entry zipFile = new ZipFile(sessionFile); InputStream metadataStream = zipFile.getInputStream(zipFile.getEntry(UserSession.SESSION_DATA_FILENAME)); // validate // ClientSession.getSchema().newValidator().validate(new StreamSource(metadataStream)); // parse the metadata xml to java objects using jaxb Unmarshaller unmarshaller = UserSession.getPreviousJAXBContext().createUnmarshaller(); unmarshaller.setSchema(UserSession.getPreviousSchema()); NonStoppingValidationEventHandler validationEventHandler = new NonStoppingValidationEventHandler(); unmarshaller.setEventHandler(validationEventHandler); this.sessionType = unmarshaller.unmarshal(new StreamSource(metadataStream), SessionType.class).getValue(); if (validationEventHandler.hasEvents()) { throw new JAXBException( "Invalid session file:\n" + validationEventHandler.getValidationEventsAsString()); } } finally { IOUtils.closeIfPossible(zipFile); } }
private String getSourceCode(String sourceCodeFileName) throws ZipException, IOException { ZipFile zipFile = null; InputStream sourceCodeInputStream = null; StringWriter stringWriter = null; try { zipFile = new ZipFile(sessionFile); sourceCodeInputStream = zipFile.getInputStream(zipFile.getEntry(sourceCodeFileName)); stringWriter = new StringWriter(); IOUtils.copy(sourceCodeInputStream, new WriterOutputStream(stringWriter)); stringWriter.flush(); } finally { IOUtils.closeIfPossible(sourceCodeInputStream); IOUtils.closeIfPossible(stringWriter); if (zipFile != null) { zipFile.close(); } } return stringWriter.toString(); }
@Override public boolean isAvailable( String dataId, Long contentLength, String checksum, FileBrokerArea area) throws FileBrokerException { try { InputStream inStream = download(getSessionId(), dataId); IOUtils.closeIfPossible(inStream); return true; } catch (NotFoundException e) { return false; } }
/** * A convenience method for gathering streamed binary content into a byte array. Gathers only * maxLength first bytes. Returns null if none of the content locations are available. * * @throws IOException * @see #getContentStream() */ public byte[] getContentBytes(DataBean bean, long maxLength, DataNotAvailableHandling naHandling) throws IOException { InputStream in = null; try { in = getContentStream(bean, naHandling); if (in != null) { return Files.inputStreamToBytes(in, maxLength); } else { return null; } } finally { IOUtils.closeIfPossible(in); } }