use of org.h2.dev.util.BitStream.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();
}
}
use of org.h2.dev.util.BitStream.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);
}
}
use of org.h2.dev.util.BitStream.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);
}
}
use of org.h2.dev.util.BitStream.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);
}
}
use of org.h2.dev.util.BitStream.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();
}
Aggregations