Search in sources :

Example 26 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class PrepareTranslation method buildHtml.

private static void buildHtml(String templateDir, String targetDir, String language) throws IOException {
    File[] list = new File(templateDir).listFiles();
    new File(targetDir).mkdirs();
    // load the main 'translation'
    String propName = templateDir + "/_docs_" + MAIN_LANGUAGE + ".properties";
    Properties prop = load(propName, false);
    propName = templateDir + "/_docs_" + language + ".properties";
    if (!(new File(propName)).exists()) {
        throw new IOException("Translation not found: " + propName);
    }
    Properties transProp = load(propName, false);
    for (Object k : transProp.keySet()) {
        String key = (String) k;
        String t = transProp.getProperty(key);
        // overload with translations, but not the ones starting with #
        if (t.startsWith("##")) {
            prop.put(key, t.substring(2));
        } else if (!t.startsWith("#")) {
            prop.put(key, t);
        }
    }
    ArrayList<String> fileNames = new ArrayList<>();
    for (File f : list) {
        String name = f.getName();
        if (!name.endsWith(".jsp")) {
            continue;
        }
        // remove '.jsp'
        name = name.substring(0, name.length() - 4);
        fileNames.add(name);
    }
    for (File f : list) {
        String name = f.getName();
        if (!name.endsWith(".jsp")) {
            continue;
        }
        // remove '.jsp'
        name = name.substring(0, name.length() - 4);
        String template = IOUtils.readStringAndClose(new FileReader(templateDir + "/" + name + ".jsp"), -1);
        HashMap<String, Object> map = new HashMap<>();
        for (Object k : prop.keySet()) {
            map.put(k.toString(), prop.get(k));
        }
        String html = PageParser.parse(template, map);
        html = StringUtils.replaceAll(html, "lang=\"" + MAIN_LANGUAGE + "\"", "lang=\"" + language + "\"");
        for (String n : fileNames) {
            if ("frame".equals(n)) {
                // otherwise we can't switch back to English
                continue;
            }
            html = StringUtils.replaceAll(html, n + ".html\"", n + "_" + language + ".html\"");
        }
        html = StringUtils.replaceAll(html, "_" + MAIN_LANGUAGE + ".html\"", ".html\"");
        String target;
        if (language.equals(MAIN_LANGUAGE)) {
            target = targetDir + "/" + name + ".html";
        } else {
            target = targetDir + "/" + name + "_" + language + ".html";
        }
        OutputStream out = new FileOutputStream(target);
        OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
        writer.write(html);
        writer.close();
    }
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties) FileOutputStream(java.io.FileOutputStream) FileReader(java.io.FileReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 27 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class JdbcBlob method getBytes.

/**
 * Returns some bytes of the object.
 *
 * @param pos the index, the first byte is at position 1
 * @param length the number of bytes
 * @return the bytes, at most length bytes
 */
@Override
public byte[] getBytes(long pos, int length) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("getBytes(" + pos + ", " + length + ");");
        }
        checkClosed();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (InputStream in = value.getInputStream()) {
            IOUtils.skipFully(in, pos - 1);
            IOUtils.copy(in, out, length);
        }
        return out.toByteArray();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 28 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class JdbcBlob method setBinaryStream.

/**
 * Get a writer to update the Blob. This is only supported for new, empty
 * Blob objects that were created with Connection.createBlob(). The Blob is
 * created in a separate thread, and the object is only updated when
 * OutputStream.close() is called. The position must be 1, meaning the whole
 * Blob data is set.
 *
 * @param pos where to start writing (the first byte is at position 1)
 * @return an output stream
 */
@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("setBinaryStream(" + pos + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        }
        if (value.getPrecision() != 0) {
            throw DbException.getInvalidValueException("length", value.getPrecision());
        }
        // local variable avoids generating synthetic accessor method
        final JdbcConnection c = conn;
        final PipedInputStream in = new PipedInputStream();
        final Task task = new Task() {

            @Override
            public void call() {
                value = c.createBlob(in, -1);
            }
        };
        PipedOutputStream out = new PipedOutputStream(in) {

            @Override
            public void close() throws IOException {
                super.close();
                try {
                    task.get();
                } catch (Exception e) {
                    throw DbException.convertToIOException(e);
                }
            }
        };
        task.execute();
        return new BufferedOutputStream(out);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Task(org.h2.util.Task) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 29 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class JdbcClob method setCharacterStream.

/**
 * Get a writer to update the Clob. This is only supported for new, empty
 * Clob objects that were created with Connection.createClob() or
 * createNClob(). The Clob is created in a separate thread, and the object
 * is only updated when Writer.close() is called. The position must be 1,
 * meaning the whole Clob data is set.
 *
 * @param pos where to start writing (the first character is at position 1)
 * @return a writer
 */
@Override
public Writer setCharacterStream(long pos) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCodeCall("setCharacterStream(" + pos + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        }
        if (value.getPrecision() != 0) {
            throw DbException.getInvalidValueException("length", value.getPrecision());
        }
        // required to avoid synthetic method creation
        final JdbcConnection c = conn;
        // PipedReader / PipedWriter are a lot slower
        // than PipedInputStream / PipedOutputStream
        // (Sun/Oracle Java 1.6.0_20)
        final PipedInputStream in = new PipedInputStream();
        final Task task = new Task() {

            @Override
            public void call() {
                value = c.createClob(IOUtils.getReader(in), -1);
            }
        };
        PipedOutputStream out = new PipedOutputStream(in) {

            @Override
            public void close() throws IOException {
                super.close();
                try {
                    task.get();
                } catch (Exception e) {
                    throw DbException.convertToIOException(e);
                }
            }
        };
        task.execute();
        return IOUtils.getBufferedWriter(out);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Task(org.h2.util.Task) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 30 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TestWeb method testServlet.

private void testServlet() throws Exception {
    WebServlet servlet = new WebServlet();
    final HashMap<String, String> configMap = new HashMap<>();
    configMap.put("ifExists", "");
    configMap.put("", "");
    configMap.put("", "");
    configMap.put("", "");
    ServletConfig config = new ServletConfig() {

        @Override
        public String getServletName() {
            return "H2Console";
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return new Vector<>(configMap.keySet()).elements();
        }

        @Override
        public String getInitParameter(String name) {
            return configMap.get(name);
        }

        @Override
        public ServletContext getServletContext() {
            return null;
        }
    };
    servlet.init(config);
    TestHttpServletRequest request = new TestHttpServletRequest();
    request.setPathInfo("/");
    TestHttpServletResponse response = new TestHttpServletResponse();
    TestServletOutputStream out = new TestServletOutputStream();
    response.setServletOutputStream(out);
    servlet.doGet(request, response);
    assertContains(out.toString(), "location.href = 'login.jsp");
    servlet.destroy();
}
Also used : WebServlet(org.h2.server.web.WebServlet) HashMap(java.util.HashMap) ServletConfig(javax.servlet.ServletConfig)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SQLException (java.sql.SQLException)17 DbException (org.h2.message.DbException)17 Random (java.util.Random)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)9 Statement (java.sql.Statement)9 Connection (java.sql.Connection)7 PrintStream (java.io.PrintStream)6 Properties (java.util.Properties)6 Task (org.h2.util.Task)6 BufferedOutputStream (java.io.BufferedOutputStream)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 PipedInputStream (java.io.PipedInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4