use of net.morimekta.util.Binary in project providence by morimekta.
the class FastBinarySerializer method writeContainerEntry.
@SuppressWarnings("unchecked")
private int writeContainerEntry(LittleEndianBinaryWriter out, int typeid, PDescriptor descriptor, Object value) throws IOException {
switch(typeid) {
case VARINT:
{
if (value instanceof Boolean) {
return out.writeVarint(((Boolean) value ? 1 : 0));
} else if (value instanceof Number) {
return out.writeZigzag(((Number) value).longValue());
} else if (value instanceof PEnumValue) {
return out.writeZigzag(((PEnumValue) value).asInteger());
} else {
throw new SerializerException("");
}
}
case FIXED_64:
{
return out.writeDouble((Double) value);
}
case BINARY:
{
if (value instanceof CharSequence) {
byte[] bytes = ((String) value).getBytes(StandardCharsets.UTF_8);
int len = out.writeVarint(bytes.length);
out.write(bytes);
return len + bytes.length;
} else if (value instanceof Binary) {
Binary bytes = (Binary) value;
int len = out.writeVarint(bytes.length());
bytes.write(out);
return len + bytes.length();
} else {
throw new SerializerException("");
}
}
case MESSAGE:
{
return writeMessage(out, (PMessage) value);
}
case COLLECTION:
{
if (value instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) value;
PMap<?, ?> desc = (PMap<?, ?>) descriptor;
int ktype = itemType(desc.keyDescriptor());
int vtype = itemType(desc.itemDescriptor());
int len = out.writeVarint(map.size() * 2);
len += out.writeVarint(ktype << 3 | vtype);
for (Map.Entry<Object, Object> entry : map.entrySet()) {
len += writeContainerEntry(out, ktype, desc.keyDescriptor(), entry.getKey());
len += writeContainerEntry(out, vtype, desc.itemDescriptor(), entry.getValue());
}
return len;
} else if (value instanceof Collection) {
Collection<Object> coll = (Collection<Object>) value;
PContainer<?> desc = (PContainer<?>) descriptor;
int vtype = itemType(desc.itemDescriptor());
int len = out.writeVarint(coll.size());
len += out.writeVarint(vtype);
for (Object item : coll) {
len += writeContainerEntry(out, vtype, desc.itemDescriptor(), item);
}
return len;
} else {
throw new SerializerException("");
}
}
default:
throw new SerializerException("");
}
}
use of net.morimekta.util.Binary in project providence by morimekta.
the class PrettySerializer method appendPrimitive.
private void appendPrimitive(IndentedPrintWriter writer, Object o) {
if (o instanceof PEnumValue) {
writer.print(((PEnumValue) o).asString());
} else if (o instanceof CharSequence) {
writer.print(Token.kLiteralDoubleQuote);
writer.print(Strings.escape((CharSequence) o));
writer.print(Token.kLiteralDoubleQuote);
} else if (o instanceof Binary) {
Binary b = (Binary) o;
writer.append(Token.B64).append(Token.kParamsStart).append(b.toBase64()).append(Token.kParamsEnd);
} else if (o instanceof Boolean) {
writer.print(((Boolean) o).booleanValue());
} else if (o instanceof Byte || o instanceof Short || o instanceof Integer || o instanceof Long) {
writer.print(o.toString());
} else if (o instanceof Double) {
Double d = (Double) o;
if (d.equals(((double) d.longValue()))) {
// actually an integer or long value.
writer.print(d.longValue());
} else {
writer.print(d.doubleValue());
}
} else {
throw new IllegalArgumentException("Unknown primitive type class " + o.getClass().getSimpleName());
}
}
use of net.morimekta.util.Binary in project providence by morimekta.
the class SerializerTest method testOutput.
/**
* Tests the current output. This tests the byte-stream generated by serialising with the current generated code /
*/
public void testOutput(Serializer serializer, String resource) throws IOException {
Binary expected;
List<Containers> source;
try (InputStream r = SerializerTest.class.getResourceAsStream(resource)) {
if (r == null) {
File file = new File("src/test/resources" + resource);
File testing = new File("providence-core");
if (testing.isDirectory()) {
file = new File(testing, file.toString());
}
containers.stream().limit(10).collect(MessageCollectors.toFile(file, serializer));
fail("No such resource to compare: " + resource);
return;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = r.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, len);
}
expected = Binary.wrap(out.toByteArray());
source = MessageStreams.stream(new ByteArrayInputStream(out.toByteArray()), serializer, Containers.kDescriptor).collect(Collectors.toList());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
source.stream().collect(MessageCollectors.toStream(out, serializer));
Binary actual = Binary.wrap(out.toByteArray());
if (serializer.binaryProtocol()) {
assertEquals("Hex data comparison.", expected.toHexString(), actual.toHexString());
} else {
assertThat(new String(expected.get()), is(equalToLines(new String(actual.get()))));
}
}
use of net.morimekta.util.Binary in project providence by morimekta.
the class MessageFieldArgument method apply.
@Override
@SuppressWarnings("unchecked")
public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
if (message.has(field)) {
switch(field.getType()) {
case BOOL:
{
boolean value = message.get(field);
if (type == Types.BOOLEAN || type == Types.BIT) {
statement.setBoolean(position, value);
} else {
statement.setInt(position, value ? 1 : 0);
}
break;
}
case BYTE:
{
statement.setByte(position, message.get(field));
break;
}
case I16:
{
statement.setShort(position, message.get(field));
break;
}
case I32:
{
if (type == Types.TIMESTAMP) {
Timestamp timestamp = new Timestamp(1000L * (int) message.get(field));
statement.setTimestamp(position, timestamp);
} else {
statement.setInt(position, message.get(field));
}
break;
}
case I64:
{
if (type == Types.TIMESTAMP) {
Timestamp timestamp = new Timestamp(message.get(field));
statement.setTimestamp(position, timestamp);
} else {
statement.setLong(position, message.get(field));
}
break;
}
case DOUBLE:
{
statement.setDouble(position, message.get(field));
break;
}
case STRING:
{
statement.setString(position, message.get(field));
break;
}
case BINARY:
{
Binary binary = message.get(field);
switch(type) {
case Types.BINARY:
case Types.VARBINARY:
{
statement.setBytes(position, binary.get());
break;
}
case Types.BLOB:
{
statement.setBlob(position, binary.getInputStream());
break;
}
case Types.CHAR:
case Types.VARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
{
statement.setString(position, binary.toBase64());
break;
}
default:
throw new SQLDataException("Unknown binary field type: " + type + " for " + field);
}
break;
}
case ENUM:
{
PEnumValue value = message.get(field);
statement.setInt(position, value.asInteger());
break;
}
case MESSAGE:
{
PMessage value = message.get(field);
switch(type) {
case Types.BINARY:
case Types.VARBINARY:
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BINARY.serialize(out, value);
statement.setBytes(position, out.toByteArray());
} catch (IOException e) {
throw new SQLDataException(e.getMessage(), e);
}
break;
}
case Types.BLOB:
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BINARY.serialize(out, value);
statement.setBlob(position, new ByteArrayInputStream(out.toByteArray()));
} catch (IOException e) {
throw new SQLDataException(e.getMessage(), e);
}
break;
}
case Types.CHAR:
case Types.VARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
{
StringWriter writer = new StringWriter();
try {
JSON.serialize(new PrintWriter(writer), value);
statement.setString(position, writer.getBuffer().toString());
} catch (IOException e) {
throw new SQLDataException(e.getMessage(), e);
}
break;
}
case Types.CLOB:
{
StringWriter writer = new StringWriter();
try {
JSON.serialize(new PrintWriter(writer), value);
statement.setClob(position, new StringReader(writer.getBuffer().toString()));
} catch (IOException e) {
throw new SQLDataException(e.getMessage(), e);
}
break;
}
default:
throw new SQLDataException("Unknown message field type: " + type + " for " + field);
}
break;
}
default:
throw new SQLDataException("Unhandled field type in SQL: " + field);
}
} else {
statement.setNull(position, type);
}
}
use of net.morimekta.util.Binary in project providence by morimekta.
the class LogFormatter method appendPrimitive.
private void appendPrimitive(IndentedPrintWriter writer, Object o) {
if (o instanceof PEnumValue) {
writer.print(((PEnumValue) o).asString());
} else if (o instanceof CharSequence) {
writer.print(Token.kLiteralDoubleQuote);
writer.print(Strings.escape((CharSequence) o));
writer.print(Token.kLiteralDoubleQuote);
} else if (o instanceof Binary) {
Binary b = (Binary) o;
writer.append(Token.B64).append(Token.kParamsStart).append(b.toBase64()).append(Token.kParamsEnd);
} else if (o instanceof Boolean) {
writer.print(((Boolean) o).booleanValue());
} else if (o instanceof Byte || o instanceof Short || o instanceof Integer || o instanceof Long) {
writer.print(o.toString());
} else if (o instanceof Double) {
Double d = (Double) o;
if (d.equals(((double) d.longValue()))) {
// actually an integer or long value.
writer.print(d.longValue());
} else {
writer.print(d.doubleValue());
}
} else {
throw new IllegalArgumentException("Unknown primitive type class " + o.getClass().getSimpleName());
}
}
Aggregations