1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Protobuffer - Java client example


Below is an example class which can be used to communicate with Hyperion over the ProtoBuffer interface.
This code depends on the ProtoBuf library and the ProtoBuf interface definition file. The definition file needs to be compiled by protoc. When using Eclipse, this plugin may be very helpful and after installation set path to protoc and enable generation of java code on save.

package hyperion;

import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.google.protobuf.ByteString;

import proto.Message.ClearRequest;
import proto.Message.ColorRequest;
import proto.Message.HyperionReply;
import proto.Message.HyperionRequest;
import proto.Message.ImageRequest;

public class Hyperion {
   private final Socket mSocket;   
   public Hyperion(String address, int port) throws UnknownHostException, IOException {
     mSocket = new Socket(address, port);
   protected void finalize() throws Throwable {
     if (mSocket != null && mSocket.isConnected()) {
   public void clear(int priority) throws IOException {
     ClearRequest clearRequest = ClearRequest.newBuilder()
     HyperionRequest request = HyperionRequest.newBuilder()
         .setExtension(ClearRequest.clearRequest, clearRequest)
   public void clearall() throws IOException {
     HyperionRequest request = HyperionRequest.newBuilder()
   public void setColor(Color color, int priority) throws IOException {
     setColor(color, priority, -1);
   public void setColor(Color color, int priority, int duration_ms) throws IOException {
     ColorRequest colorRequest = ColorRequest.newBuilder()
     HyperionRequest request = HyperionRequest.newBuilder()
         .setExtension(ColorRequest.colorRequest, colorRequest)
   public void setImage(byte[] data, int width, int height, int priority) throws IOException {
     setImage(data, width, height, priority, -1);
   public void setImage(byte[] data, int width, int height, int priority, int duration_ms) throws IOException {
     ImageRequest imageRequest = ImageRequest.newBuilder()
     HyperionRequest request = HyperionRequest.newBuilder()
         .setExtension(ImageRequest.imageRequest, imageRequest)
   private void sendRequest(HyperionRequest request) throws IOException {
     int size = request.getSerializedSize();
     // create the header
     byte[] header = new byte[4];
     header[0] = (byte)((size >> 24) & 0xFF);
     header[1] = (byte)((size >> 16) & 0xFF);
     header[2] = (byte)((size >>  8) & 0xFF);
     header[3] = (byte)((size  ) & 0xFF);
     // write the data to the socket
     OutputStream output = mSocket.getOutputStream();
     HyperionReply reply = receiveReply();
     if (!reply.getSuccess()) {
   private HyperionReply receiveReply() throws IOException {
     InputStream input = mSocket.getInputStream();
     byte[] header = new byte[4];
     input.read(header, 0, 4);
     int size = (header[0]<<24) | (header[1]<<16) | (header[2]<<8) | (header[3]);
     byte[] data = new byte[size];
     input.read(data, 0, size);
     HyperionReply reply = HyperionReply.parseFrom(data);
     return reply;
   public static void main(String[] args) {
     try {
       Hyperion hyperion = new Hyperion("", 19445);
       hyperion.setColor(Color.RED, 50);
       byte[] imageData = {
           (byte)0xFF, (byte)0x00, (byte)0x00,
           (byte)0xFF, (byte)0xFF, (byte)0x00,
           (byte)0x00, (byte)0xFF, (byte)0x00,
           (byte)0x00, (byte)0xFF, (byte)0xFF,
           (byte)0x00, (byte)0x00, (byte)0xFF};
       hyperion.setImage(imageData, 5, 1, 50, 5000);
     } catch (Exception e) {

This page has been seen 3,103 times.

    • Last updated by on