use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestStreams method testLZFStreams.
private void testLZFStreams() throws IOException {
Random random = new Random(1);
int max = getSize(100, 1000);
for (int i = 0; i < max; i += 3) {
byte[] buffer = getRandomBytes(random);
ByteArrayOutputStream out = new ByteArrayOutputStream();
LZFOutputStream comp = new LZFOutputStream(out);
if (random.nextInt(10) == 1) {
comp.write(buffer);
} else {
for (int j = 0; j < buffer.length; ) {
int[] sizes = { 0, 1, random.nextInt(100), random.nextInt(100000) };
int size = sizes[random.nextInt(sizes.length)];
size = Math.min(size, buffer.length - j);
if (size == 1) {
comp.write(buffer[j]);
} else {
comp.write(buffer, j, size);
}
j += size;
}
}
comp.close();
byte[] compressed = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(compressed);
LZFInputStream decompress = new LZFInputStream(in);
byte[] test = new byte[buffer.length];
for (int j = 0; j < buffer.length; ) {
int[] sizes = { 0, 1, random.nextInt(100), random.nextInt(100000) };
int size = sizes[random.nextInt(sizes.length)];
if (size == 1) {
int x = decompress.read();
if (x < 0) {
break;
}
test[j++] = (byte) x;
} else {
size = Math.min(size, test.length - j);
int l = decompress.read(test, j, size);
if (l < 0) {
break;
}
j += l;
}
}
decompress.close();
assertEquals(buffer, test);
}
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestCompress method testVariableEnd.
private void testVariableEnd() {
CompressTool utils = CompressTool.getInstance();
StringBuilder b = new StringBuilder();
for (int i = 0; i < 90; i++) {
b.append('0');
}
String prefix = b.toString();
for (int i = 0; i < 100; i++) {
b = new StringBuilder(prefix);
for (int j = 0; j < i; j++) {
b.append((char) ('1' + j));
}
String test = b.toString();
byte[] in = test.getBytes();
assertEquals(in, utils.expand(utils.compress(in, "LZF")));
}
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestDate method testDateTimeUtils.
private void testDateTimeUtils() {
ValueTimestamp ts1 = ValueTimestamp.parse("-999-08-07 13:14:15.16");
ValueTimestamp ts2 = ValueTimestamp.parse("19999-08-07 13:14:15.16");
ValueTime t1 = (ValueTime) ts1.convertTo(Value.TIME);
ValueTime t2 = (ValueTime) ts2.convertTo(Value.TIME);
ValueDate d1 = (ValueDate) ts1.convertTo(Value.DATE);
ValueDate d2 = (ValueDate) ts2.convertTo(Value.DATE);
assertEquals("-999-08-07 13:14:15.16", ts1.getString());
assertEquals("-999-08-07", d1.getString());
assertEquals("13:14:15.16", t1.getString());
assertEquals("19999-08-07 13:14:15.16", ts2.getString());
assertEquals("19999-08-07", d2.getString());
assertEquals("13:14:15.16", t2.getString());
ValueTimestamp ts1a = DateTimeUtils.convertTimestamp(ts1.getTimestamp(), DateTimeUtils.createGregorianCalendar());
ValueTimestamp ts2a = DateTimeUtils.convertTimestamp(ts2.getTimestamp(), DateTimeUtils.createGregorianCalendar());
assertEquals("-999-08-07 13:14:15.16", ts1a.getString());
assertEquals("19999-08-07 13:14:15.16", ts2a.getString());
// test for bug on Java 1.8.0_60 in "Europe/Moscow" timezone.
// Doesn't affect most other timezones
long millis = 1407437460000L;
long result1 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis));
long result2 = DateTimeUtils.nanosFromDate(DateTimeUtils.getTimeUTCWithoutDst(millis));
assertEquals(result1, result2);
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class H2Database method create.
/**
* Create a new in-memory database.
*
* @param factory the cursor factory
* @return a connection to this database
*/
public static H2Database create(H2Database.CursorFactory factory) {
ConnectionInfo ci = new ConnectionInfo("mem:");
Database db = new Database(ci, null);
Session s = db.getSystemSession();
return new H2Database(s, factory);
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class Railroads method map.
private void map(String key, ResultSet rs, boolean railroads) throws Exception {
ArrayList<HashMap<String, String>> list;
list = new ArrayList<>();
while (rs.next()) {
HashMap<String, String> map = new HashMap<>();
ResultSetMetaData meta = rs.getMetaData();
for (int i = 0; i < meta.getColumnCount(); i++) {
String k = StringUtils.toLowerEnglish(meta.getColumnLabel(i + 1));
String value = rs.getString(i + 1);
value = value.trim();
map.put(k, PageParser.escapeHtml(value));
}
String topic = rs.getString("TOPIC");
String syntax = rs.getString("SYNTAX").trim();
if (railroads) {
BnfRailroad r = new BnfRailroad();
String railroad = r.getHtml(bnf, syntax);
map.put("railroad", railroad);
}
BnfSyntax visitor = new BnfSyntax();
String syntaxHtml = visitor.getHtml(bnf, syntax);
map.put("syntax", syntaxHtml);
// remove newlines in the regular text
String text = map.get("text");
if (text != null) {
// text is enclosed in <p> .. </p> so this works.
text = StringUtils.replaceAll(text, "<br /><br />", "</p><p>");
text = StringUtils.replaceAll(text, "<br />", " ");
map.put("text", text);
}
String link = topic.toLowerCase();
link = link.replace(' ', '_');
// link = StringUtils.replaceAll(link, "_", "");
link = link.replace('@', '_');
map.put("link", StringUtils.urlEncode(link));
list.add(map);
}
session.put(key, list);
int div = 3;
int part = (list.size() + div - 1) / div;
for (int i = 0, start = 0; i < div; i++, start += part) {
List<HashMap<String, String>> listThird = list.subList(start, Math.min(start + part, list.size()));
session.put(key + "-" + i, listThird);
}
rs.close();
}
Aggregations