public static int runProcess(String[] pstr, int timeout) throws TimeoutException, InterruptedException, IOException { String cmdStr = ""; for (String st : pstr) { cmdStr = cmdStr + " " + st; } SDFSLogger.getLog().debug("Executing [" + cmdStr + "]"); Process p = null; try { p = Runtime.getRuntime().exec(pstr, null, new File(Main.volume.getPath())); ReadStream s1 = new ReadStream("stdin", p.getInputStream()); ReadStream s2 = new ReadStream("stderr", p.getErrorStream()); s1.start(); s2.start(); } catch (Throwable e) { SDFSLogger.getLog().error("unable to execute " + cmdStr, e); throw new IOException(e); } long now = System.currentTimeMillis(); long finish = now + timeout; while (isAlive(p) && (System.currentTimeMillis() < finish)) { Thread.sleep(10); } if (isAlive(p)) { throw new TimeoutException("Process [" + cmdStr + "] timeout out after [" + timeout + "] ms"); } return p.exitValue(); }
/** Read data from the connection. If the request hasn't yet been sent to the server, send it. */ public int readInt(byte[] buf, int offset, int length) throws IOException { if (!_didGet) { getConnInput(); } if (_isRequestDone) { return -1; } try { int len = length; if (_isChunked) { if (_chunkLength == 0) { int ch; for (ch = _rs.read(); ch >= 0 && (ch == '\r' || ch == '\n' || ch == ' '); ch = _rs.read()) {} for (; ch >= 0 && ch != '\n'; ch = _rs.read()) { if (ch >= '0' && ch <= '9') _chunkLength = 16 * _chunkLength + ch - '0'; else if (ch >= 'a' && ch <= 'f') _chunkLength = 16 * _chunkLength + ch - 'a' + 10; else if (ch >= 'A' && ch <= 'F') _chunkLength = 16 * _chunkLength + ch - 'A' + 10; } if (_chunkLength == 0) { _isRequestDone = true; return -1; } } else if (_chunkLength < 0) return -1; if (_chunkLength < len) len = _chunkLength; } else if (_contentLength < 0) { } else if (_contentLength == 0) { _isRequestDone = true; return -1; } else if (_contentLength < len) len = _contentLength; len = _rs.read(buf, offset, len); if (len < 0) { } else if (_isChunked) _chunkLength -= len; else if (_contentLength > 0) _contentLength -= len; return len; } catch (IOException e) { _isKeepalive = false; throw e; } catch (RuntimeException e) { _isKeepalive = false; throw e; } }
/** Creates a ReadStream from a Reader */ public static ReadStream openRead(Reader reader) { if (reader instanceof ReadStream.StreamReader) return ((ReadStream.StreamReader) reader).getStream(); ReaderWriterStream s = new ReaderWriterStream(reader, null); ReadStream is = new ReadStream(s); try { is.setEncoding("utf-8"); } catch (Exception e) { } return is; }
private void initDriverList() { try { Thread thread = Thread.currentThread(); ClassLoader loader = thread.getContextClassLoader(); Enumeration iter = loader.getResources("META-INF/services/java.sql.Driver"); while (iter.hasMoreElements()) { URL url = (URL) iter.nextElement(); ReadStream is = null; try { is = Vfs.lookup(url.toString()).openRead(); String filename; while ((filename = is.readLine()) != null) { int p = filename.indexOf('#'); if (p >= 0) filename = filename.substring(0, p); filename = filename.trim(); if (filename.length() == 0) continue; try { Class cl = Class.forName(filename, false, loader); Driver driver = null; if (Driver.class.isAssignableFrom(cl)) driver = (Driver) cl.newInstance(); if (driver != null) { log.fine(L.l("DatabaseManager adding driver '{0}'", driver.getClass().getName())); _driverList.add(driver); } } catch (Exception e) { log.log(Level.FINE, e.toString(), e); } } } catch (Exception e) { log.log(Level.FINE, e.toString(), e); } finally { if (is != null) is.close(); } } } catch (Exception e) { log.log(Level.FINE, e.toString(), e); } }
/** Returns the bytes still available. */ public int getAvailable() throws IOException { if (!_didGet) getConnInput(); // php/164q if (_isRequestDone) return 0; else if (_contentLength > 0) return _contentLength; else return _rs.getAvailable(); }
public static int runProcess(String pstr) throws TimeoutException, InterruptedException, IOException { SDFSLogger.getLog().debug("Executing [" + pstr + "]"); Process p = null; try { p = Runtime.getRuntime().exec(pstr, null, new File(Main.volume.getPath())); ReadStream s1 = new ReadStream("stdin", p.getInputStream()); ReadStream s2 = new ReadStream("stderr", p.getErrorStream()); s1.start(); s2.start(); } catch (Throwable e) { SDFSLogger.getLog().error("unable to execute " + pstr, e); throw new IOException(e); } p.waitFor(); return p.exitValue(); }
public boolean open() throws IOException { if (!_isValid) return false; _is = _path.openRead(); _is.skip(_offset); _index = 0; return true; }
private Map<String, RepositoryTagEntry> readTagMap(AbstractRepository repository, String sha1) throws IOException { TreeMap<String, RepositoryTagEntry> map = new TreeMap<String, RepositoryTagEntry>(); InputStream is = repository.openBlob(sha1); try { ReadStream in = Vfs.openRead(is); String tag; while ((tag = in.readLine()) != null) { String entrySha1 = in.readLine(); RepositoryTagEntry entry = new RepositoryTagEntry(repository, entrySha1); map.put(tag, entry); } } finally { is.close(); } return Collections.unmodifiableMap(map); }
/** Close the connection. */ public void close() throws IOException { if (_isKeepalive) { // If recycling, read any unread data if (!_didGet) getConnInput(); if (!_isRequestDone) { if (_tempBuffer == null) _tempBuffer = new byte[256]; try { while (read(_tempBuffer, 0, _tempBuffer.length) > 0) {} } catch (IOException e) { _isKeepalive = false; } } } if (_isKeepalive) { HttpStream oldSaved; long now; now = CurrentTime.getCurrentTime(); synchronized (LOCK) { oldSaved = _savedStream; _savedStream = this; _saveTime = now; } if (oldSaved != null && oldSaved != this) { oldSaved._isKeepalive = false; oldSaved.close(); } return; } try { try { if (_ws != null) _ws.close(); } catch (Throwable e) { } _ws = null; try { if (_rs != null) _rs.close(); } catch (Throwable e) { } _rs = null; try { if (_os != null) _os.close(); } catch (Throwable e) { } _os = null; try { if (_is != null) _is.close(); } catch (Throwable e) { } _is = null; } finally { if (_s != null) _s.close(); _s = null; } }
/** Parse the headers returned from the server. */ private void parseHeaders() throws IOException { CharBuffer line = new CharBuffer(); // Skip blank lines int count = 0; do { line.clear(); if (!_rs.readln(line)) { _isKeepalive = false; return; } } while (line.length() == 0 && ++count < 10); if (line.length() == 0) { _isKeepalive = false; return; } if (line.startsWith("HTTP/1.1 100")) { count = 100; do { line.clear(); if (!_rs.readln(line)) { _isKeepalive = false; return; } } while (line.length() != 0 && count-- > 0); count = 100; do { line.clear(); if (!_rs.readln(line)) { _isKeepalive = false; return; } } while (line.length() == 0 && count-- > 0); } if (line.length() == 0) { _isKeepalive = false; return; } int i = 0; for (i = 0; i < line.length() && line.charAt(i) != ' '; i++) {} for (; i < line.length() && line.charAt(i) == ' '; i++) {} int status = 0; for (; i < line.length(); i++) { char ch = line.charAt(i); if (ch >= '0' && ch <= '9') status = 10 * status + ch - '0'; else break; } if (status != 200) _isKeepalive = false; else if (!line.startsWith("HTTP/1.1 ")) _isKeepalive = false; _attributes.put("status", String.valueOf(status)); _attributes.put("status-message", line.toString()); CharBuffer key = new CharBuffer(); while (true) { line.clear(); if (!_rs.readln(line) || line.length() == 0) break; int lineLength = line.length(); for (i = 0; i < lineLength && Character.isWhitespace(line.charAt(i)); i++) {} key.clear(); for (; i < lineLength && !Character.isWhitespace(line.charAt(i)) && line.charAt(i) != ':'; i++) { key.append((char) line.charAt(i)); } for (; i < lineLength && Character.isWhitespace(line.charAt(i)); i++) {} if (key.length() == 0 || lineLength <= i || line.charAt(i) != ':') continue; for (i++; i < lineLength && Character.isWhitespace(line.charAt(i)); i++) {} key.toLowerCase(); String value = line.substring(i); if (log.isLoggable(Level.FINE)) log.fine(key + ": " + value); if (key.matchesIgnoreCase("content-length")) { _contentLength = Integer.parseInt(value.trim()); } else if (key.matchesIgnoreCase("connection") && value.equalsIgnoreCase("close")) { _isKeepalive = false; } else if (key.matchesIgnoreCase("transfer-encoding") && value.equalsIgnoreCase("chunked")) { _isChunked = true; _chunkLength = 0; } String keyString = key.toLowerCase().toString(); String oldValue = (String) _attributes.put(keyString, value); if (oldValue != null) { value = oldValue + '\n' + value; } _attributes.put(keyString, value); } }
/** * Creates a new Jar. * * @param path canonical path */ public ZipScanner(Path path) { try { _path = path; int length = (int) path.getLength(); ReadStream is = path.openRead(); try { // PACK200 is a standard comment, so try skipping it first is.skip(length - 22 - 7); if (is.read() != 0x50) { is.skip(6); if (is.read() != 0x50) return; } if (is.read() == 0x4b && is.read() == 0x05 && is.read() == 0x06) { _isValid = true; } if (_isValid) { is.skip(6); _entries = is.read() + (is.read() << 8); is.skip(4); _offset = readInt(is); } } finally { is.close(); } } catch (Exception e) { log().log(Level.FINER, e.toString(), e); } }
public boolean next() throws IOException { if (_entries <= _index) return false; _index++; ReadStream is = _is; if (is.readInt() != 0x504b0102) { throw new IOException("illegal zip format"); } is.skip(2 + 2 + 2 + 2 + 2 + 2 + 4); int compressedSize = readInt(is); int uncompressedSize = readInt(is); int nameLen = is.read() + (is.read() << 8); int extraLen = is.read() + (is.read() << 8); int commentLen = is.read() + (is.read() << 8); is.skip(2 + 2 + 4); _localFileOffset = readInt(is); _nameLen = nameLen; if (_cbuf.length < nameLen) _cbuf = new char[nameLen]; char[] cbuf = _cbuf; int k = is.readUTF8ByByteLength(cbuf, 0, nameLen); for (int i = k - 1; i >= 0; i--) { char ch = cbuf[i]; // win32 canonicalize if (ch == '\\') cbuf[i] = '/'; } _name = null; // new String(cbuf, 0, k); if (extraLen + commentLen > 0) is.skip(extraLen + commentLen); return true; }