Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Writing
- AddressBookProtos.Person.Builder personBuilder = AddressBookProtos.Person.newBuilder();
- personBuilder.setId(1);
- personBuilder.setName("Galder Zamarreño");
- personBuilder.setEmail("galder@galder.com");
- AddressBookProtos.Person person = personBuilder.build();
- hotRodClient.put("me", person);
- // Writing marshaller
- public byte[] objectToByteBuffer(Object o) throws Exception {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- Message message = (Message) o;
- byte[] name = message.getDescriptorForType().getFullName().getBytes("UTF-8");
- baos.write(name.length); // TODO: Send length as int and not byte
- baos.write(name);
- byte[] messageBytes = message.toByteArray();
- baos.write(messageBytes.length); // TODO: Send length as int and not byte
- baos.write(messageBytes);
- return baos.toByteArray();
- }
- // Reading marshaller
- public Object objectFromByteBuffer(byte[] buffer) throws Exception {
- ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
- byte[] name = new byte[bais.read()];
- bais.read(name); // TODO: Read fully??
- String className = mapping.get(new String(name, "UTF-8"));
- Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
- Method parseFromMethod = clazz.getMethod("parseFrom", byte[].class);
- byte[] message = new byte[bais.read()];
- bais.read(message); // TODO: Read fully??
- return parseFromMethod.invoke(null, message);
- }
- // Mapping of proto language descriptor name and class name shown below:
- FileDescriptorSet descriptorSet = FileDescriptorSet.parseFrom(new FileInputStream("src/main/desc/addressbook.desc"));
- for (FileDescriptorProto fdp: descriptorSet.getFileList()) {
- FileDescriptor fd = FileDescriptor.buildFrom(fdp, new FileDescriptor[]{});
- for(Descriptor descriptor : fd.getMessageTypes()) {
- String className = fdp.getOptions().getJavaPackage() + "." + fdp.getOptions().getJavaOuterClassname() + "$" + descriptor.getName();
- mapping.put(descriptor.getFullName(), className);
- }
- }
- // Reading
- AddressBookProtos.Person person = hotRodClient.get("me");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement