public static ByteArrayOutputStream readFully(InputStream stream) throws IOException { return readFully(stream, LoggerFactory.DEFAULT); }
/** Called when all I/O streams should be closed. Subclasses can override but must call super. */ protected void closeAllStreams() { IOUtils.closeQuietly(in, out); }
public static String readFromFile(File f) throws IOException { FileInputStream fileInputStream = new FileInputStream(f); try { ByteArrayOutputStream byteArrayOutputStream = IOUtils.readFully(fileInputStream); return byteArrayOutputStream.toString(IOUtils.UTF8.displayName()); } finally { IOUtils.closeQuietly(fileInputStream); } } }
void check(String what) throws IOException { int code = scp.getInputStream().read(); switch (code) { case -1: String stderr = IOUtils.readFully(scp.getErrorStream(), loggerFactory).toString(); if (!stderr.isEmpty()) stderr = ". Additional info: `" + stderr + "`"; throw new SCPException("EOF while expecting response to protocol message" + stderr); case 0: // OK log.debug(what); return; case 1: // Warning? not case 2: final String remoteMessage = readMessage(); throw new SCPRemoteException("Remote SCP command had error: " + remoteMessage, remoteMessage); default: throw new SCPException("Received unknown response code"); } }
public static void closeQuietly(Closeable... closeables) { closeQuietly(LoggerFactory.DEFAULT, closeables); }
private void finishOff() { reader.interrupt(); IOUtils.closeQuietly(connInfo.in); IOUtils.closeQuietly(connInfo.out); }
public static ByteArrayOutputStream readFully(InputStream stream) throws IOException { return readFully(stream, LoggerFactory.DEFAULT); }
private static String readHeader(Reader privateKey) throws IOException { BufferedReader br = new BufferedReader(privateKey); try { String header; while ((header = br.readLine()) != null) { header = header.trim(); if (!header.isEmpty()) { break; } } return header; } finally { IOUtils.closeQuietly(br); } }
/** * Checks if the path is a correct path to vcgencmd. * * @param path the path to check * @param client authenticated and open client * @return true, if correct, false if not * @throws IOException if something ssh related goes wrong */ private boolean isValidVcgencmdPath(String path, SSHClient client) throws IOException { final Session session = client.startSession(); session.allocateDefaultPTY(); LOGGER.debug("Checking vcgencmd location: {}", path); final Command cmd = session.exec(path); cmd.join(30, TimeUnit.SECONDS); session.close(); final Integer exitStatus = cmd.getExitStatus(); final String output = IOUtils.readFully(cmd.getInputStream()).toString().toLowerCase(); LOGGER.debug("Path check output: {}", output); return exitStatus != null && exitStatus.equals(0) && !output.contains("not found") && !output.contains("no such file or directory"); }
@Override protected void closeAllStreams() { IOUtils.closeQuietly(err); super.closeAllStreams(); }
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, output.getExitStatus()); } finally { clear(); } }
/** * Append a single entry */ public void write(KnownHostEntry entry) throws IOException { final BufferedWriter writer = new BufferedWriter(new FileWriter(khFile, true)); try { writer.write(entry.getLine()); writer.newLine(); writer.flush(); } finally { IOUtils.closeQuietly(writer); } }
void check(String what) throws IOException { int code = scp.getInputStream().read(); switch (code) { case -1: String stderr = IOUtils.readFully(scp.getErrorStream()).toString(); if (!stderr.isEmpty()) stderr = ". Additional info: `" + stderr + "`"; throw new SCPException("EOF while expecting response to protocol message" + stderr); case 0: // OK log.debug(what); return; case 1: // Warning? not case 2: throw new SCPException("Remote SCP command had error: " + readMessage()); default: throw new SCPException("Received unknown response code"); } }
public static void writeToFile(File f, String content) throws IOException { FileWriter w = new FileWriter(f); try { w.write(content); } finally { IOUtils.closeQuietly(w); } }
@Override public String run() throws RaspiQueryException { LOGGER.debug("Querying system time via 'date --rfc-2822'."); try { Session session = getSSHClient().startSession(); String cmdString = "date --rfc-2822"; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); String output = IOUtils.readFully(cmd.getInputStream()) .toString(); final String result = output.trim(); LOGGER.debug("System time: {}", result); return result; } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } } }
public void run() { try { for (Event<IOException> ev = x; !ev.tryAwait(frequency, unit); ev = (ev == x) ? y : x) { } } catch (IOException ignored) { } finally { IOUtils.closeQuietly(channel, asCloseable(socket)); } } }).start();
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); int errorStatus = output.getExitStatus(); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, errorStatus); } finally { clear(); } }
@Override public void run() { try { listener.gotConnect(chan); } catch (IOException logged) { log.warn("In callback to {}: {}", listener, logged); if (chan.isOpen()) IOUtils.closeQuietly(chan); else try { chan.reject(OpenFailException.Reason.CONNECT_FAILED, ""); } catch (TransportException cantdonthn) { log.warn("Error rejecting {}: {}", chan, cantdonthn); } } }
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, output.getExitStatus()); } finally { clear(); } }