use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TTupleProtocolSerializer method serialize.
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException {
CountingOutputStream wrapper = new CountingOutputStream(output);
TTransport transport = new TIOStreamTransport(wrapper);
try {
TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence());
protocol.writeMessageBegin(tm);
writeMessage(call.getMessage(), protocol);
protocol.writeMessageEnd();
transport.flush();
wrapper.flush();
return wrapper.getByteCount();
} catch (TException e) {
throw new SerializerException(e, e.getMessage());
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class Config method run.
public void run(String... args) {
ConfigOptions op = new ConfigOptions(tty);
try {
ArgumentParser cli = op.getArgumentParser("pvdcfg", "Providence Config Tool");
try {
cli.parse(args);
if (op.showHelp()) {
System.out.println(cli.getDescription() + " - " + getVersionString());
System.out.println("Usage: " + cli.getSingleLineUsage());
System.out.println();
cli.printUsage(System.out);
System.out.println();
System.out.println("Available Commands:");
System.out.println();
op.getCommandSet().printUsage(System.out);
return;
} else if (op.showVersion()) {
System.out.println(cli.getDescription() + " - " + getVersionString());
return;
}
cli.validate();
op.execute();
return;
} catch (ArgumentException e) {
System.err.println("Invalid argument: " + e.getMessage());
System.err.println("Usage: " + cli.getSingleLineUsage());
if (op.verbose()) {
e.printStackTrace();
}
} catch (ParseException e) {
System.out.flush();
System.err.println(e.asString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (TokenizerException e) {
System.out.flush();
System.err.println(e.asString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (SerializerException e) {
System.out.flush();
System.err.println("Serialization error: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (IOException | RuntimeException e) {
System.out.flush();
System.err.println("IO Error: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.flush();
System.err.println("Unhandled exception: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
}
exit(1);
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class BinaryFormatUtils method readFieldValue.
/**
* Read a field value from stream.
*
* @param in The stream to consume.
* @param fieldInfo The field info about the content.
* @param fieldType The type to generate content for.
* @param strict If the field should be read strictly.
* @return The field value, or null if no type.
* @throws IOException If unable to read from stream or invalid field type.
*/
public static Object readFieldValue(BigEndianBinaryReader in, FieldInfo fieldInfo, PDescriptor fieldType, boolean strict) throws IOException {
if (fieldType != null && forType(fieldType.getType()) != fieldInfo.type) {
throw new SerializerException("Wrong field type for id=%d: expected %s, got %s", fieldInfo.id, asString(forType(fieldType.getType())), asString(fieldInfo.getType()));
}
switch(fieldInfo.type) {
case BinaryType.VOID:
return Boolean.TRUE;
case BinaryType.BOOL:
return in.expectByte() != 0;
case BinaryType.BYTE:
return in.expectByte();
case BinaryType.I16:
return in.expectShort();
case BinaryType.I32:
int val = in.expectInt();
if (fieldType != null && fieldType instanceof PEnumDescriptor) {
@SuppressWarnings("unchecked") PEnumBuilder builder = ((PEnumDescriptor<?>) fieldType).builder();
builder.setById(val);
return builder.build();
} else {
return val;
}
case BinaryType.I64:
return in.expectLong();
case BinaryType.DOUBLE:
return in.expectDouble();
case BinaryType.STRING:
int len = in.expectUInt32();
byte[] data = in.expectBytes(len);
if (fieldType != null && fieldType.getType() == PType.STRING) {
return new String(data, StandardCharsets.UTF_8);
} else {
return Binary.wrap(data);
}
case BinaryType.STRUCT:
{
if (fieldType == null) {
consumeMessage(in);
return null;
}
return readMessage(in, (PMessageDescriptor<?, ?>) fieldType, strict);
}
case BinaryType.MAP:
{
final byte keyT = in.expectByte();
final byte itemT = in.expectByte();
final int size = in.expectUInt32();
PDescriptor keyType = null;
PDescriptor valueType = null;
PMap.Builder<Object, Object> out;
if (fieldType != null) {
@SuppressWarnings("unchecked") PMap<Object, Object> mapType = (PMap<Object, Object>) fieldType;
keyType = mapType.keyDescriptor();
valueType = mapType.itemDescriptor();
out = mapType.builder();
} else {
out = new PMap.DefaultBuilder<>();
}
FieldInfo keyInfo = new FieldInfo(1, keyT);
FieldInfo itemInfo = new FieldInfo(2, itemT);
for (int i = 0; i < size; ++i) {
Object key = readFieldValue(in, keyInfo, keyType, strict);
Object value = readFieldValue(in, itemInfo, valueType, strict);
if (key != null && value != null) {
out.put(key, value);
} else if (strict) {
if (key == null) {
throw new SerializerException("Null key in map");
} else {
throw new SerializerException("Null value in map");
}
}
}
return out.build();
}
case BinaryType.SET:
{
final byte itemT = in.expectByte();
final int size = in.expectUInt32();
PDescriptor entryType = null;
PSet.Builder<Object> out;
if (fieldType != null) {
@SuppressWarnings("unchecked") PSet<Object> setType = (PSet<Object>) fieldType;
entryType = setType.itemDescriptor();
out = setType.builder();
} else {
out = new PSet.DefaultBuilder<>();
}
FieldInfo itemInfo = new FieldInfo(0, itemT);
for (int i = 0; i < size; ++i) {
Object value = readFieldValue(in, itemInfo, entryType, strict);
if (value != null) {
out.add(value);
} else if (strict) {
throw new SerializerException("Null value in set");
}
}
return out.build();
}
case BinaryType.LIST:
{
final byte itemT = in.expectByte();
final int size = in.expectUInt32();
PDescriptor entryType = null;
PList.Builder<Object> out;
if (fieldType != null) {
@SuppressWarnings("unchecked") PList<Object> listType = (PList<Object>) fieldType;
entryType = listType.itemDescriptor();
out = listType.builder();
} else {
out = new PList.DefaultBuilder<>();
}
FieldInfo itemInfo = new FieldInfo(0, itemT);
for (int i = 0; i < size; ++i) {
Object value = readFieldValue(in, itemInfo, entryType, strict);
if (value != null) {
out.add(value);
} else if (strict) {
throw new SerializerException("Null value in list");
}
}
return out.build();
}
default:
throw new SerializerException("unknown data type: " + fieldInfo.getType());
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class BinaryFormatUtils method readMessage.
/**
* Read message from reader.
*
* @param input The input reader.
* @param descriptor The message descriptor.
* @param strict If the message should be read in strict mode.
* @param <Message> The message type.
* @param <Field> The field type.
* @return The read and parsed message.
* @throws IOException If read failed.
*/
public static <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(BigEndianBinaryReader input, PMessageDescriptor<Message, Field> descriptor, boolean strict) throws IOException {
PMessageBuilder<Message, Field> builder = descriptor.builder();
if (builder instanceof BinaryReader) {
((BinaryReader) builder).readBinary(input, strict);
} else {
FieldInfo fieldInfo = readFieldInfo(input);
while (fieldInfo != null) {
PField field = descriptor.findFieldById(fieldInfo.getId());
if (field != null) {
Object value = readFieldValue(input, fieldInfo, field.getDescriptor(), strict);
builder.set(field.getId(), value);
} else {
readFieldValue(input, fieldInfo, null, false);
}
fieldInfo = readFieldInfo(input);
}
if (strict) {
try {
builder.validate();
} catch (IllegalStateException e) {
throw new SerializerException(e, e.getMessage());
}
}
}
return builder.build();
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class MessageCollectors method toFile.
/**
* write stream of messages to file.
*
* @param file The file to write.
* @param serializer The serializer to use.
* @param <Message> The message type.
* @param <Field> The field type.
* @return The collector.
*/
@Nonnull
public static <Message extends PMessage<Message, Field>, Field extends PField> Collector<Message, OutputStream, Integer> toFile(File file, Serializer serializer) {
final AtomicInteger result = new AtomicInteger(0);
return Collector.of(Suppliers.memoize(() -> {
// Delay file creation until the write starts.
try {
return new BufferedOutputStream(new FileOutputStream(file));
} catch (IOException e) {
throw new UncheckedIOException("Unable to open " + file.getName(), e);
}
}), (outputStream, t) -> {
try {
synchronized (result) {
result.addAndGet(serializer.serialize(outputStream, t));
if (!serializer.binaryProtocol()) {
result.addAndGet(maybeWriteBytes(outputStream, MessageStreams.READABLE_ENTRY_SEP));
}
}
} catch (SerializerException e) {
throw new UncheckedIOException("Bad data", e);
} catch (IOException e) {
throw new UncheckedIOException("Unable to write to " + file.getName(), e);
}
}, (a, b) -> a, (outputStream) -> {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
throw new UncheckedIOException("Unable to close " + file.getName(), e);
}
return result.getAndSet(0);
});
}
Aggregations