use of java.io.PipedOutputStream in project spring-integration by spring-projects.
the class SftpServerOutboundTests method testInt3047ConcurrentSharedSession.
@Test
public void testInt3047ConcurrentSharedSession() throws Exception {
final Session<?> session1 = this.sessionFactory.getSession();
final Session<?> session2 = this.sessionFactory.getSession();
final PipedInputStream pipe1 = new PipedInputStream();
PipedOutputStream out1 = new PipedOutputStream(pipe1);
final PipedInputStream pipe2 = new PipedInputStream();
PipedOutputStream out2 = new PipedOutputStream(pipe2);
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Executors.newSingleThreadExecutor().execute(() -> {
try {
session1.write(pipe1, "foo.txt");
} catch (IOException e) {
e.printStackTrace();
}
latch1.countDown();
});
Executors.newSingleThreadExecutor().execute(() -> {
try {
session2.write(pipe2, "bar.txt");
} catch (IOException e) {
e.printStackTrace();
}
latch2.countDown();
});
out1.write('a');
out2.write('b');
out1.write('c');
out2.write('d');
out1.write('e');
out2.write('f');
out1.close();
out2.close();
assertTrue(latch1.await(10, TimeUnit.SECONDS));
assertTrue(latch2.await(10, TimeUnit.SECONDS));
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
session1.read("foo.txt", bos1);
session2.read("bar.txt", bos2);
assertEquals("ace", new String(bos1.toByteArray()));
assertEquals("bdf", new String(bos2.toByteArray()));
session1.remove("foo.txt");
session2.remove("bar.txt");
session1.close();
session2.close();
}
use of java.io.PipedOutputStream in project sw360portal by sw360.
the class AttachmentStreamConnector method getAttachmentBundleStream.
/**
* It is highly recommended to close this stream after using to avoid connection leak
*/
public <T> InputStream getAttachmentBundleStream(Set<AttachmentContent> attachments, User user, T context) throws IOException, SW360Exception {
assertNotNull(context);
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
new Thread(() -> {
byte[] buffer = new byte[1024];
int length;
try (ZipOutputStream zip = new ZipOutputStream(out)) {
for (AttachmentContent attachment : attachments) {
// TODO: handle attachments with equal name
ZipEntry zipEntry = new ZipEntry(attachment.getFilename());
zip.putNextEntry(zipEntry);
try (InputStream attachmentStream = getAttachmentStream(attachment, user, context)) {
while ((length = attachmentStream.read(buffer)) >= 0) {
zip.write(buffer, 0, length);
}
} catch (TException e) {
log.error("failed to get AttachmentStream, maybe due to permission problems", e);
}
zip.closeEntry();
}
} catch (IOException e) {
log.error("failed to write zip stream", e);
}
}).start();
return in;
}
use of java.io.PipedOutputStream in project directory-ldap-api by apache.
the class SchemaParser method init.
/**
* Initializes a parser and its plumbing.
*
* @throws java.io.IOException if a pipe cannot be formed.
*/
public synchronized void init() throws IOException {
parserIn = new PipedOutputStream();
PipedInputStream in = new PipedInputStream();
parserIn.connect(in);
antlrSchemaConverterLexer lexer = new antlrSchemaConverterLexer(in);
parser = new antlrSchemaConverterParser(lexer);
}
use of java.io.PipedOutputStream in project com.juick.xmpp by juick.
the class ComponentTests method componentTest.
@Test
public void componentTest() throws IOException, InterruptedException, XmlPullParserException {
ServerSocket serverSocket = mock(ServerSocket.class);
Socket client = mock(Socket.class);
Socket server = mock(Socket.class);
when(serverSocket.accept()).thenReturn(server);
PipedInputStream serverInputStream = new PipedInputStream();
PipedOutputStream clientOutputStream = new PipedOutputStream(serverInputStream);
PipedInputStream clientInputStream = new PipedInputStream();
PipedOutputStream serverOutputStream = new PipedOutputStream(clientInputStream);
when(client.getInputStream()).thenReturn(clientInputStream);
when(client.getOutputStream()).thenReturn(clientOutputStream);
when(server.getInputStream()).thenReturn(serverInputStream);
when(server.getOutputStream()).thenReturn(serverOutputStream);
Jid localhost = Jid.of("localhost");
StreamListener serverListener = mock(StreamListener.class);
when(serverListener.filter(null, localhost)).thenReturn(false);
final StreamComponentServer[] componentServer = new StreamComponentServer[1];
executorService.submit(() -> {
try {
Socket clientSocket = serverSocket.accept();
componentServer[0] = new StreamComponentServer(clientSocket.getInputStream(), clientSocket.getOutputStream(), "secret");
componentServer[0].addListener(serverListener);
componentServer[0].connect();
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
}
});
StreamComponent component = new StreamComponent(localhost, client.getInputStream(), client.getOutputStream(), "secret");
component.addListener(testListener);
component.addListener(messageListener);
executorService.submit(component::connect);
verify(testListener, timeout(5000).times(1)).ready();
Message msg = new Message();
msg.from = Jid.of("vasya@localhost");
msg.to = Jid.of("masha@localhost");
msg.body = "test";
componentServer[0].send(msg);
verify(messageListener, times(1)).onMessage(messageCaptor.capture());
Message received = messageCaptor.getValue();
assertEquals("test", received.body);
component.send("<yo:people/>");
verify(testListener, timeout(5000).times(1)).fail(exceptionArgumentCaptor.capture());
assertEquals("invalid-xml", exceptionArgumentCaptor.getValue().getMessage());
}
use of java.io.PipedOutputStream in project i2p.i2p by i2p.
the class EepGet method doFetch.
/**
* single fetch
* @param timeout may be null
*/
protected void doFetch(SocketTimeout timeout) throws IOException {
_aborted = false;
readHeaders();
if (_aborted)
throw new IOException("Timed out reading the HTTP headers");
if (timeout != null) {
timeout.resetTimer();
if (_fetchInactivityTimeout > 0)
timeout.setInactivityTimeout(_fetchInactivityTimeout);
else
timeout.setInactivityTimeout(INACTIVITY_TIMEOUT);
}
// _proxy is null when extended by I2PSocketEepGet
if (_proxy != null && !_shouldProxy) {
// we only set the soTimeout before the headers if not proxied
if (_fetchInactivityTimeout > 0)
_proxy.setSoTimeout(_fetchInactivityTimeout);
else
_proxy.setSoTimeout(INACTIVITY_TIMEOUT);
}
if (_redirectLocation != null) {
// we also are here after a 407
try {
if (_redirectLocation.startsWith("http://")) {
_actualURL = _redirectLocation;
} else if (_redirectLocation.startsWith("https://")) {
throw new IOException("Redirect to https unsupported");
} else {
// the Location: field has been required to be an absolute URI at least since
// RFC 1945 (HTTP/1.0 1996), so it isn't clear what the point of this is.
// This oddly adds a ":" even if no port, but that seems to work.
URI url = new URI(_actualURL);
String host = url.getHost();
if (host == null)
throw new MalformedURLException("Redirected to invalid URL");
int port = url.getPort();
if (port < 0)
port = 80;
if (_redirectLocation.startsWith("/"))
_actualURL = "http://" + host + ":" + port + _redirectLocation;
else
// this blows up completely on a redirect to https://, for example
_actualURL = "http://" + host + ":" + port + "/" + _redirectLocation;
}
} catch (URISyntaxException use) {
IOException ioe = new MalformedURLException("Redirected to invalid URL");
ioe.initCause(use);
throw ioe;
}
AuthState as = _authState;
if (_responseCode == 407) {
if (!_shouldProxy)
throw new IOException("Proxy auth response from non-proxy");
if (as == null)
throw new IOException("Proxy requires authentication");
if (as.authSent)
// ignore stale
throw new IOException("Proxy authentication failed");
if (_log.shouldLog(Log.INFO))
_log.info("Adding auth");
// actually happens in getRequest()
} else {
_redirects++;
if (_redirects > 5)
throw new IOException("Too many redirects: to " + _redirectLocation);
if (_log.shouldLog(Log.INFO))
_log.info("Redirecting to " + _redirectLocation);
if (as != null)
as.authSent = false;
}
// reset some important variables, we don't want to save the values from the redirect
_bytesRemaining = -1;
_redirectLocation = null;
_etag = _etagOrig;
_lastModified = _lastModifiedOrig;
_contentType = null;
_encodingChunked = false;
sendRequest(timeout);
doFetch(timeout);
return;
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Headers read completely, reading " + _bytesRemaining);
boolean strictSize = (_bytesRemaining >= 0);
// If minimum or maximum size defined, ensure they aren't exceeded
if ((_minSize > 0) && (_bytesRemaining < _minSize))
throw new IOException("HTTP response size " + _bytesRemaining + " violates minimum of " + _minSize + " bytes");
if ((_maxSize > -1) && (_bytesRemaining > _maxSize))
throw new IOException("HTTP response size " + _bytesRemaining + " violates maximum of " + _maxSize + " bytes");
Thread pusher = null;
_decompressException = null;
if (_isGzippedResponse) {
if (_log.shouldInfo())
_log.info("Gzipped response, starting decompressor");
PipedInputStream pi = new PipedInputStream(64 * 1024);
PipedOutputStream po = new PipedOutputStream(pi);
pusher = new I2PAppThread(new Gunzipper(pi, _out), "EepGet Decompressor");
_out = po;
pusher.start();
}
int remaining = (int) _bytesRemaining;
byte[] buf = new byte[16 * 1024];
while (_keepFetching && ((remaining > 0) || !strictSize) && !_aborted) {
int toRead = buf.length;
if (strictSize && toRead > remaining)
toRead = remaining;
int read = _proxyIn.read(buf, 0, toRead);
if (read == -1)
break;
if (timeout != null)
timeout.resetTimer();
_out.write(buf, 0, read);
_bytesTransferred += read;
if (// could transfer a little over maxSize
(_maxSize > -1) && (_alreadyTransferred + read > _maxSize))
throw new IOException("Bytes transferred " + (_alreadyTransferred + read) + " violates maximum of " + _maxSize + " bytes");
remaining -= read;
if (remaining == 0 && _encodingChunked) {
int char1 = _proxyIn.read();
if (char1 == '\r') {
int char2 = _proxyIn.read();
if (char2 == '\n') {
remaining = (int) readChunkLength();
} else {
_out.write(char1);
_out.write(char2);
_bytesTransferred += 2;
remaining -= 2;
read += 2;
}
} else {
_out.write(char1);
_bytesTransferred++;
remaining--;
read++;
}
}
if (timeout != null)
timeout.resetTimer();
if (// else chunked?
_bytesRemaining >= read)
_bytesRemaining -= read;
if (read > 0) {
for (int i = 0; i < _listeners.size(); i++) _listeners.get(i).bytesTransferred(_alreadyTransferred, read, _bytesTransferred, _encodingChunked ? -1 : _bytesRemaining, _url);
// This seems necessary to properly resume a partial download into a stream,
// as nothing else increments _alreadyTransferred, and there's no file length to check.
// Do this after calling the listeners to keep the total correct
_alreadyTransferred += read;
}
}
if (_out != null)
_out.close();
_out = null;
if (_isGzippedResponse) {
try {
pusher.join();
} catch (InterruptedException ie) {
}
pusher = null;
if (_decompressException != null) {
// we can't resume from here
_keepFetching = false;
throw _decompressException;
}
}
if (_aborted)
throw new IOException("Timed out reading the HTTP data");
if (timeout != null)
timeout.cancel();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Done transferring " + _bytesTransferred + " (ok? " + !_transferFailed + ")");
if (_transferFailed) {
// 404, etc - transferFailed is called after all attempts fail, by fetch() above
if (!_listeners.isEmpty()) {
String s;
if (_responseText != null)
s = "Attempt failed: " + _responseCode + ' ' + _responseText;
else
s = "Attempt failed: " + _responseCode;
Exception e = new IOException(s);
for (int i = 0; i < _listeners.size(); i++) {
_listeners.get(i).attemptFailed(_url, _bytesTransferred, _bytesRemaining, _currentAttempt, _numRetries, e);
}
}
} else if ((_minSize > 0) && (_alreadyTransferred < _minSize)) {
throw new IOException("Bytes transferred " + _alreadyTransferred + " violates minimum of " + _minSize + " bytes");
} else if ((_bytesRemaining == -1) || (remaining == 0)) {
for (int i = 0; i < _listeners.size(); i++) _listeners.get(i).transferComplete(_alreadyTransferred, _bytesTransferred, _encodingChunked ? -1 : _bytesRemaining, _url, _outputFile, _notModified);
} else {
throw new IOException("Disconnection on attempt " + _currentAttempt + " after " + _bytesTransferred);
}
}
Aggregations