Search in sources :

Example 66 with Out

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

the class CreateScriptFile method openScriptWriter.

/**
 * Open a script writer.
 *
 * @param fileName the file name (the file will be overwritten)
 * @param compressionAlgorithm the compression algorithm (uppercase)
 * @param cipher the encryption algorithm or null
 * @param password the encryption password
 * @param charset the character set (for example UTF-8)
 * @return the print writer
 */
public static PrintWriter openScriptWriter(String fileName, String compressionAlgorithm, String cipher, String password, String charset) throws IOException {
    try {
        OutputStream out;
        if (cipher != null) {
            byte[] key = SHA256.getKeyPasswordHash("script", password.toCharArray());
            FileUtils.delete(fileName);
            FileStore store = FileStore.open(null, fileName, "rw", cipher, key);
            store.init();
            out = new FileStoreOutputStream(store, null, compressionAlgorithm);
            out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
        } else {
            out = FileUtils.newOutputStream(fileName, false);
            out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE);
            out = CompressTool.wrapOutputStream(out, compressionAlgorithm, "script.sql");
        }
        return new PrintWriter(new OutputStreamWriter(out, charset));
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : FileStore(org.h2.store.FileStore) OutputStream(java.io.OutputStream) FileStoreOutputStream(org.h2.store.FileStoreOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) FileStoreOutputStream(org.h2.store.FileStoreOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 67 with Out

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

the class TestTraceSystem method testTraceDebug.

private void testTraceDebug() {
    TraceSystem ts = new TraceSystem(null);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ts.setSysOut(new PrintStream(out));
    ts.setLevelSystemOut(TraceSystem.DEBUG);
    ts.getTrace("test").debug(new Exception("error"), "test");
    ts.close();
    String outString = new String(out.toByteArray());
    assertContains(outString, "error");
    assertContains(outString, "Exception");
    assertContains(outString, "test");
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TraceSystem(org.h2.message.TraceSystem)

Example 68 with Out

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

the class PropertiesToUTF8 method convert.

private static void convert(String source) throws Exception {
    for (File f : new File(source).listFiles()) {
        if (!f.getName().endsWith(".properties")) {
            continue;
        }
        FileInputStream in = new FileInputStream(f);
        InputStreamReader r = new InputStreamReader(in, StandardCharsets.UTF_8);
        String s = IOUtils.readStringAndClose(r, -1);
        in.close();
        String name = f.getName();
        String utf8, html;
        if (name.startsWith("utf8")) {
            utf8 = HtmlConverter.convertHtmlToString(s);
            html = HtmlConverter.convertStringToHtml(utf8);
            RandomAccessFile out = new RandomAccessFile("_" + name.substring(4), "rw");
            out.write(html.getBytes());
            out.setLength(out.getFilePointer());
            out.close();
        } else {
            new CheckTextFiles().checkOrFixFile(f, false, false);
            html = s;
            utf8 = HtmlConverter.convertHtmlToString(html);
            // s = unescapeHtml(s);
            utf8 = StringUtils.javaDecode(utf8);
            FileOutputStream out = new FileOutputStream("_utf8" + f.getName());
            OutputStreamWriter w = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            w.write(utf8);
            w.close();
            out.close();
        }
        String java = StringUtils.javaEncode(utf8);
        java = StringUtils.replaceAll(java, "\\r", "\r");
        java = StringUtils.replaceAll(java, "\\n", "\n");
        RandomAccessFile out = new RandomAccessFile("_java." + name, "rw");
        out.write(java.getBytes());
        out.setLength(out.getFilePointer());
        out.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RandomAccessFile(java.io.RandomAccessFile) CheckTextFiles(org.h2.build.code.CheckTextFiles) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 69 with Out

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

the class PropertiesToUTF8 method propertiesToTextUTF8.

/**
 * Convert a properties file to a UTF-8 text file.
 *
 * @param source the name of the properties file
 * @param target the target file name
 */
static void propertiesToTextUTF8(String source, String target) throws Exception {
    if (!new File(source).exists()) {
        return;
    }
    Properties prop = SortedProperties.loadProperties(source);
    FileOutputStream out = new FileOutputStream(target);
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
    // keys is sorted
    for (Enumeration<Object> en = prop.keys(); en.hasMoreElements(); ) {
        String key = (String) en.nextElement();
        String value = prop.getProperty(key, null);
        writer.print("@" + key + "\n");
        writer.print(value + "\n\n");
    }
    writer.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 70 with Out

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

the class UploadBuild method main.

/**
 * This method is called when executing this application from the command
 * line.
 *
 * @param args the command line parameters
 */
public static void main(String... args) throws Exception {
    System.setProperty("h2.socketConnectTimeout", "30000");
    String password = System.getProperty("h2.ftpPassword");
    if (password == null) {
        return;
    }
    FtpClient ftp = FtpClient.open("h2database.com");
    ftp.login("h2database", password);
    ftp.changeWorkingDirectory("/httpdocs");
    boolean coverage = new File("coverage/index.html").exists();
    boolean coverageFailed;
    if (coverage) {
        byte[] data = IOUtils.readBytesAndClose(new FileInputStream("coverage/index.html"), -1);
        String index = new String(data, StandardCharsets.ISO_8859_1);
        coverageFailed = index.contains("CLASS=\"h\"");
        while (true) {
            int idx = index.indexOf("<A HREF=\"");
            if (idx < 0) {
                break;
            }
            int end = index.indexOf('>', idx) + 1;
            index = index.substring(0, idx) + index.substring(end);
            idx = index.indexOf("</A>");
            index = index.substring(0, idx) + index.substring(idx + "</A>".length());
        }
        index = StringUtils.replaceAll(index, "[all", "");
        index = StringUtils.replaceAll(index, "classes]", "");
        FileOutputStream out = new FileOutputStream("coverage/overview.html");
        out.write(index.getBytes(StandardCharsets.ISO_8859_1));
        out.close();
        new File("details").mkdir();
        zip("details/coverage_files.zip", "coverage", true);
        zip("coverage.zip", "details", false);
        FileUtils.delete("coverage.txt");
        FileUtils.delete("details/coverage_files.zip");
        FileUtils.delete("details");
        if (ftp.exists("/httpdocs", "coverage")) {
            ftp.removeDirectoryRecursive("/httpdocs/coverage");
        }
        ftp.makeDirectory("/httpdocs/coverage");
    } else {
        coverageFailed = true;
    }
    String testOutput;
    boolean error;
    if (new File("docs/html/testOutput.html").exists()) {
        testOutput = IOUtils.readStringAndClose(new FileReader("docs/html/testOutput.html"), -1);
        error = testOutput.contains(OutputCatcher.START_ERROR);
    } else if (new File("log.txt").exists()) {
        testOutput = IOUtils.readStringAndClose(new FileReader("log.txt"), -1);
        testOutput = testOutput.replaceAll("\n", "<br />");
        error = true;
    } else {
        testOutput = "No log.txt";
        error = true;
    }
    if (!ftp.exists("/httpdocs", "automated")) {
        ftp.makeDirectory("/httpdocs/automated");
    }
    String buildSql;
    if (ftp.exists("/httpdocs/automated", "history.sql")) {
        buildSql = new String(ftp.retrieve("/httpdocs/automated/history.sql"));
    } else {
        buildSql = "create table item(id identity, title varchar, " + "issued timestamp, desc varchar);\n";
    }
    String ts = new java.sql.Timestamp(System.currentTimeMillis()).toString();
    String now = ts.substring(0, 16);
    int idx = testOutput.indexOf("Statements per second: ");
    if (idx >= 0) {
        int end = testOutput.indexOf("<br />", idx);
        if (end >= 0) {
            String result = testOutput.substring(idx + "Statements per second: ".length(), end);
            now += " " + result + " op/s";
        }
    }
    String sql = "insert into item(title, issued, desc) values('Build " + now + (error ? " FAILED" : "") + (coverageFailed ? " COVERAGE" : "") + "', '" + ts + "', '<a href=\"http://www.h2database.com/" + "html/testOutput.html\">Output</a>" + " - <a href=\"http://www.h2database.com/" + "coverage/overview.html\">Coverage</a>" + " - <a href=\"http://www.h2database.com/" + "automated/h2-latest.jar\">Jar</a>');\n";
    buildSql += sql;
    Connection conn;
    try {
        Class.forName("org.h2.Driver");
        conn = DriverManager.getConnection("jdbc:h2:mem:");
    } catch (Exception e) {
        Class.forName("org.h2.upgrade.v1_1.Driver");
        conn = DriverManager.getConnection("jdbc:h2v1_1:mem:");
    }
    conn.createStatement().execute(buildSql);
    String newsfeed = IOUtils.readStringAndClose(new FileReader("src/tools/org/h2/build/doc/buildNewsfeed.sql"), -1);
    ScriptReader r = new ScriptReader(new StringReader(newsfeed));
    Statement stat = conn.createStatement();
    ResultSet rs = null;
    while (true) {
        String s = r.readStatement();
        if (s == null) {
            break;
        }
        if (stat.execute(s)) {
            rs = stat.getResultSet();
        }
    }
    rs.next();
    String content = rs.getString("content");
    conn.close();
    ftp.store("/httpdocs/automated/history.sql", new ByteArrayInputStream(buildSql.getBytes()));
    ftp.store("/httpdocs/automated/news.xml", new ByteArrayInputStream(content.getBytes()));
    ftp.store("/httpdocs/html/testOutput.html", new ByteArrayInputStream(testOutput.getBytes()));
    String jarFileName = "bin/h2-" + Constants.getVersion() + ".jar";
    if (FileUtils.exists(jarFileName)) {
        ftp.store("/httpdocs/automated/h2-latest.jar", new FileInputStream(jarFileName));
    }
    if (coverage) {
        ftp.store("/httpdocs/coverage/overview.html", new FileInputStream("coverage/overview.html"));
        ftp.store("/httpdocs/coverage/coverage.zip", new FileInputStream("coverage.zip"));
        FileUtils.delete("coverage.zip");
    }
    String mavenRepoDir = System.getProperty("user.home") + "/.m2/repository/";
    boolean mavenSnapshot = new File(mavenRepoDir + "com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar").exists();
    if (mavenSnapshot) {
        if (!ftp.exists("/httpdocs", "m2-repo")) {
            ftp.makeDirectory("/httpdocs/m2-repo");
        }
        if (!ftp.exists("/httpdocs/m2-repo", "com")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com", "h2database")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database", "h2")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database/h2", "1.0-SNAPSHOT")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2/1.0-SNAPSHOT");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database", "h2-mvstore")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2-mvstore");
        }
        if (!ftp.exists("/httpdocs/m2-repo/com/h2database/h2-mvstore", "1.0-SNAPSHOT")) {
            ftp.makeDirectory("/httpdocs/m2-repo/com/h2database/h2-mvstore/1.0-SNAPSHOT");
        }
        ftp.store("/httpdocs/m2-repo/com/h2database/h2" + "/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.pom", new FileInputStream(mavenRepoDir + "com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.pom"));
        ftp.store("/httpdocs/m2-repo/com/h2database/h2" + "/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar", new FileInputStream(mavenRepoDir + "com/h2database/h2/1.0-SNAPSHOT/h2-1.0-SNAPSHOT.jar"));
        ftp.store("/httpdocs/m2-repo/com/h2database/h2-mvstore" + "/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.pom", new FileInputStream(mavenRepoDir + "com/h2database/h2-mvstore/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.pom"));
        ftp.store("/httpdocs/m2-repo/com/h2database/h2-mvstore" + "/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.jar", new FileInputStream(mavenRepoDir + "com/h2database/h2-mvstore/1.0-SNAPSHOT/h2-mvstore-1.0-SNAPSHOT.jar"));
    }
    ftp.close();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) StringReader(java.io.StringReader) ResultSet(java.sql.ResultSet) FileReader(java.io.FileReader) FtpClient(org.h2.dev.ftp.FtpClient) File(java.io.File) ScriptReader(org.h2.util.ScriptReader)

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