下面列出了org.apache.http.nio.IOControl#java.io.PipedInputStream 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public DuplexAsync(Endpoint client_endpoint, Endpoint server_endpoint) throws Exception
{
this.client = client_endpoint;
this.server = server_endpoint;
client_input = (client_endpoint != null) ? client_endpoint.getInputStream() : null;
client_output = (client_endpoint != null) ? client_endpoint.getOutputStream() : null;
server_input = (server_endpoint != null) ? server_endpoint.getInputStream() : null;
server_output = (server_endpoint != null) ? server_endpoint.getOutputStream() : null;
flow_controlled_client_output = new PipedOutputStream();
flow_controlled_client_input = new PipedInputStream(flow_controlled_client_output, 65536);
flow_controlled_server_output = new PipedOutputStream();
flow_controlled_server_input = new PipedInputStream(flow_controlled_server_output, 65536);
client_to_server = createClientToServerSimplex(client_input, flow_controlled_server_output);
server_to_client = createServerToClientSimplex(server_input, flow_controlled_client_output);
disableDuplexEventListener();
}
/**
* Return InputStream from which a new generated resource bundle can be retrieved.
*/
public SupportBundle generateNewBundle(List<String> generatorNames, BundleType bundleType) throws IOException {
List<BundleContentGeneratorDefinition> defs = getRequestedDefinitions(generatorNames);
PipedInputStream inputStream = new PipedInputStream();
PipedOutputStream outputStream = new PipedOutputStream();
inputStream.connect(outputStream);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
executor.submit(() -> generateNewBundleInternal(defs, bundleType, zipOutputStream));
String bundleName = generateBundleName(bundleType);
String bundleKey = generateBundleDate(bundleType) + "/" + bundleName;
return new SupportBundle(
bundleKey,
bundleName,
inputStream
);
}
@Test
public void buildACommand() throws IOException {
PipedInputStream pis = new PipedInputStream();
BufferedInputStream bis = new BufferedInputStream(pis);
PipedOutputStream pos = new PipedOutputStream(pis);
RedisOutputStream ros = new RedisOutputStream(pos);
Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
ros.flush();
pos.close();
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
int b;
StringBuilder sb = new StringBuilder();
while ((b = bis.read()) != -1) {
sb.append((char) b);
}
assertEquals(expectedCommand, sb.toString());
}
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
public S3CommonPipedOutputStream( S3CommonFileSystem fileSystem, String bucketId, String key, int partSize ) throws IOException {
this.pipedInputStream = new PipedInputStream();
try {
this.pipedInputStream.connect( this );
} catch ( IOException e ) {
// FATAL, unexpected
throw new IOException( "could not connect to pipedInputStream", e );
}
this.s3AsyncTransferRunner = new S3AsyncTransferRunner();
this.bucketId = bucketId;
this.key = key;
this.fileSystem = fileSystem;
this.partSize = partSize;
}
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
private static void testJarList(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-tvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
static void redirectSystemOut() throws Exception {
PipedOutputStream pos = new PipedOutputStream();
PrintStream ps = new PrintStream(pos);
PipedInputStream pis = new PipedInputStream(pos);
final BufferedReader reader = new BufferedReader(new InputStreamReader(pis));
new Thread() {
public void run() {
try {
String line = null;
while ((line = reader.readLine()) != null) {
MvsConsole.wto(line,
MvsConsole.ROUTCDE_SYSPROG_INFORMATION,
MvsConsole.DESC_JOB_STATUS);
}
} catch(IOException ioe) {
// Pipe breaks when shutting down; ignore
}
}
}.start();
System.setOut(ps);
}
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
@Test
public void testUnrecoverable() throws Throwable {
try (PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos, 512);
RLPStream stream = new RLPStream(pis)) {
pos.write(0x81);
pos.write(0x00);
Iterator<RLPItem> iter = stream.iterator();
TestUtils.assertThrown(IllegalArgumentException.class, "invalid rlp for single byte @ 0", iter::hasNext);
try (RLPStream stream2 = new RLPStream(pis)) {
pos.write(0xf8);
pos.write(0x37);
Iterator<RLPItem> iter2 = stream2.iterator();
for (int i = 0; i < 3; i++) {
TestUtils.assertThrown(
IllegalArgumentException.class,
"long element data length must be 56 or greater; found: 55 for element @ 0",
iter2::hasNext
);
}
}
}
}
@Test
public void canHandleDataNotAlreadyPresentSeparatedByNewline()
throws Exception {
List<String> expected = Arrays.asList("a", "b", "c");
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
StreamReader reader = process(is, "\n", expected);
TimeUnit.SECONDS.sleep(2);
os.write("a\nb\nc\n".getBytes());
waitUntil(expected.size());
assertThat(received, is(expected));
reader.close();
}
public LineDisciplineTerminal(String name,
String type,
OutputStream masterOutput) throws IOException {
super(name, type);
PipedInputStream input = new LinePipedInputStream(PIPE_SIZE);
this.slaveInputPipe = new PipedOutputStream(input);
// This is a hack to fix a problem in gogo where closure closes
// streams for commands if they are instances of PipedInputStream.
// So we need to get around and make sure it's not an instance of
// that class by using a dumb FilterInputStream class to wrap it.
this.slaveInput = new FilterInputStream(input) {};
this.slaveOutput = new FilteringOutputStream();
this.masterOutput = masterOutput;
this.attributes = new Attributes();
this.size = new Size(160, 50);
}
@Override
public InputStream getInputStream() throws IOException {
final PipedInputStream pin = new PipedInputStream();
final PipedOutputStream pout = new PipedOutputStream(pin);
new Thread() {
public void run() {
try {
BarTransformer.transform(barXmlURL, pout);
} catch (Exception e) {
LOGGER.warn("Bundle cannot be generated");
} finally {
try {
pout.close();
} catch (IOException ignore) {
// if we get here something is very wrong
LOGGER.error("Bundle cannot be generated", ignore);
}
}
}
}.start();
return pin;
}
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
/**
* Instantiates a new microphone input stream.
*
* @param opusEncoded the opus encoded
*/
public MicrophoneInputStream(boolean opusEncoded) {
captureThread = new MicrophoneCaptureThread(this, opusEncoded);
if (opusEncoded == true) {
CONTENT_TYPE = ContentType.OPUS;
} else {
CONTENT_TYPE = ContentType.RAW;
}
os = new PipedOutputStream();
is = new PipedInputStream();
try {
is.connect(os);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
captureThread.start();
}
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
private static void testJarExtract(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-xvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
Main jarTool = new Main(out, System.err, "jar");
if (!jarTool.run(jarArgs)) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
private static void testJarList(String jarFile) throws IOException {
List<String> argList = new ArrayList<String>();
argList.add("-tvf");
argList.add(jarFile);
argList.add(JARBACKSLASH + File.separatorChar + DIR + File.separatorChar + FILENAME);
String jarArgs[] = new String[argList.size()];
jarArgs = argList.toArray(jarArgs);
PipedOutputStream pipedOutput = new PipedOutputStream();
PipedInputStream pipedInput = new PipedInputStream(pipedOutput);
PrintStream out = new PrintStream(pipedOutput);
int rc = JAR_TOOL.run(out, System.err, jarArgs);
if (rc != 0) {
fail("Could not list jar file.");
}
out.flush();
check(pipedInput.available() > 0);
}
@Before
public void setUp() throws Exception
{
PipedInputStream pipedInput = new PipedInputStream(1024*1024);
this.in = new DataInputStream(pipedInput);
this.out = new DataOutputStream(new PipedOutputStream(pipedInput));
}
public DemuxTester() throws IOException {
feeder = new PipedOutputStream();
feeder_input_stream = new PipedInputStream();
feeder_input_stream.connect(feeder);
stream = new DockerMultiplexedInputStream(feeder_input_stream);
sink = new ByteArrayOutputStream();
eof = false;
exc = null;
thread = new Thread(this);
thread.start();
}
@Before
public void setup() throws Exception {
in = new PipedInputStream(1000);
out = new PipedOutputStream(in);
mIn = new DataInputViewStreamWrapper(in);
mOut = new DataOutputViewStreamWrapper(out);
}
public AbstractWindowsTerminal(boolean consumeCP, OutputStream output, String name, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(name, "windows", signalHandler);
PipedInputStream input = new PipedInputStream(PIPE_SIZE);
this.slaveInputPipe = new PipedOutputStream(input);
this.input = new FilterInputStream(input) {};
this.cpConsumer = consumeCP ? new ConsoleOutput() : null;
this.output = output;
String encoding = getConsoleEncoding();
if (encoding == null) {
encoding = Charset.defaultCharset().name();
}
this.writer = new PrintWriter(new OutputStreamWriter(this.output, encoding));
// Attributes
attributes.setLocalFlag(Attributes.LocalFlag.ISIG, true);
attributes.setControlChar(Attributes.ControlChar.VINTR, ctrl('C'));
attributes.setControlChar(Attributes.ControlChar.VEOF, ctrl('D'));
attributes.setControlChar(Attributes.ControlChar.VSUSP, ctrl('Z'));
// Handle signals
if (nativeSignals) {
for (final Signal signal : Signal.values()) {
nativeHandlers.put(signal,
Signals.register(signal.name(), () -> raise(signal)));
}
}
pump = new Thread(this::pump, "WindowsStreamPump");
pump.setDaemon(true);
pump.start();
closer = this::close;
ShutdownHooks.add(closer);
}
@Before
public void setup() throws Exception {
in = new PipedInputStream(1000);
out = new PipedOutputStream(in);
mIn = new DataInputViewStreamWrapper(in);
mOut = new DataOutputViewStreamWrapper(out);
}
public XmlOutputStreamFilter(final OutputStream outputStream, final XmlEventTransformer eventTransformer) throws IOException {
super();
pipedInputStream = new PipedInputStream(this);
Callable<Boolean> output = new Callable<Boolean>() {
public Boolean call() {
try {
XMLEventReader eventReader = new com.fasterxml.aalto.stax.InputFactoryImpl()
.createXMLEventReader(pipedInputStream);
XMLEventWriter eventWriter = new com.fasterxml.aalto.stax.OutputFactoryImpl()
.createXMLEventWriter(outputStream);
XMLEvent event = null;
while (eventReader.hasNext()) {
event = eventReader.nextEvent();
event = eventTransformer.transform(event);
eventWriter.add(event);
eventWriter.flush();
}
eventReader.close();
eventWriter.flush();
eventWriter.close();
outputStream.flush();
outputStream.close();
} catch (XMLStreamException | IOException e) {
throw new IllegalStateException(e);
}
return true;
}
};
future = Executors.newSingleThreadExecutor().submit(output);
}
@Override
public OutputStream getOutputStream() throws IOException {
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream();
in.connect(out);
requestBuilder.body(in);
return out;
}
public void start() throws IOException {
if (shouldCollectData) {
return;
}
shouldCollectData = true;
in = new PipedInputStream(POOL_SIZE);
out = new PipedOutputStream(in);
}
/**
* Gets the input stream for the file.
*
* @param api The discord api instance.
* @return The input stream for the file.
* @throws IOException If an IO error occurs.
*/
public InputStream asInputStream(DiscordApi api) throws IOException {
if (fileAsBufferedImage != null) {
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
api.getThreadPool().getExecutorService().submit(() -> {
try {
ImageIO.write(fileAsBufferedImage, getFileType(), pos);
pos.close();
} catch (Throwable t) {
logger.error("Failed to process buffered image file!", t);
}
});
return pis;
}
if (fileAsFile != null) {
return new FileInputStream(fileAsFile);
}
if (fileAsIcon != null || fileAsUrl != null) {
URL url = fileAsUrl == null ? fileAsIcon.getUrl() : fileAsUrl;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setRequestProperty("User-Agent", Javacord.USER_AGENT);
return conn.getInputStream();
}
if (fileAsByteArray != null) {
return new ByteArrayInputStream(fileAsByteArray);
}
if (fileAsInputStream != null) {
return fileAsInputStream;
}
throw new IllegalStateException("No file variant is set");
}
private static InputStream getInputStream(final AwContentsClient contentClient)
throws IOException {
final PipedInputStream inputStream = new PipedInputStream();
final PipedOutputStream outputStream = new PipedOutputStream(inputStream);
// Send the request to UI thread to callback to the client, and if it provides a
// valid bitmap bounce on to the worker thread pool to compress it into the piped
// input/output stream.
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
final Bitmap defaultVideoPoster = contentClient.getDefaultVideoPoster();
if (defaultVideoPoster == null) {
closeOutputStream(outputStream);
return;
}
AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
defaultVideoPoster.compress(Bitmap.CompressFormat.PNG, 100,
outputStream);
outputStream.flush();
} catch (IOException e) {
Log.e(TAG, null, e);
} finally {
closeOutputStream(outputStream);
}
}
});
}
});
return inputStream;
}
public RtcpDeinterleaver(InputStream inputStream) {
mInputStream = inputStream;
mPipedInputStream = new PipedInputStream(4096);
try {
mPipedOutputStream = new PipedOutputStream(mPipedInputStream);
} catch (IOException e) {}
mBuffer = new byte[1024];
new Thread(this).start();
}