Bucket.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package oram;
  2. import java.util.Random;
  3. import exceptions.LengthNotMatchException;
  4. public class Bucket {
  5. private int numBytes;
  6. private Tuple[] tuples;
  7. public Bucket(int numTuples, int[] tupleParams, Random rand) {
  8. if (tupleParams.length != 4)
  9. throw new LengthNotMatchException(tupleParams.length + " != 4");
  10. tuples = new Tuple[numTuples];
  11. for (int i = 0; i < numTuples; i++)
  12. tuples[i] = new Tuple(tupleParams[0], tupleParams[1], tupleParams[2], tupleParams[3], rand);
  13. numBytes = numTuples * tuples[0].getNumBytes();
  14. }
  15. public Bucket(Tuple[] tuples) {
  16. if (tuples == null)
  17. throw new NullPointerException();
  18. this.tuples = tuples;
  19. numBytes = tuples.length * tuples[0].getNumBytes();
  20. }
  21. // deep copy
  22. public Bucket(Bucket b) {
  23. numBytes = b.getNumBytes();
  24. tuples = new Tuple[b.getNumTuples()];
  25. for (int i = 0; i < tuples.length; i++)
  26. tuples[i] = new Tuple(b.getTuple(i));
  27. }
  28. public int getNumBytes() {
  29. return numBytes;
  30. }
  31. public int getNumTuples() {
  32. return tuples.length;
  33. }
  34. public Tuple getTuple(int i) {
  35. return tuples[i];
  36. }
  37. public void setTuple(int i, Tuple tuple) {
  38. if (!tuples[i].sameLength(tuple))
  39. throw new LengthNotMatchException(tuples[i].getNumBytes() + " != " + tuple.getNumBytes());
  40. tuples[i] = tuple;
  41. }
  42. public Bucket xor(Bucket b) {
  43. if (!this.sameLength(b))
  44. throw new LengthNotMatchException(numBytes + " != " + b.getNumBytes());
  45. Tuple[] newTuples = new Tuple[tuples.length];
  46. for (int i = 0; i < tuples.length; i++)
  47. newTuples[i] = tuples[i].xor(b.getTuple(i));
  48. return new Bucket(newTuples);
  49. }
  50. public void setXor(Bucket b) {
  51. if (!this.sameLength(b))
  52. throw new LengthNotMatchException(numBytes + " != " + b.getNumBytes());
  53. for (int i = 0; i < tuples.length; i++)
  54. tuples[i].setXor(b.getTuple(i));
  55. }
  56. public boolean sameLength(Bucket b) {
  57. return numBytes == b.getNumBytes();
  58. }
  59. public byte[] toByteArray() {
  60. int tupleBytes = tuples[0].getNumBytes();
  61. byte[] bucket = new byte[numBytes];
  62. for (int i = 0; i < tuples.length; i++) {
  63. byte[] tuple = tuples[i].toByteArray();
  64. System.arraycopy(tuple, 0, bucket, i * tupleBytes, tupleBytes);
  65. }
  66. return bucket;
  67. }
  68. @Override
  69. public String toString() {
  70. String str = "Bucket:";
  71. for (int i = 0; i < tuples.length; i++)
  72. str += ("\n " + tuples[i]);
  73. return str;
  74. }
  75. }