-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathListPeople.java
125 lines (113 loc) · 4.71 KB
/
ListPeople.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// See README.txt for information and build instructions.
import com.example.tutorial.AddressBookProtos.AddressBook;
import com.example.tutorial.AddressBookProtos.Person;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class ListPeople {
// load native library
static {
System.loadLibrary("readprotonative");
}
// Iterates though all people in the AddressBook and prints info about them.
static void Print(AddressBook addressBook) {
System.out.println("\nReading AddressBook Proto from Java");
// Iterate through persons
for (Person person: addressBook.getPeopleList()) {
System.out.println("\nPerson ID: " + person.getId());
System.out.println(" Name: " + person.getName());
if (!person.getEmail().isEmpty()) {
System.out.println(" E-mail address: " + person.getEmail());
}
// Iterate through phone numbers
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;
default:
System.out.println(" Unknown phone #: ");
break;
}
System.out.println(phoneNumber.getNumber());
}
}
}
// Native method that takes proto file name and reads it
static native void readProtoNative(String filename);
// Native method that takes a byte array from Java program and reads it
static native void readProtoNativeByteArray(byte[] byteArray);
// Native method that takes a byte array, allows user to change it and returns it
static native byte[] readProtoNativeByteArrayAndModify(byte[] byteArray);
// 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 != 2) {
System.err.println("Usage: ListPeople ADDRESS_BOOK_FILE OPERATION_TYPE");
System.out.println("OPERATION_TYPES");
System.out.println("1 -> Read file using native C++ application");
System.out.println("2 -> Read file in Java, pass byte array to C++ application and parse byte array in C++");
System.out.println("3 -> Read file in Java, pass and modify byte array in C++ application and return to Java");
System.exit(-1);
}
int operation_type = Integer.parseInt(args[1]);
switch(operation_type){
case 1:
System.out.println("Selected option: Read file using native C++ application");
readProtoNative(args[0]);
break;
case 2:
System.out.println("Selected option: Read file in Java, pass byte array to C++ application and parse byte array in C++");
AddressBook aBook =
AddressBook.parseFrom(new FileInputStream(args[0]));
byte[] bArray = aBook.toByteArray();
readProtoNativeByteArray(bArray);
break;
case 3:
System.out.println("Selected option: Read file in Java, pass and modify byte array in C++ application and return to Java");
//AddressBook addressBook = null;
// Read the existing address book.
AddressBook addressBook =
AddressBook.parseFrom(new FileInputStream(args[0]));
byte[] bArray2 = addressBook.toByteArray();
byte[] bArray3 = readProtoNativeByteArrayAndModify(bArray2);
AddressBook abookReturned = AddressBook.parseFrom(bArray3);
System.out.println("\n\nReading returned byte array from C++ in Java");
Print(abookReturned);
// Store updated proto to file
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(args[0]);
outputStream.write(bArray3);
} catch(IOException ex) {
throw ex;
} finally {
outputStream.close();
}
System.out.println("File updated!!!\n\n");
}
}
}