Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to use protobuf in java

// Here is sample code from https://developers.google.com/protocol-buffers/docs/javatutorial

import com.example.tutorial.protos.AddressBook;
import com.example.tutorial.protos.Person;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;

class ListPeople {
  // Iterates though all people in the AddressBook and prints info about them.
  static void Print(AddressBook addressBook) {
    for (Person person: addressBook.getPeopleList()) {
      System.out.println("Person ID: " + person.getId());
      System.out.println("  Name: " + person.getName());
      if (person.hasEmail()) {
        System.out.println("  E-mail address: " + person.getEmail());
      }

      for (Person.PhoneNumber phoneNumber : person.getPhonesList()) {
        switch (phoneNumber.getType()) {
          case MOBILE:
            System.out.print("  Mobile phone #: ");
            break;
          case HOME:
            System.out.print("  Home phone #: ");
            break;
          case WORK:
            System.out.print("  Work phone #: ");
            break;
        }
        System.out.println(phoneNumber.getNumber());
      }
    }
  }

  // Main function:  Reads the entire address book from a file and prints all
  //   the information inside.
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage:  ListPeople ADDRESS_BOOK_FILE");
      System.exit(-1);
    }

    // Read the existing address book.
    AddressBook addressBook =
      AddressBook.parseFrom(new FileInputStream(args[0]));

    Print(addressBook);
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to add classpath in spring boot 
Java :: change activity main drawer items text color android 
Java :: recycler view event being raised multiple times 
Java :: Copying Arrays Using arraycopy() method Java 
Java :: java map get value 
Java :: recursion java 
Java :: java search tree 
Java :: how to set edittext color in android 
Java :: java bufferedreader 
Java :: is java pass by value or pass by reference 
Java :: java lambda function 
Java :: java join array 
Java :: final class java 
Java :: casting in java 
Java :: Calling A Class From Another Class In Java 
Java :: how to be good at javasctript 
Java :: get the logged in user java 
Java :: java stack with max size 
Java :: java color light gray 
Java :: bukkit e.getCurrentItem() bytes? 
Sql :: delete all nodes neo4j 
Sql :: How to select rows with no matching entry in another table? 
Sql :: port 5432 is already in use mac 
Sql :: uninstall postgresql mac 
Sql :: last 24 HOUR data in mysql 
Sql :: Cannot load driver class: com.mysql.cj.jdbc.Driver 
Sql :: mysql command line xampp ubuntu 
Sql :: sql first day of current year 
Sql :: how to see which columns are indexxed mysql 
Sql :: mysql version check cmd 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =