ComUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package communication;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import com.oblivm.backend.gc.GCSignal;
  9. import oram.Bucket;
  10. import oram.Tuple;
  11. public class ComUtil {
  12. public static byte[] serialize(byte[][] in) {
  13. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  14. DataOutputStream dos = new DataOutputStream(baos);
  15. byte[] out = null;
  16. try {
  17. dos.writeInt(in.length);
  18. dos.writeInt(in[0].length);
  19. for (int i = 0; i < in.length; i++)
  20. dos.write(in[i]);
  21. out = baos.toByteArray();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. } finally {
  25. try {
  26. dos.close();
  27. baos.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. return out;
  33. }
  34. public static byte[][] toDoubleByteArray(byte[] in) {
  35. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  36. DataInputStream dis = new DataInputStream(bais);
  37. byte[][] out = null;
  38. try {
  39. int len1 = dis.readInt();
  40. int len2 = dis.readInt();
  41. out = new byte[len1][len2];
  42. for (int i = 0; i < len1; i++)
  43. dis.read(out[i]);
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. } finally {
  47. try {
  48. dis.close();
  49. bais.close();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. return out;
  55. }
  56. public static byte[] serialize(byte[][][] in) {
  57. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  58. DataOutputStream dos = new DataOutputStream(baos);
  59. byte[] out = null;
  60. try {
  61. dos.writeInt(in.length);
  62. dos.writeInt(in[0].length);
  63. dos.writeInt(in[0][0].length);
  64. for (int i = 0; i < in.length; i++)
  65. for (int j = 0; j < in[i].length; j++)
  66. dos.write(in[i][j]);
  67. out = baos.toByteArray();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. } finally {
  71. try {
  72. dos.close();
  73. baos.close();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. return out;
  79. }
  80. public static byte[][][] toTripleByteArray(byte[] in) {
  81. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  82. DataInputStream dis = new DataInputStream(bais);
  83. byte[][][] out = null;
  84. try {
  85. int len1 = dis.readInt();
  86. int len2 = dis.readInt();
  87. int len3 = dis.readInt();
  88. out = new byte[len1][len2][len3];
  89. for (int i = 0; i < len1; i++)
  90. for (int j = 0; j < len2; j++)
  91. dis.read(out[i][j]);
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. } finally {
  95. try {
  96. dis.close();
  97. bais.close();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. return out;
  103. }
  104. public static byte[] serialize(int[] in) {
  105. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  106. DataOutputStream dos = new DataOutputStream(baos);
  107. byte[] out = null;
  108. try {
  109. dos.writeInt(in.length);
  110. for (int i = 0; i < in.length; i++)
  111. dos.writeInt(in[i]);
  112. out = baos.toByteArray();
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. } finally {
  116. try {
  117. dos.close();
  118. baos.close();
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123. return out;
  124. }
  125. public static int[] toIntArray(byte[] in) {
  126. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  127. DataInputStream dis = new DataInputStream(bais);
  128. int[] out = null;
  129. try {
  130. int len1 = dis.readInt();
  131. out = new int[len1];
  132. for (int i = 0; i < len1; i++)
  133. out[i] = dis.readInt();
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. } finally {
  137. try {
  138. dis.close();
  139. bais.close();
  140. } catch (IOException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. return out;
  145. }
  146. public static byte[] serialize(int[][] in) {
  147. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  148. DataOutputStream dos = new DataOutputStream(baos);
  149. byte[] out = null;
  150. try {
  151. dos.writeInt(in.length);
  152. dos.writeInt(in[0].length);
  153. for (int i = 0; i < in.length; i++)
  154. for (int j = 0; j < in[i].length; j++)
  155. dos.writeInt(in[i][j]);
  156. out = baos.toByteArray();
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. } finally {
  160. try {
  161. dos.close();
  162. baos.close();
  163. } catch (IOException e) {
  164. e.printStackTrace();
  165. }
  166. }
  167. return out;
  168. }
  169. public static int[][] toDoubleIntArray(byte[] in) {
  170. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  171. DataInputStream dis = new DataInputStream(bais);
  172. int[][] out = null;
  173. try {
  174. int len1 = dis.readInt();
  175. int len2 = dis.readInt();
  176. out = new int[len1][len2];
  177. for (int i = 0; i < len1; i++)
  178. for (int j = 0; j < len2; j++)
  179. out[i][j] = dis.readInt();
  180. } catch (IOException e) {
  181. e.printStackTrace();
  182. } finally {
  183. try {
  184. dis.close();
  185. bais.close();
  186. } catch (IOException e) {
  187. e.printStackTrace();
  188. }
  189. }
  190. return out;
  191. }
  192. public static byte[] serialize(Tuple in) {
  193. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  194. DataOutputStream dos = new DataOutputStream(baos);
  195. byte[] out = null;
  196. try {
  197. dos.writeInt(in.getF().length);
  198. dos.writeInt(in.getN().length);
  199. dos.writeInt(in.getL().length);
  200. dos.writeInt(in.getA().length);
  201. dos.write(in.getF());
  202. dos.write(in.getN());
  203. dos.write(in.getL());
  204. dos.write(in.getA());
  205. out = baos.toByteArray();
  206. } catch (IOException e) {
  207. e.printStackTrace();
  208. } finally {
  209. try {
  210. dos.close();
  211. baos.close();
  212. } catch (IOException e) {
  213. e.printStackTrace();
  214. }
  215. }
  216. return out;
  217. }
  218. public static Tuple toTuple(byte[] in) {
  219. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  220. DataInputStream dis = new DataInputStream(bais);
  221. Tuple out = null;
  222. try {
  223. int f = dis.readInt();
  224. int n = dis.readInt();
  225. int l = dis.readInt();
  226. int a = dis.readInt();
  227. byte[] F = new byte[f];
  228. byte[] N = new byte[n];
  229. byte[] L = new byte[l];
  230. byte[] A = new byte[a];
  231. dis.read(F);
  232. dis.read(N);
  233. dis.read(L);
  234. dis.read(A);
  235. out = new Tuple(F, N, L, A);
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. } finally {
  239. try {
  240. dis.close();
  241. bais.close();
  242. } catch (IOException e) {
  243. e.printStackTrace();
  244. }
  245. }
  246. return out;
  247. }
  248. public static byte[] serialize(Tuple[] in) {
  249. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  250. DataOutputStream dos = new DataOutputStream(baos);
  251. byte[] out = null;
  252. try {
  253. dos.writeInt(in.length);
  254. dos.writeInt(in[0].getF().length);
  255. dos.writeInt(in[0].getN().length);
  256. dos.writeInt(in[0].getL().length);
  257. dos.writeInt(in[0].getA().length);
  258. for (int i = 0; i < in.length; i++) {
  259. dos.write(in[i].getF());
  260. dos.write(in[i].getN());
  261. dos.write(in[i].getL());
  262. dos.write(in[i].getA());
  263. }
  264. out = baos.toByteArray();
  265. } catch (IOException e) {
  266. e.printStackTrace();
  267. } finally {
  268. try {
  269. dos.close();
  270. baos.close();
  271. } catch (IOException e) {
  272. e.printStackTrace();
  273. }
  274. }
  275. return out;
  276. }
  277. public static Tuple[] toTupleArray(byte[] in) {
  278. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  279. DataInputStream dis = new DataInputStream(bais);
  280. Tuple[] out = null;
  281. try {
  282. int len = dis.readInt();
  283. int f = dis.readInt();
  284. int n = dis.readInt();
  285. int l = dis.readInt();
  286. int a = dis.readInt();
  287. out = new Tuple[len];
  288. for (int i = 0; i < len; i++) {
  289. byte[] F = new byte[f];
  290. byte[] N = new byte[n];
  291. byte[] L = new byte[l];
  292. byte[] A = new byte[a];
  293. dis.read(F);
  294. dis.read(N);
  295. dis.read(L);
  296. dis.read(A);
  297. out[i] = new Tuple(F, N, L, A);
  298. }
  299. } catch (IOException e) {
  300. e.printStackTrace();
  301. } finally {
  302. try {
  303. dis.close();
  304. bais.close();
  305. } catch (IOException e) {
  306. e.printStackTrace();
  307. }
  308. }
  309. return out;
  310. }
  311. public static byte[] serialize(GCSignal[] in) {
  312. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  313. DataOutputStream dos = new DataOutputStream(baos);
  314. byte[] out = null;
  315. try {
  316. dos.writeInt(in.length);
  317. for (int i = 0; i < in.length; i++)
  318. dos.write(in[i].bytes);
  319. out = baos.toByteArray();
  320. } catch (IOException e) {
  321. e.printStackTrace();
  322. } finally {
  323. try {
  324. dos.close();
  325. baos.close();
  326. } catch (IOException e) {
  327. e.printStackTrace();
  328. }
  329. }
  330. return out;
  331. }
  332. public static GCSignal[] toGCSignalArray(byte[] in) {
  333. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  334. DataInputStream dis = new DataInputStream(bais);
  335. GCSignal[] out = null;
  336. try {
  337. int len1 = dis.readInt();
  338. out = new GCSignal[len1];
  339. for (int i = 0; i < len1; i++) {
  340. out[i] = new GCSignal(new byte[GCSignal.len]);
  341. dis.read(out[i].bytes);
  342. }
  343. } catch (IOException e) {
  344. e.printStackTrace();
  345. } finally {
  346. try {
  347. dis.close();
  348. bais.close();
  349. } catch (IOException e) {
  350. e.printStackTrace();
  351. }
  352. }
  353. return out;
  354. }
  355. public static byte[] serialize(GCSignal[][] in) {
  356. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  357. DataOutputStream dos = new DataOutputStream(baos);
  358. byte[] out = null;
  359. try {
  360. dos.writeInt(in.length);
  361. dos.writeInt(in[0].length);
  362. for (int i = 0; i < in.length; i++)
  363. for (int j = 0; j < in[i].length; j++)
  364. dos.write(in[i][j].bytes);
  365. out = baos.toByteArray();
  366. } catch (IOException e) {
  367. e.printStackTrace();
  368. } finally {
  369. try {
  370. dos.close();
  371. baos.close();
  372. } catch (IOException e) {
  373. e.printStackTrace();
  374. }
  375. }
  376. return out;
  377. }
  378. public static GCSignal[][] toDoubleGCSignalArray(byte[] in) {
  379. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  380. DataInputStream dis = new DataInputStream(bais);
  381. GCSignal[][] out = null;
  382. try {
  383. int len1 = dis.readInt();
  384. int len2 = dis.readInt();
  385. out = new GCSignal[len1][len2];
  386. for (int i = 0; i < len1; i++)
  387. for (int j = 0; j < len2; j++) {
  388. out[i][j] = new GCSignal(new byte[GCSignal.len]);
  389. dis.read(out[i][j].bytes);
  390. }
  391. } catch (IOException e) {
  392. e.printStackTrace();
  393. } finally {
  394. try {
  395. dis.close();
  396. bais.close();
  397. } catch (IOException e) {
  398. e.printStackTrace();
  399. }
  400. }
  401. return out;
  402. }
  403. public static byte[] serialize(GCSignal[][][] in) {
  404. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  405. DataOutputStream dos = new DataOutputStream(baos);
  406. byte[] out = null;
  407. try {
  408. dos.writeInt(in.length);
  409. dos.writeInt(in[0].length);
  410. dos.writeInt(in[0][0].length);
  411. for (int i = 0; i < in.length; i++)
  412. for (int j = 0; j < in[i].length; j++)
  413. for (int k = 0; k < in[i][j].length; k++)
  414. dos.write(in[i][j][k].bytes);
  415. out = baos.toByteArray();
  416. } catch (IOException e) {
  417. e.printStackTrace();
  418. } finally {
  419. try {
  420. dos.close();
  421. baos.close();
  422. } catch (IOException e) {
  423. e.printStackTrace();
  424. }
  425. }
  426. return out;
  427. }
  428. public static GCSignal[][][] toTripleGCSignalArray(byte[] in) {
  429. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  430. DataInputStream dis = new DataInputStream(bais);
  431. GCSignal[][][] out = null;
  432. try {
  433. int len1 = dis.readInt();
  434. int len2 = dis.readInt();
  435. int len3 = dis.readInt();
  436. out = new GCSignal[len1][len2][len3];
  437. for (int i = 0; i < len1; i++)
  438. for (int j = 0; j < len2; j++)
  439. for (int k = 0; k < len3; k++) {
  440. out[i][j][k] = new GCSignal(new byte[GCSignal.len]);
  441. dis.read(out[i][j][k].bytes);
  442. }
  443. } catch (IOException e) {
  444. e.printStackTrace();
  445. } finally {
  446. try {
  447. dis.close();
  448. bais.close();
  449. } catch (IOException e) {
  450. e.printStackTrace();
  451. }
  452. }
  453. return out;
  454. }
  455. public static byte[] serialize(GCSignal[][][][] in) {
  456. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  457. DataOutputStream dos = new DataOutputStream(baos);
  458. byte[] out = null;
  459. try {
  460. dos.writeInt(in.length);
  461. dos.writeInt(in[0].length);
  462. dos.writeInt(in[0][0].length);
  463. dos.writeInt(in[0][0][0].length);
  464. for (int i = 0; i < in.length; i++)
  465. for (int j = 0; j < in[i].length; j++)
  466. for (int k = 0; k < in[i][j].length; k++)
  467. for (int l = 0; l < in[i][j][k].length; l++)
  468. dos.write(in[i][j][k][l].bytes);
  469. out = baos.toByteArray();
  470. } catch (IOException e) {
  471. e.printStackTrace();
  472. } finally {
  473. try {
  474. dos.close();
  475. baos.close();
  476. } catch (IOException e) {
  477. e.printStackTrace();
  478. }
  479. }
  480. return out;
  481. }
  482. public static GCSignal[][][][] toQuadGCSignalArray(byte[] in) {
  483. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  484. DataInputStream dis = new DataInputStream(bais);
  485. GCSignal[][][][] out = null;
  486. try {
  487. int len1 = dis.readInt();
  488. int len2 = dis.readInt();
  489. int len3 = dis.readInt();
  490. int len4 = dis.readInt();
  491. out = new GCSignal[len1][len2][len3][len4];
  492. for (int i = 0; i < len1; i++)
  493. for (int j = 0; j < len2; j++)
  494. for (int k = 0; k < len3; k++)
  495. for (int l = 0; l < len4; l++) {
  496. out[i][j][k][l] = new GCSignal(new byte[GCSignal.len]);
  497. dis.read(out[i][j][k][l].bytes);
  498. }
  499. } catch (IOException e) {
  500. e.printStackTrace();
  501. } finally {
  502. try {
  503. dis.close();
  504. bais.close();
  505. } catch (IOException e) {
  506. e.printStackTrace();
  507. }
  508. }
  509. return out;
  510. }
  511. public static byte[] serialize(Bucket[] in) {
  512. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  513. DataOutputStream dos = new DataOutputStream(baos);
  514. byte[] out = null;
  515. try {
  516. byte[] b = serialize(Bucket.bucketsToTuples(in));
  517. dos.writeInt(in.length);
  518. dos.writeInt(in[0].getNumTuples());
  519. dos.writeInt(in[1].getNumTuples());
  520. dos.writeInt(b.length);
  521. dos.write(b);
  522. out = baos.toByteArray();
  523. } catch (IOException e) {
  524. e.printStackTrace();
  525. } finally {
  526. try {
  527. dos.close();
  528. baos.close();
  529. } catch (IOException e) {
  530. e.printStackTrace();
  531. }
  532. }
  533. return out;
  534. }
  535. public static Bucket[] toBucketArray(byte[] in) {
  536. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  537. DataInputStream dis = new DataInputStream(bais);
  538. Bucket[] out = null;
  539. try {
  540. int d = dis.readInt();
  541. int sw = dis.readInt();
  542. int w = dis.readInt();
  543. int bytes = dis.readInt();
  544. byte[] b = new byte[bytes];
  545. dis.read(b);
  546. Tuple[] tuples = toTupleArray(b);
  547. out = Bucket.tuplesToBuckets(tuples, d, sw, w);
  548. } catch (IOException e) {
  549. e.printStackTrace();
  550. } finally {
  551. try {
  552. dis.close();
  553. bais.close();
  554. } catch (IOException e) {
  555. e.printStackTrace();
  556. }
  557. }
  558. return out;
  559. }
  560. public static byte[] serialize(ArrayList<byte[]> in) {
  561. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  562. DataOutputStream dos = new DataOutputStream(baos);
  563. byte[] out = null;
  564. try {
  565. dos.writeInt(in.size());
  566. dos.writeInt(in.get(0).length);
  567. for (int i = 0; i < in.size(); i++)
  568. dos.write(in.get(i));
  569. out = baos.toByteArray();
  570. } catch (IOException e) {
  571. e.printStackTrace();
  572. } finally {
  573. try {
  574. dos.close();
  575. baos.close();
  576. } catch (IOException e) {
  577. e.printStackTrace();
  578. }
  579. }
  580. return out;
  581. }
  582. public static ArrayList<byte[]> toArrayList(byte[] in) {
  583. ByteArrayInputStream bais = new ByteArrayInputStream(in);
  584. DataInputStream dis = new DataInputStream(bais);
  585. ArrayList<byte[]> out = null;
  586. try {
  587. int len = dis.readInt();
  588. int bytes = dis.readInt();
  589. out = new ArrayList<byte[]>(len);
  590. for (int i = 0; i < len; i++) {
  591. byte[] b = new byte[bytes];
  592. dis.read(b);
  593. out.add(b);
  594. }
  595. } catch (IOException e) {
  596. e.printStackTrace();
  597. } finally {
  598. try {
  599. dis.close();
  600. bais.close();
  601. } catch (IOException e) {
  602. e.printStackTrace();
  603. }
  604. }
  605. return out;
  606. }
  607. }