下面列出了怎么用org.apache.commons.io.input.TeeInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
private TeeInputStream tee = new TeeInputStream(RequestWrapper.super.getInputStream(), bos);
@Override
public int read() throws IOException {
return tee.read();
}
};
}
@Override
public void run() {
logger.debug("Checking for a change");
if (targetChanged()) {
logger.debug("Target changed, checking if it's different than current flow.");
try (FileInputStream configFile = new FileInputStream(configFilePath.toFile());
ByteArrayOutputStream pipedOutputStream = new ByteArrayOutputStream();
TeeInputStream teeInputStream = new TeeInputStream(configFile, pipedOutputStream)) {
if (differentiator.isNew(teeInputStream)) {
logger.debug("New change, notifying listener");
// Fill the byteArrayOutputStream with the rest of the request data
while (teeInputStream.available() != 0) {
teeInputStream.read();
}
ByteBuffer newConfig = ByteBuffer.wrap(pipedOutputStream.toByteArray());
ByteBuffer readOnlyNewConfig = newConfig.asReadOnlyBuffer();
configurationChangeNotifier.notifyListeners(readOnlyNewConfig);
logger.debug("Listeners notified");
}
} catch (Exception e) {
logger.error("Could not successfully notify listeners.", e);
}
}
}
private InputStream getConfigInputStream() throws IOException {
MCRConfigurationInputStream configurationInputStream = MCRConfigurationInputStream
.getMyCoRePropertiesInstance();
File configFile = MCRConfigurationDir.getConfigFile("mycore.active.properties");
if (configFile != null) {
FileOutputStream fout = new FileOutputStream(configFile);
return new TeeInputStream(configurationInputStream, fout, true);
}
return configurationInputStream;
}
public InputStream fetchInputStream(URL url, String prefix, String suffix) throws IOException {
createCacheDir(prefix);
File localDatasourceFile = urlToLocalFile(url, prefix, suffix);
log.info("Fetching local file: {}", localDatasourceFile.getCanonicalPath());
if (!localDatasourceFile.exists()){
log.info("Local file not found: {} \nDownloading external resource: {}",
localDatasourceFile.getCanonicalPath(), url.toString());
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 " +
"(KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
if (suffix.equals(".json")) { urlConnection.setRequestProperty("Accept", "application/json"); }
urlConnection.connect();
if (urlConnection instanceof HttpURLConnection) {
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
int responseCode = httpURLConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException(String.format("Cannot get the stream from the specified URL: %s\n%d: %s",
url.getPath(), responseCode, httpURLConnection.getResponseMessage()));
}
}
return new TeeInputStream(urlConnection.getInputStream(), new FileOutputStream(localDatasourceFile));
} else {
return new FileInputStream(localDatasourceFile);
}
}
public ResettableInputStream(InputStream in) throws IOException {
InputStream unwrappedInput = EnhancedBufferedInputStream.tryUnwrap(in);
if (!(unwrappedInput instanceof FileInputStream)) {
tmpFile = Files.createTempFile("vlt_tmp", null);
tmpOutputStream = new BufferedOutputStream(Files.newOutputStream(tmpFile));
log.debug("Caching input stream in temp file '{}' for later evaluation by another validator", tmpFile);
currentInput = new TeeInputStream(in, tmpOutputStream);
} else {
tmpFile = null;
tmpOutputStream = null;
currentInput = in;
}
}
@SuppressWarnings("resource")
@Test
public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams() throws IOException {
final String str = "Hello World.";
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
FilterOutputStream teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
new TeeInputStream(inputStream, teeOutputStream, true).read(new byte[str.length()]);
Assert.assertEquals(str, String.valueOf(outputStream1));
Assert.assertEquals(str, String.valueOf(outputStream2));
}
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
logRequest(request);
baseRequest.setHandled(true);
if (POST.equals(request.getMethod())) {
int statusCode;
String responseText;
try (ByteArrayOutputStream pipedOutputStream = new ByteArrayOutputStream();
TeeInputStream teeInputStream = new TeeInputStream(request.getInputStream(), pipedOutputStream)) {
if (differentiator.isNew(teeInputStream)) {
// Fill the pipedOutputStream with the rest of the request data
while (teeInputStream.available() != 0) {
teeInputStream.read();
}
ByteBuffer newConfig = ByteBuffer.wrap(pipedOutputStream.toByteArray());
ByteBuffer readOnlyNewConfig = newConfig.asReadOnlyBuffer();
Collection<ListenerHandleResult> listenerHandleResults = configurationChangeNotifier.notifyListeners(readOnlyNewConfig);
statusCode = 200;
for (ListenerHandleResult result : listenerHandleResults) {
if (!result.succeeded()) {
statusCode = 500;
break;
}
}
responseText = getPostText(listenerHandleResults);
} else {
statusCode = 409;
responseText = "Request received but instance is already running this config.";
}
writeOutput(response, responseText, statusCode);
}
} else if (GET.equals(request.getMethod())) {
writeOutput(response, GET_TEXT, 200);
} else {
writeOutput(response, OTHER_TEXT, 404);
}
}
DelegatedInputStream(ServletInputStream servletStream) {
this.servletStream = servletStream;
this.teeStream = new TeeInputStream(servletStream, contentStream);
}
private InputStreamReader spawnSpy(InputStream in, PipedOutputStream pipeout) throws InterruptedException {
return new InputStreamReader(new TeeInputStream(in,pipeout));
}