/** Called from inside _logLock */ private void flushTempStream() { TempStreamApi ts = _tempStream; _tempStream = null; _tempStreamSize = 0; try { if (ts != null) { if (_os == null) openLog(); try { ReadStream is = ts.openRead(); try { is.writeToStream(_os); } finally { is.close(); } } catch (IOException e) { e.printStackTrace(); } finally { ts.destroy(); } } } finally { _logLock.notifyAll(); } }
/** Returns the gettext metadata. */ private StringValue getMetadata() throws IOException { _in.setPosition(_offsetTranslation); int length = readInt(); _in.setPosition(readInt()); return readPluralForms(length).get(0); }
/** Executes the JSP Page */ public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; _caucho_init(req, res); if (_hasSession) { req.getSession(); res.setHeader("Cache-Control", "private"); } // res.setContentLength(_contentLength); TempCharBuffer buf = TempCharBuffer.allocate(); char[] cBuf = buf.getBuffer(); int len; PrintWriter out = response.getWriter(); ReadStream rs = _cacheEntry.openRead(); rs.setEncoding("UTF-8"); try { while ((len = rs.read(cBuf, 0, cBuf.length)) > 0) { out.write(cBuf, 0, len); } } finally { rs.close(); } TempCharBuffer.free(buf); }
/** Compile a schema. */ public Schema compileSchema(Path path) throws SAXException, IOException { String nativePath = path.getNativePath(); SoftReference<Schema> schemaRef = _schemaMap.get(path); Schema schema = null; if (schemaRef != null && (schema = schemaRef.get()) != null) { // XXX: probably eventually add an isModified return schema; } ReadStream is = path.openRead(); try { InputSource source = new InputSource(is); source.setSystemId(path.getUserPath()); schema = compileSchema(source); if (schema != null) _schemaMap.put(path, new SoftReference<Schema>(schema)); } finally { is.close(); } return schema; }
private void parseSAX(Source source, ContentHandler handler) throws TransformerConfigurationException { try { if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; XMLReader reader = saxSource.getXMLReader(); InputSource inputSource = saxSource.getInputSource(); reader.setContentHandler(handler); reader.parse(inputSource); } else if (source instanceof StreamSource) { XmlParser parser = new Xml(); parser.setContentHandler(handler); ReadStream rs = openPath(source); try { parser.parse(rs); } finally { rs.close(); } } else if (source instanceof DOMSource) { DOMSource domSource = (DOMSource) source; Node node = domSource.getNode(); XmlUtil.toSAX(node, handler); } } catch (Exception e) { throw new TransformerConfigurationException(e); } }
/** * Convenience class to create a compiled stylesheet. * * @param systemId source path for the stylesheet. * @return a compiled stylesheet */ public javax.xml.transform.Templates newTemplates(String systemId) throws TransformerConfigurationException { StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; else if (systemId == null) return generate(new QDocument(), null); Path path = getSearchPath().lookup(systemId); try { ReadStream is = path.openRead(); Document doc; try { doc = parseXSL(is); } finally { is.close(); } return generate(doc, path); } catch (TransformerConfigurationException e) { throw e; } catch (IOException e) { System.out.println("MP: " + ((MergePath) getSearchPath()).getMergePaths()); throw new TransformerConfigurationExceptionWrapper(e); } catch (Exception e) { throw new TransformerConfigurationExceptionWrapper(e); } }
/** Reads in a string until NULL or EOF encountered. */ private StringValue readOriginalString() throws IOException { StringValue sb = _env.createUnicodeBuilder(); for (int ch = _in.read(); ch > 0; ch = _in.read()) { sb.append((char) ch); } return sb; }
public static void writeFile(OutputStream os, Path path) throws IOException { ReadStream stream = path.openRead(); try { stream.writeToStream(os); } finally { stream.close(); } }
/** * Create a compiled stylesheet from an input stream. * * @param source the source stream * @return the compiled stylesheet */ public Templates newTemplates(Source source) throws TransformerConfigurationException { String systemId = source.getSystemId(); try { if (systemId != null) { StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; } if (source instanceof DOMSource) { Node node = ((DOMSource) source).getNode(); return generateFromNode(node, systemId); } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; XMLReader reader = saxSource.getXMLReader(); InputSource inputSource = saxSource.getInputSource(); Document doc = new QDocument(); DOMBuilder builder = new DOMBuilder(); builder.init(doc); reader.setContentHandler(builder); reader.parse(inputSource); return generateFromNode(doc, systemId); } ReadStream rs = openPath(source); try { Path path = rs.getPath(); Document doc = parseXSL(rs); if (systemId != null) { String mangledName = getMangledName(systemId); Path genPath = getWorkPath().lookup(mangledName); genPath.setUserPath(systemId); return generate(doc, genPath); } else return generateFromNode(doc, null); } finally { if (rs != null) rs.close(); } } catch (TransformerConfigurationException e) { throw e; } catch (Exception e) { throw new XslParseException(e); } }
/** Compile a schema. */ public Schema compileSchema(String url) throws SAXException, IOException { Path path = Vfs.lookup(url); ReadStream is = path.openRead(); try { InputSource source = new InputSource(is); source.setSystemId(url); return compileSchema(source); } finally { is.close(); } }
/** * Returns the gettext translations. * * @return translations from file, or null on error */ HashMap<StringValue, ArrayList<StringValue>> readTranslations() throws IOException { int[] originalOffsets = new int[_numberOfStrings]; int[] translatedOffsets = new int[_numberOfStrings]; int[] translatedLengths = new int[_numberOfStrings]; StringValue[] originals = new StringValue[_numberOfStrings]; _in.setPosition(_offsetOriginal); // Read in offsets of the original strings for (int i = 0; i < _numberOfStrings; i++) { // XXX: length of original strings not needed? readInt(); originalOffsets[i] = readInt(); if (originalOffsets[i] <= 0) return null; } _in.setPosition(_offsetTranslation); // Read in lengths and offsets of the translated strings for (int i = 0; i < _numberOfStrings; i++) { translatedLengths[i] = readInt(); translatedOffsets[i] = readInt(); if (translatedLengths[i] < 0 || translatedOffsets[i] <= 0) return null; } _in.setEncoding(_charset); // Read in the original strings for (int i = 0; i < _numberOfStrings; i++) { _in.setPosition(originalOffsets[i]); originals[i] = readOriginalString(); } HashMap<StringValue, ArrayList<StringValue>> map = new HashMap<StringValue, ArrayList<StringValue>>(); // Read translated strings into the HashMap for (int i = 0; i < _numberOfStrings; i++) { _in.setPosition(translatedOffsets[i]); map.put(originals[i], readPluralForms(translatedLengths[i])); } return map; }
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); } }
/** Parses a stylesheet from the source. */ protected Node parseStylesheet(Source source) throws TransformerConfigurationException { if (source instanceof DOMSource) return ((DOMSource) source).getNode(); else if (source instanceof StreamSource) { InputStream is = ((StreamSource) source).getInputStream(); ReadStream rs = null; try { rs = Vfs.openRead(is); return parseXSL(rs); } catch (Exception e) { throw new TransformerConfigurationException(e); } finally { if (rs != null) rs.close(); } } else return null; }
private static String parseName(ReadStream is) throws IOException { int ch; for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) {} if (ch < 0) return null; CharBuffer cb = new CharBuffer(); for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) { cb.append((char) ch); } if (ch < 0) return null; return cb.toString(); }
private byte[] load(String className) throws IOException { Path path = getPostWorkPath().lookup(className.replace('.', '/') + ".class"); int length = (int) path.getLength(); if (length < 0) throw new FileNotFoundException(L.l("Can't find class file '{0}'", path.getNativePath())); byte[] buffer = new byte[length]; ReadStream is = path.openRead(); try { is.readAll(buffer, 0, buffer.length); } finally { is.close(); } return buffer; }
private void handleExternalBody(String url) throws JspException, ServletException, IOException { URL netURL = new URL(url); URLConnection conn = netURL.openConnection(); if (conn instanceof HttpURLConnection) ((HttpURLConnection) conn).setFollowRedirects(true); InputStream is = conn.getInputStream(); try { ReadStream in = Vfs.openRead(is); String encoding = conn.getContentEncoding(); String contentType = conn.getContentType(); if (_charEncoding != null) { encoding = _charEncoding; if (encoding != null && !encoding.equals("")) in.setEncoding(encoding); } else if (encoding != null) in.setEncoding(encoding); else if (contentType != null) { int p = contentType.indexOf("charset="); if (p > 0) { CharBuffer cb = new CharBuffer(); for (int i = p + 8; i < contentType.length(); i++) { int ch = contentType.charAt(i); if (ch == '"' || ch == '\'') { } else if (ch >= 'a' && ch <= 'z') cb.append((char) ch); else if (ch >= 'A' && ch <= 'Z') cb.append((char) ch); else if (ch >= '0' && ch <= '9') cb.append((char) ch); else if (ch == '-' || ch == '_') cb.append((char) ch); else break; } encoding = cb.toString(); in.setEncoding(encoding); } } JspWriter out = pageContext.getOut(); int ch; while ((ch = in.readChar()) >= 0) out.print((char) ch); } finally { is.close(); } }
/** Sets the current location in the file. */ public boolean setPosition(long offset) { if (_is == null) return false; _isEOF = false; try { return _is.setPosition(offset); } catch (IOException e) { throw new QuercusModuleException(e); } }
static ArrayList<Depend> parseDepend(Path dependPath) throws IOException { ReadStream is = dependPath.openRead(); try { ArrayList<Depend> dependList = new ArrayList<Depend>(); String name; while ((name = parseName(is)) != null) { long digest = Long.parseLong(parseName(is)); Depend depend = new Depend(dependPath.lookup(name), digest); dependList.add(depend); } return dependList; } finally { is.close(); } }
private static Node parse( Env env, Value data, int options, boolean dataIsUrl, String namespace, boolean isPrefix) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = null; if (dataIsUrl) { Path path = env.lookup(data.toStringValue()); // PHP throws an Exception instead if (path == null) { log.log(Level.FINE, L.l("Cannot read file/URL '{0}'", data)); env.warning(L.l("Cannot read file/URL '{0}'", data)); return null; } ReadStream is = path.openRead(); try { document = builder.parse(is); } finally { is.close(); } } else { StringReader reader = new java.io.StringReader(data.toString()); document = builder.parse(new InputSource(reader)); } NodeList childList = document.getChildNodes(); // php/1x70 for (int i = 0; i < childList.getLength(); i++) { if (childList.item(i).getNodeType() == Node.ELEMENT_NODE) return childList.item(i); } return childList.item(0); }
public void writeToStream(OutputStream os, int length) throws IOException { try { if (_is != null) { _is.writeToStream(os, length); } } catch (IOException e) { _isTimeout = true; _isEOF = true; log.log(Level.FINER, e.toString(), e); } }
/** * Loads a stylesheet from a named file * * @param systemId the URL of the file */ public StylesheetImpl newStylesheet(String systemId) throws Exception { StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; synchronized (AbstractStylesheetFactory.class) { stylesheet = loadPrecompiledStylesheet(systemId, systemId); if (stylesheet != null) return stylesheet; ReadStream is; if (_stylePath != null) is = _stylePath.lookup(systemId).openRead(); else is = Vfs.lookup(systemId).openRead(); try { return newStylesheet(is); } finally { if (is != null) is.close(); } } }
public boolean getFile(String tagName, String fileName, OutputStream os) throws IOException { StreamSource fileSource = _deployProxy.getFile(tagName, fileName); if (fileSource != null) { ReadStream is = null; GitObjectStream gitIs = new GitObjectStream(fileSource.getInputStream()); try { is = Vfs.openRead(gitIs); is.writeToStream(os); } finally { gitIs.close(); IoUtil.close(is); } return true; } else { return false; } }
public static void writeStream(OutputStream os, Call call, int length) throws Throwable { if (length < 1) return; char[] buf = new char[256]; int len; Object obj = call.getArgObject(0, length); if (obj instanceof ReadStream) { ReadStream is = (ReadStream) obj; is.writeToStream(os); } else if (obj instanceof ReadWritePair) { ((ReadWritePair) obj).getReadStream().writeToStream(os); } else if (obj instanceof InputStream) { if (os instanceof WriteStream) { ((WriteStream) os).writeStream((InputStream) obj); } else { int ch; InputStream is = (InputStream) obj; while ((ch = is.read()) >= 0) os.write(ch); } } else throw new IllegalArgumentException("expected stream at " + obj.getClass().getName()); }
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); }
private int readInt() throws IOException { int len = _in.read(_tmpBuf); if (len != 4) return -1; if (_isLittleEndian) { return (_tmpBuf[0] & 0xff) | (_tmpBuf[1] & 0xff) << 8 | (_tmpBuf[2] & 0xff) << 16 | _tmpBuf[3] << 24; } else { return _tmpBuf[0] << 24 | (_tmpBuf[1] & 0xff) << 16 | (_tmpBuf[2] & 0xff) << 8 | (_tmpBuf[3] & 0xff); } }
/** Reads in translated plurals forms that are separated by NULL. */ private ArrayList<StringValue> readPluralForms(int length) throws IOException { ArrayList<StringValue> list = new ArrayList<StringValue>(); StringValue sb = new UnicodeBuilderValue(); for (; length > 0; length--) { int ch = _in.readChar(); if (ch > 0) sb.append((char) ch); else if (ch == 0) { list.add(sb); sb = new UnicodeBuilderValue(); } else break; } list.add(sb); return list; }
/** Reads a character from a file, returning -1 on EOF. */ public int read() throws IOException { try { if (_is != null) { int c = _is.read(); if (c < 0) _isEOF = true; return c; } else return -1; } catch (IOException e) { _isTimeout = true; _isEOF = true; log.log(Level.FINER, e.toString(), e); return -1; } }
/** Reads a buffer from a file, returning -1 on EOF. */ public int read(char[] buffer, int offset, int length) throws IOException { try { if (_is != null) { int c = _is.read(buffer, offset, length); if (c == -1) _isEOF = true; else _isEOF = false; return c; } else return -1; } catch (IOException e) { _isTimeout = true; _isEOF = true; log.log(Level.FINER, e.toString(), e); return -1; } }
/** Opens a path based on a Source. */ ReadStream openPath(Source source) throws TransformerException, IOException { String systemId = source.getSystemId(); Path path; if (systemId != null) path = getSearchPath().lookup(systemId); else path = getSearchPath().lookup("anonymous.xsl"); if (source instanceof StreamSource) { StreamSource stream = (StreamSource) source; InputStream is = stream.getInputStream(); if (is instanceof ReadStream) { ReadStream rs = (ReadStream) is; rs.setPath(path); return rs; } else if (is != null) { ReadStream rs = Vfs.openRead(is); rs.setPath(path); return rs; } Reader reader = stream.getReader(); if (reader != null) { ReadStream rs = Vfs.openRead(reader); rs.setPath(path); return rs; } } if (systemId != null) return getSearchPath().lookup(systemId).openRead(); throw new TransformerException("bad source " + source); }
/** Unread the last byte. */ public void unread() throws IOException { if (_is != null) { _is.unread(); _isEOF = false; } }