Java Code Examples for java.io.PipedInputStream#close()

The following examples show how to use java.io.PipedInputStream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestMRJobClient.java    From hadoop with Apache License 2.0 6 votes vote down vote up
protected void verifyJobPriority(String jobId, String priority,
    Configuration conf, CLI jc) throws Exception {
  PipedInputStream pis = new PipedInputStream();
  PipedOutputStream pos = new PipedOutputStream(pis);
  int exitCode = runTool(conf, jc, new String[] { "-list", "all" }, pos);
  assertEquals("Exit code", 0, exitCode);
  BufferedReader br = new BufferedReader(new InputStreamReader(pis));
  String line;
  while ((line = br.readLine()) != null) {
    LOG.info("line = " + line);
    if (!line.contains(jobId)) {
      continue;
    }
    assertTrue(line.contains(priority));
    break;
  }
  pis.close();
}
 
Example 2
Source File: TestJMXGet.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private static boolean checkPrintAllValues(JMXGet jmx) throws Exception {
  int size = 0; 
  byte[] bytes = null;
  String pattern = "List of all the available keys:";
  PipedOutputStream pipeOut = new PipedOutputStream();
  PipedInputStream pipeIn = new PipedInputStream(pipeOut);
  System.setErr(new PrintStream(pipeOut));
  jmx.printAllValues();
  if ((size = pipeIn.available()) != 0) {
    bytes = new byte[size];
    pipeIn.read(bytes, 0, bytes.length);            
  }
  pipeOut.close();
  pipeIn.close();
  return bytes != null ? new String(bytes).contains(pattern) : false;
}
 
Example 3
Source File: AbstractSshSupport.java    From sshd-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
void sshCall(String username, String password, SshExecutor executor, String channelType) {
    try {
        Session session = openSession(username, password);
        Channel channel = session.openChannel(channelType);
        PipedInputStream pis = new PipedInputStream();
        PipedOutputStream pos = new PipedOutputStream();
        channel.setInputStream(new PipedInputStream(pos));
        channel.setOutputStream(new PipedOutputStream(pis));
        channel.connect();
        try {
            executor.execute(pis, pos);
        } finally {
            pis.close();
            pos.close();
            channel.disconnect();
            session.disconnect();
        }
    } catch (JSchException | IOException ex) {
        fail(ex.toString());
    }
}
 
Example 4
Source File: TestMRJobClient.java    From big-c with Apache License 2.0 6 votes vote down vote up
protected void verifyJobPriority(String jobId, String priority,
    Configuration conf, CLI jc) throws Exception {
  PipedInputStream pis = new PipedInputStream();
  PipedOutputStream pos = new PipedOutputStream(pis);
  int exitCode = runTool(conf, jc, new String[] { "-list", "all" }, pos);
  assertEquals("Exit code", 0, exitCode);
  BufferedReader br = new BufferedReader(new InputStreamReader(pis));
  String line;
  while ((line = br.readLine()) != null) {
    LOG.info("line = " + line);
    if (!line.contains(jobId)) {
      continue;
    }
    assertTrue(line.contains(priority));
    break;
  }
  pis.close();
}
 
Example 5
Source File: TestJMXGet.java    From big-c with Apache License 2.0 6 votes vote down vote up
private static boolean checkPrintAllValues(JMXGet jmx) throws Exception {
  int size = 0; 
  byte[] bytes = null;
  String pattern = "List of all the available keys:";
  PipedOutputStream pipeOut = new PipedOutputStream();
  PipedInputStream pipeIn = new PipedInputStream(pipeOut);
  System.setErr(new PrintStream(pipeOut));
  jmx.printAllValues();
  if ((size = pipeIn.available()) != 0) {
    bytes = new byte[size];
    pipeIn.read(bytes, 0, bytes.length);            
  }
  pipeOut.close();
  pipeIn.close();
  return bytes != null ? new String(bytes).contains(pattern) : false;
}
 
Example 6
Source File: OutputCollectorImpl.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
private <T> T cloneObj(T t) throws IOException
{
  Serializer<T> keySerializer;
  Class<T> keyClass;
  PipedInputStream pis = new PipedInputStream();
  PipedOutputStream pos = new PipedOutputStream(pis);
  keyClass = (Class<T>)t.getClass();
  keySerializer = serializationFactory.getSerializer(keyClass);
  keySerializer.open(pos);
  keySerializer.serialize(t);
  Deserializer<T> keyDesiralizer = serializationFactory.getDeserializer(keyClass);
  keyDesiralizer.open(pis);
  T clonedArg0 = keyDesiralizer.deserialize(null);
  pos.close();
  pis.close();
  keySerializer.close();
  keyDesiralizer.close();
  return clonedArg0;

}
 
Example 7
Source File: TestJobClient.java    From RDFS with Apache License 2.0 6 votes vote down vote up
private void verifyJobPriority(String jobId, String priority)
                          throws Exception {
  PipedInputStream pis = new PipedInputStream();
  PipedOutputStream pos = new PipedOutputStream(pis);
  int exitCode = runTool(createJobConf(), new JobClient(),
      new String[] { "-list", "all" },
      pos);
  assertEquals("Exit code", 0, exitCode);
  BufferedReader br = new BufferedReader(new InputStreamReader(pis));
  String line = null;
  while ((line=br.readLine()) != null) {
    LOG.info("line = " + line);
    if (!line.startsWith(jobId)) {
      continue;
    }
    assertTrue(line.contains(priority));
    break;
  }
  pis.close();
}
 
Example 8
Source File: TestJobClient.java    From hadoop-gpu with Apache License 2.0 6 votes vote down vote up
private void verifyJobPriority(String jobId, String priority)
                          throws Exception {
  PipedInputStream pis = new PipedInputStream();
  PipedOutputStream pos = new PipedOutputStream(pis);
  int exitCode = runTool(createJobConf(), new JobClient(),
      new String[] { "-list", "all" },
      pos);
  assertEquals("Exit code", 0, exitCode);
  BufferedReader br = new BufferedReader(new InputStreamReader(pis));
  String line = null;
  while ((line=br.readLine()) != null) {
    LOG.info("line = " + line);
    if (!line.startsWith(jobId)) {
      continue;
    }
    assertTrue(line.contains(priority));
    break;
  }
  pis.close();
}
 
Example 9
Source File: TestTools.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void checkOutput(String[] args, String pattern, PrintStream out,
    Class<?> clazz) {       
  ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
  try {
    PipedOutputStream pipeOut = new PipedOutputStream();
    PipedInputStream pipeIn = new PipedInputStream(pipeOut, PIPE_BUFFER_SIZE);
    if (out == System.out) {
      System.setOut(new PrintStream(pipeOut));
    } else if (out == System.err) {
      System.setErr(new PrintStream(pipeOut));
    }

    if (clazz == DelegationTokenFetcher.class) {
      expectDelegationTokenFetcherExit(args);
    } else if (clazz == JMXGet.class) {
      expectJMXGetExit(args);
    } else if (clazz == DFSAdmin.class) {
      expectDfsAdminPrint(args);
    }
    pipeOut.close();
    ByteStreams.copy(pipeIn, outBytes);      
    pipeIn.close();
    assertTrue(new String(outBytes.toByteArray()).contains(pattern));            
  } catch (Exception ex) {
    fail("checkOutput error " + ex);
  }
}
 
Example 10
Source File: LocaleUtilitiesImpl.java    From BiglyBT with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void integrateLocalisedMessageBundle(Properties p) {
	// Surely there's a more convenient way of doing this?
	ResourceBundle rb = null;
	try {
		PipedInputStream in_stream = new PipedInputStream();
		PipedOutputStream out_stream = new PipedOutputStream(in_stream);
		p.store(out_stream, "");
		out_stream.close();
		rb = new PropertyResourceBundle(in_stream);
		in_stream.close();
	}
	catch (IOException ioe) {return;}
	integrateLocalisedMessageBundle(rb);
}
 
Example 11
Source File: TestTools.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void checkOutput(String[] args, String pattern, PrintStream out,
    Class<?> clazz) {       
  ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
  try {
    PipedOutputStream pipeOut = new PipedOutputStream();
    PipedInputStream pipeIn = new PipedInputStream(pipeOut, PIPE_BUFFER_SIZE);
    if (out == System.out) {
      System.setOut(new PrintStream(pipeOut));
    } else if (out == System.err) {
      System.setErr(new PrintStream(pipeOut));
    }

    if (clazz == DelegationTokenFetcher.class) {
      expectDelegationTokenFetcherExit(args);
    } else if (clazz == JMXGet.class) {
      expectJMXGetExit(args);
    } else if (clazz == DFSAdmin.class) {
      expectDfsAdminPrint(args);
    }
    pipeOut.close();
    ByteStreams.copy(pipeIn, outBytes);      
    pipeIn.close();
    assertTrue(new String(outBytes.toByteArray()).contains(pattern));            
  } catch (Exception ex) {
    fail("checkOutput error " + ex);
  }
}
 
Example 12
Source File: OdfPackage.java    From incubator-taverna-language with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the InputStream containing whole OdfPackage.
 *
 * @return the ODF package as input stream
 * @throws java.lang.Exception
 *             - if the package could not be read
 */
public InputStream getInputStream() throws Exception {
	final PipedOutputStream os = new PipedOutputStream();
	final PipedInputStream is = new PipedInputStream();

	is.connect(os);

	Thread thread1 = new Thread() {
		@Override
		public void run() {
			try {
				save(os, mBaseURI);
			} catch (Exception e) {
			}
		}
	};

	Thread thread2 = new Thread() {
		@Override
		public void run() {
			try {
				BufferedInputStream bis = new BufferedInputStream(is,
						PAGE_SIZE);
				BufferedOutputStream bos = new BufferedOutputStream(os,
						PAGE_SIZE);
				StreamHelper.stream(bis, bos);
				is.close();
				os.close();
			} catch (Exception ie) {
			}
		}
	};

	thread1.start();
	thread2.start();

	return is;
}
 
Example 13
Source File: SSHShellUtil.java    From mcg-helper with Apache License 2.0 4 votes vote down vote up
public static String execute(String ip, int port, String userName, String password, String secretKey, String shell) throws JSchException, IOException {
		String response = null;
		JSch.setLogger(new ShellLogger());
		JSch jsch = new JSch();
		Session session = jsch.getSession(userName, ip, port);
		UserInfo ui = null;
		if(StringUtils.isEmpty(secretKey)) {
			ui = new SSHUserInfo(password);
		} else {
			ui = new SSHGoogleAuthUserInfo(secretKey, password);
		}
		session.setUserInfo(ui);
		session.connect(6000);

		Channel channel = session.openChannel("shell");
		PipedInputStream pipedInputStream = new PipedInputStream();
		PipedOutputStream pipedOutputStream = new PipedOutputStream();
		pipedOutputStream.connect(pipedInputStream);
		
		Thread thread = new Thread(new MonitorShellUser(channel, shell, pipedOutputStream));
		thread.start();
		
		channel.setInputStream(pipedInputStream);
		
		PipedOutputStream shellPipedOutputStream = new PipedOutputStream();
		PipedInputStream receiveStream = new PipedInputStream(); 
		shellPipedOutputStream.connect(receiveStream);
		
		channel.setOutputStream(shellPipedOutputStream);
		((ChannelShell)channel).setPtyType("vt100", 160, 24, 1000, 480);   // dumb
		//((ChannelShell)channel).setTerminalMode("binary".getBytes(Constants.CHARSET));
	//	((ChannelShell)channel).setEnv("LANG", "zh_CN.UTF-8");
		try {
			channel.connect();
			response = IOUtils.toString(receiveStream, "UTF-8");
		}finally {
//			if(channel.isClosed()) {
				pipedOutputStream.close();
				pipedInputStream.close();
				shellPipedOutputStream.close();
				receiveStream.close();
				channel.disconnect();
				session.disconnect();
			}
//		}
			
		return response;
	}
 
Example 14
Source File: LargeTarTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
protected void createLargeFile(final String path, final String name) throws Exception {
    final long _1K = 1024;
    final long _1M = 1024 * _1K;
    // long _256M = 256 * _1M;
    // long _512M = 512 * _1M;
    final long _1G = 1024 * _1M;

    // File size of 3 GB
    final long fileSize = 3 * _1G;

    final File tarGzFile = new File(path + name + ".tar.gz");

    if (!tarGzFile.exists()) {
        System.out.println(
                "This test is a bit slow. It needs to write 3GB of data as a compressed file (approx. 3MB) to your hard drive");

        final PipedOutputStream outTarFileStream = new PipedOutputStream();
        final PipedInputStream inTarFileStream = new PipedInputStream(outTarFileStream);

        final Thread source = new Thread() {

            @Override
            public void run() {
                final byte ba_1k[] = new byte[(int) _1K];
                for (int i = 0; i < ba_1k.length; i++) {
                    ba_1k[i] = 'a';
                }
                try {
                    final TarArchiveOutputStream outTarStream = (TarArchiveOutputStream) new ArchiveStreamFactory()
                            .createArchiveOutputStream(ArchiveStreamFactory.TAR, outTarFileStream);
                    // Create archive contents
                    final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name + ".txt");
                    tarArchiveEntry.setSize(fileSize);

                    outTarStream.putArchiveEntry(tarArchiveEntry);
                    for (long i = 0; i < fileSize; i += ba_1k.length) {
                        outTarStream.write(ba_1k);
                    }
                    outTarStream.closeArchiveEntry();
                    outTarStream.close();
                    outTarFileStream.close();
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }

        };
        source.start();

        // Create compressed archive
        final OutputStream outGzipFileStream = new FileOutputStream(path + name + ".tar.gz");

        final GzipCompressorOutputStream outGzipStream = (GzipCompressorOutputStream) new CompressorStreamFactory()
                .createCompressorOutputStream(CompressorStreamFactory.GZIP, outGzipFileStream);

        IOUtils.copy(inTarFileStream, outGzipStream);
        inTarFileStream.close();

        outGzipStream.close();
        outGzipFileStream.close();

    }
}