use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class TestCsv method testRead.
private void testRead() throws Exception {
String fileName = getBaseDir() + "/test.csv";
FileUtils.delete(fileName);
OutputStream out = FileUtils.newOutputStream(fileName, false);
byte[] b = ("a,b,c,d\n201,-2,0,18\n, \"abc\"\"\" ," + ",\"\"\n 1 ,2 , 3, 4 \n5, 6, 7, 8").getBytes();
out.write(b, 0, b.length);
out.close();
ResultSet rs = new Csv().read(fileName, null, "UTF8");
ResultSetMetaData meta = rs.getMetaData();
assertEquals(4, meta.getColumnCount());
assertEquals("A", meta.getColumnLabel(1));
assertEquals("B", meta.getColumnLabel(2));
assertEquals("C", meta.getColumnLabel(3));
assertEquals("D", meta.getColumnLabel(4));
assertTrue(rs.next());
assertEquals("201", rs.getString(1));
assertEquals("-2", rs.getString(2));
assertEquals("0", rs.getString(3));
assertEquals("18", rs.getString(4));
assertTrue(rs.next());
assertEquals(null, rs.getString(1));
assertEquals("abc\"", rs.getString(2));
assertEquals(null, rs.getString(3));
assertEquals("", rs.getString(4));
assertTrue(rs.next());
assertEquals("1", rs.getString(1));
assertEquals("2", rs.getString(2));
assertEquals("3", rs.getString(3));
assertEquals("4", rs.getString(4));
assertTrue(rs.next());
assertEquals("5", rs.getString(1));
assertEquals("6", rs.getString(2));
assertEquals("7", rs.getString(3));
assertEquals("8", rs.getString(4));
assertFalse(rs.next());
// a,b,c,d
// 201,-2,0,18
// 201,2,0,18
// 201,2,0,18
// 201,2,0,18
// 201,2,0,18
// 201,2,0,18
FileUtils.delete(fileName);
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project elastic-core-maven by OrdinaryDude.
the class Helper method executeQuery.
public static String executeQuery(String line) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream out = new PrintStream(baos);
out.println(line);
try {
Shell shell = new Shell();
shell.setErr(out);
shell.setOut(out);
shell.runTool(Db.db.getConnection(), "-sql", line);
} catch (SQLException e) {
out.println(e.toString());
}
return new String(baos.toByteArray());
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project elastic-core-maven by OrdinaryDude.
the class DbShellServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
resp.setHeader("Pragma", "no-cache");
resp.setDateHeader("Expires", 0);
if (!API.isAllowed(req.getRemoteHost())) {
resp.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
String body = null;
if (!API.disableAdminPassword) {
if (API.adminPassword.isEmpty()) {
body = errorNoPasswordIsConfigured;
} else {
try {
API.verifyPassword(req);
if ("true".equals(req.getParameter("showShell"))) {
body = form.replace("{adminPassword}", URLEncoder.encode(req.getParameter("adminPassword"), "UTF-8"));
}
} catch (ParameterException exc) {
String desc = (String) ((JSONObject) JSONValue.parse(JSON.toString(exc.getErrorResponse()))).get("errorDescription");
body = String.format(passwordFormTemplate, "<p style=\"color:red\">" + desc + "</p>");
}
}
}
if (body != null) {
try (PrintStream out = new PrintStream(resp.getOutputStream())) {
out.print(header);
out.print(body);
out.print(barter);
}
return;
}
String line = Convert.nullToEmpty(req.getParameter("line"));
try (PrintStream out = new PrintStream(resp.getOutputStream())) {
out.println("\n> " + line);
try {
Shell shell = new Shell();
shell.setErr(out);
shell.setOut(out);
shell.runTool(Db.db.getConnection(), "-sql", line);
} catch (SQLException e) {
out.println(e.toString());
}
}
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class Optimizer method calculateBruteForceSome.
private void calculateBruteForceSome() {
int bruteForce = getMaxBruteForceFilters(filters.length);
TableFilter[] list = new TableFilter[filters.length];
Permutations<TableFilter> p = Permutations.create(filters, list, bruteForce);
for (int x = 0; !canStop(x) && p.next(); x++) {
// find out what filters are not used yet
for (TableFilter f : filters) {
f.setUsed(false);
}
for (int i = 0; i < bruteForce; i++) {
list[i].setUsed(true);
}
// fill the remaining elements with the unused elements (greedy)
for (int i = bruteForce; i < filters.length; i++) {
double costPart = -1.0;
int bestPart = -1;
for (int j = 0; j < filters.length; j++) {
if (!filters[j].isUsed()) {
if (i == filters.length - 1) {
bestPart = j;
break;
}
list[i] = filters[j];
Plan part = new Plan(list, i + 1, condition);
double costNow = part.calculateCost(session);
if (costPart < 0 || costNow < costPart) {
costPart = costNow;
bestPart = j;
}
}
}
filters[bestPart].setUsed(true);
list[i] = filters[bestPart];
}
testPlan(list);
}
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class ScriptBase method openOutput.
/**
* Open the output stream.
*/
void openOutput() {
String file = getFileName();
if (file == null) {
return;
}
if (isEncrypted()) {
initStore();
out = new FileStoreOutputStream(store, this, compressionAlgorithm);
// always use a big buffer, otherwise end-of-block is written a lot
out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
} else {
OutputStream o;
try {
o = FileUtils.newOutputStream(file, false);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
out = new BufferedOutputStream(o, Constants.IO_BUFFER_SIZE);
out = CompressTool.wrapOutputStream(out, compressionAlgorithm, SCRIPT_SQL);
}
}
Aggregations