List of usage examples for org.apache.commons.io.input BoundedInputStream BoundedInputStream
public BoundedInputStream(InputStream in, long size)
BoundedInputStream
that wraps the given input stream and limits it to a certain size. From source file:org.codice.ddf.catalog.ui.util.EndpointUtil.java
public String safeGetBody(Request req) throws IOException { if (req.contentLength() > config.getMaximumUploadSize()) { throw new EntityTooLargeException(req.ip(), req.userAgent(), req.url(), random.nextInt()); }/*from w w w . j a va 2 s. co m*/ byte[] bytes = IOUtils.toByteArray( new BoundedInputStream(req.raw().getInputStream(), config.getMaximumUploadSize() + 1L)); if (bytes.length > config.getMaximumUploadSize()) { throw new EntityTooLargeException(req.ip(), req.userAgent(), req.url(), random.nextInt()); } return new String(bytes, Charset.defaultCharset()); }
From source file:org.codice.ddf.rest.impl.CatalogServiceImpl.java
private void parseAttribute(Map<String, AttributeImpl> attributeMap, String parsedName, InputStream inputStream, AttributeType.AttributeFormat attributeFormat) { try (InputStream is = inputStream; InputStream boundedStream = new BoundedInputStream(is, MAX_INPUT_SIZE + 1L)) { if (attributeFormat == OBJECT) { LOGGER.debug("Object type not supported for override"); return; }/*from w w w . j a v a 2 s . co m*/ byte[] bytes = IOUtils.toByteArray(boundedStream); if (bytes.length > MAX_INPUT_SIZE) { LOGGER.debug("Attribute length is limited to {} bytes", MAX_INPUT_SIZE); return; } AttributeImpl attribute; if (attributeMap.containsKey(parsedName)) { attribute = attributeMap.get(parsedName); } else { attribute = new AttributeImpl(parsedName, Collections.emptyList()); attributeMap.put(parsedName, attribute); } if (attributeFormat == BINARY) { attribute.addValue(bytes); return; } String value = new String(bytes, Charset.defaultCharset()); switch (attributeFormat) { case XML: case GEOMETRY: case STRING: attribute.addValue(value); break; case BOOLEAN: attribute.addValue(Boolean.valueOf(value)); break; case SHORT: attribute.addValue(Short.valueOf(value)); break; case LONG: attribute.addValue(Long.valueOf(value)); break; case INTEGER: attribute.addValue(Integer.valueOf(value)); break; case FLOAT: attribute.addValue(Float.valueOf(value)); break; case DOUBLE: attribute.addValue(Double.valueOf(value)); break; case DATE: try { Instant instant = Instant.parse(value); attribute.addValue(Date.from(instant)); } catch (DateTimeParseException e) { LOGGER.debug("Unable to parse instant '{}'", attribute, e); } break; default: LOGGER.debug("Attribute format '{}' not supported", attributeFormat); break; } } catch (IOException e) { LOGGER.debug("Unable to read attribute to override", e); } }
From source file:org.deegree.console.client.RequestBean.java
public void sendRequest() throws UnsupportedEncodingException { if (!request.startsWith("<?xml")) { request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + request; }//from w w w . ja v a2 s.c o m String targetUrl = getTargetUrl(); LOG.debug("Try to send the following request to " + targetUrl + " : \n" + request); if (targetUrl != null && targetUrl.length() > 0 && request != null && request.length() > 0) { InputStream is = new ByteArrayInputStream(request.getBytes("UTF-8")); try { DURL u = new DURL(targetUrl); DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u); HttpPost post = new HttpPost(targetUrl); post.setHeader("Content-Type", "text/xml;charset=UTF-8"); post.setEntity(new InputStreamEntity(is, -1)); HttpResponse response = client.execute(post); Header[] headers = response.getHeaders("Content-Type"); if (headers.length > 0) { mimeType = headers[0].getValue(); LOG.debug("Response mime type: " + mimeType); if (!mimeType.toLowerCase().contains("xml")) { this.response = null; FacesMessage fm = MessageUtils.getFacesMessage(FacesMessage.SEVERITY_INFO, "INFO_RESPONSE_NOT_XML"); FacesContext.getCurrentInstance().addMessage(null, fm); } } if (mimeType == null) { mimeType = "text/plain"; } InputStream in = response.getEntity().getContent(); File file = File.createTempFile("genericclient", ".xml"); responseFile = file.getName().toString(); FileOutputStream out = new FileOutputStream(file); try { IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); } BufferedReader reader = null; try { in = new BoundedInputStream(new FileInputStream(file), 1024 * 256); reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); StringBuilder sb = new StringBuilder(); String s; while ((s = reader.readLine()) != null) { sb.append(s); } this.response = sb.toString(); } finally { IOUtils.closeQuietly(reader); } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } } }
From source file:org.deventropy.shared.utils.DirectoryArchiverUtilTest.java
private void checkTarArchive(final File archiveFile, final File sourceDirectory, final String pathPrefix) throws IOException { ArchiveInputStream tarInputStream = null; try {//from www. j a v a 2 s . co m tarInputStream = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, new BufferedInputStream(new FileInputStream(archiveFile))); final ArchiveEntries archiveEntries = createArchiveEntries(sourceDirectory, pathPrefix); ArchiveEntry tarEntry = null; while ((tarEntry = tarInputStream.getNextEntry()) != null) { if (tarEntry.isDirectory()) { assertTrue("Directory in tar should be from us [" + tarEntry.getName() + "]", archiveEntries.dirs.contains(tarEntry.getName())); archiveEntries.dirs.remove(tarEntry.getName()); } else { assertTrue("File in tar should be from us [" + tarEntry.getName() + "]", archiveEntries.files.containsKey(tarEntry.getName())); final byte[] inflatedMd5 = getMd5Digest( new BoundedInputStream(tarInputStream, tarEntry.getSize()), false); assertArrayEquals("MD5 hash of files should equal [" + tarEntry.getName() + "]", archiveEntries.files.get(tarEntry.getName()), inflatedMd5); archiveEntries.files.remove(tarEntry.getName()); } } // Check that all files and directories have been accounted for assertTrue("All directories should be in the tar", archiveEntries.dirs.isEmpty()); assertTrue("All files should be in the tar", archiveEntries.files.isEmpty()); } catch (ArchiveException e) { throw new IOException(e); } finally { if (null != tarInputStream) { tarInputStream.close(); } } }
From source file:org.eclipse.acute.OmnisharpStreamConnectionProvider.java
/** * * @return path to server, unzipping it if necessary. Can be null is fragment is missing. *//*from w ww.ja va 2 s. co m*/ private @Nullable File getServer() throws IOException { File serverPath = new File(AcutePlugin.getDefault().getStateLocation().toFile(), "omnisharp-roslyn"); //$NON-NLS-1$ if (!serverPath.exists()) { serverPath.mkdirs(); try (InputStream stream = FileLocator.openStream(AcutePlugin.getDefault().getBundle(), new Path("omnisharp-roslyn.tar"), true); //$NON-NLS-1$ TarArchiveInputStream tarStream = new TarArchiveInputStream(stream);) { TarArchiveEntry entry = null; while ((entry = tarStream.getNextTarEntry()) != null) { if (!entry.isDirectory()) { File targetFile = new File(serverPath, entry.getName()); targetFile.getParentFile().mkdirs(); InputStream in = new BoundedInputStream(tarStream, entry.getSize()); // mustn't be closed try (FileOutputStream out = new FileOutputStream(targetFile);) { IOUtils.copy(in, out); if (!Platform.OS_WIN32.equals(Platform.getOS())) { int xDigit = entry.getMode() % 10; targetFile.setExecutable(xDigit > 0, (xDigit & 1) == 1); int wDigit = (entry.getMode() / 10) % 10; targetFile.setWritable(wDigit > 0, (wDigit & 1) == 1); int rDigit = (entry.getMode() / 100) % 10; targetFile.setReadable(rDigit > 0, (rDigit & 1) == 1); } } } } } } return serverPath; }
From source file:org.eclipse.dirigible.repository.ext.command.Piper.java
public Piper(java.io.InputStream input, java.io.OutputStream output) { this.input = new BoundedInputStream(input, MAX_COMMAND_OUTPUT_LENGTH); this.output = output; }
From source file:org.fastcatsearch.ir.document.DocumentReader.java
public Document readDocument(int docNo, boolean[] fieldSelectOption, boolean indexable) throws IOException { // if(docNo < baseDocNo) throw new // IOException("Request docNo cannot less than baseDocNo! docNo = "+docNo+", baseDocNo = "+baseDocNo); // baseDocNo? . // docNo -= baseDocNo; DataInput bai = null;/*from w ww. j a v a 2 s. c o m*/ if (docNo != lastDocNo) { long positionOffset = docNo * IOUtil.SIZE_OF_LONG; if (positionOffset >= positionLimit) { //. return null; } positionInput.seek(positionOffset); long pos = positionInput.readLong(); // find a document block docInput.seek(pos); int len = docInput.readInt(); //2014-11-26 ? working ? ? ? GC ?? OOM ? ?. // Stream . InflaterInputStream decompressInputStream = null; inflaterOutput.reset(); int count = -1; try { BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len); boundedInputStream.setPropagateClose(false);// docInput . decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512); while ((count = decompressInputStream.read(workingBuffer)) != -1) { inflaterOutput.write(workingBuffer, 0, count); } } finally { decompressInputStream.close(); } BytesRef bytesRef = inflaterOutput.getBytesRef(); bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length); lastDocNo = docNo; lastBai = bai; } else { lastBai.reset(); bai = lastBai; } Document document = new Document(fields.size()); for (int i = 0; i < fields.size(); i++) { FieldSetting fs = fields.get(i); Field f = null; boolean hasValue = bai.readBoolean(); // logger.debug("read hasValue={}, select={}, fs={} ", hasValue, fieldSelectOption, fs); if (hasValue) { //1. fieldSelectOption ? ? ??. //2. ? , true? ? ?. if (fieldSelectOption == null || (fieldSelectOption != null && fieldSelectOption[i])) { f = fs.createEmptyField(); f.readRawFrom(bai); } else { bai.skipVIntData(); } // logger.debug("fill {} >> {}", i, f); } else { //? ? . f = fs.createEmptyField(); // logger.debug("fill {} >> empty", i); } if (f != null && indexable) { String multiValueDelimiter = fs.getMultiValueDelimiter(); try { f.parseIndexable(multiValueDelimiter); } catch (FieldDataParseException e) { throw new IOException(e); } } document.set(i, f); } document.setDocId(docNo + baseDocNo); return document; }
From source file:org.fastcatsearch.ir.document.DocumentWriter.java
public Document readDocument(int docNo) throws IOException, IRException { long prevPosPos = positionOutput.position(); long docPos = -1; try {// ww w . ja va2 s.c om long positionOffset = ((long) docNo) * IOUtil.SIZE_OF_LONG; positionOutput.seek(positionOffset); docPos = IOUtil.readLong(positionOutput.getRaf()); } finally { positionOutput.seek(prevPosPos); } // find a document block long prevDocPos = docOutput.position(); try { docOutput.seek(docPos); RandomAccessFile raf = docOutput.getRaf(); int len = IOUtil.readInt(raf); long n = raf.getFilePointer(); InputStream docInput = Channels.newInputStream(docOutput.getRaf().getChannel().position(n)); //2014-11-26 ? working ? ? ? GC ?? OOM ? ?. // Stream . InflaterInputStream decompressInputStream = null; inflaterOutput.reset(); int count = -1; try { BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len); boundedInputStream.setPropagateClose(false);// docInput . decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512); while ((count = decompressInputStream.read(workingBuffer)) != -1) { inflaterOutput.write(workingBuffer, 0, count); } } finally { decompressInputStream.close(); } } finally { docOutput.seek(prevDocPos); } BytesRef bytesRef = inflaterOutput.getBytesRef(); DataInput bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length); Document document = new Document(fields.size()); for (int i = 0; i < fields.size(); i++) { FieldSetting fs = fields.get(i); Field f = null; boolean hasValue = bai.readBoolean(); if (hasValue) { f = fs.createEmptyField(); f.readRawFrom(bai); } else { f = fs.createEmptyField(); } if (f != null) { String multiValueDelimiter = fs.getMultiValueDelimiter(); try { f.parseIndexable(multiValueDelimiter); } catch (FieldDataParseException e) { throw new IOException(e); } } document.add(f); } document.setDocId(docNo); return document; }
From source file:org.fcrepo.http.commons.responses.RangeRequestInputStream.java
/** * Creates a <code>FilterInputStream</code> * by assigning the argument <code>in</code> * to the field <code>this.in</code> so as * to remember it for later use.//from w ww.j a va2s. co m * * @param in the underlying input stream, or <code>null</code> if * this instance is to be created without an underlying stream. * @param skip the number of bytes to skip at the beginning of the stream * @param length the number of bytes from the inputstream to read */ public RangeRequestInputStream(final InputStream in, final long skip, final long length) throws IOException { super(new BoundedInputStream(new SkipInputStream(in, skip), length)); }
From source file:org.jboss.windup.util.RPMToZipTransformer.java
public static File convertRpmToZip(File file) throws Exception { LOG.info("File: " + file.getAbsolutePath()); FileInputStream fis = new FileInputStream(file); ReadableChannelWrapper in = new ReadableChannelWrapper(Channels.newChannel(fis)); InputStream uncompressed = new GZIPInputStream(fis); in = new ReadableChannelWrapper(Channels.newChannel(uncompressed)); String rpmZipName = file.getName(); rpmZipName = StringUtils.replace(rpmZipName, ".", "-"); rpmZipName = rpmZipName + ".zip"; String rpmZipPath = FilenameUtils.getFullPath(file.getAbsolutePath()); File rpmZipOutput = new File(rpmZipPath + File.separator + rpmZipName); LOG.info("Converting RPM: " + file.getName() + " to ZIP: " + rpmZipOutput.getName()); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(rpmZipOutput)); String rpmName = file.getName(); rpmName = StringUtils.replace(rpmName, ".", "-"); CpioHeader header;//w w w. java2 s .co m int total = 0; do { header = new CpioHeader(); total = header.read(in, total); if (header.getFileSize() > 0) { BoundedInputStream bis = new BoundedInputStream(uncompressed, header.getFileSize()); String relPath = FilenameUtils.separatorsToSystem(header.getName()); relPath = StringUtils.removeStart(relPath, "."); relPath = StringUtils.removeStart(relPath, "/"); relPath = rpmName + File.separator + relPath; relPath = StringUtils.replace(relPath, "\\", "/"); ZipEntry zipEntry = new ZipEntry(relPath); zos.putNextEntry(zipEntry); IOUtils.copy(bis, zos); } else { final int skip = header.getFileSize(); if (uncompressed.skip(skip) != skip) throw new RuntimeException("Skip failed."); } total += header.getFileSize(); } while (!header.isLast()); zos.flush(); zos.close(); return rpmZipOutput; }