- Timestamp:
- 09/27/07 12:58:18 (2 years ago)
- Files:
-
- 1 modified
-
cs420/asst3/asst3420.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cs420/asst3/asst3420.java
r861 r862 14 14 private static final int compareBlocks(byte[] a, byte[] b) { 15 15 // We'll just assume a lexical comparison 16 return a.compareTo(b); 16 for (int i = 0; i < BLOCKSIZE; ++i) { 17 int d = a[i] - b[i]; 18 if (d != 0) 19 return d; 20 } 21 return 0; 17 22 } 18 23 … … 32 37 } 33 38 39 private static void copy(DataInputStream in, DataOutputStream out, 40 byte[] buf) throws IOException { 41 try { 42 while (true) { 43 in.readFully(buf); 44 out.write(buf); 45 } 46 } catch (EOFException e) { 47 // done! 48 } finally { 49 in.close(); 50 } 51 } 52 34 53 private static class Statistics { 35 54 public int onlyFirst = 0, onlySecond = 0, both = 0; 36 55 } 37 56 38 private static String readBlock(DataInput in, byte[] buf) {39 return40 }41 42 57 // Do a two way merge 43 58 private static Statistics merge2(InputStream is1, InputStream is2, 44 OutputStream os) {59 OutputStream os) throws IOException { 45 60 Statistics stats = new Statistics(); 46 DataInput in1 = new DataInputStream(is1),61 DataInputStream in1 = new DataInputStream(is1), 47 62 in2 = new DataInputStream(is2); 48 DataOutput out = new DataOutputStream(os);63 DataOutputStream out = new DataOutputStream(os); 49 64 65 byte[] b1 = new byte[BLOCKSIZE], b2 = new byte[BLOCKSIZE]; 66 boolean v1 = false, v2 = false; // Do b1 and b2 have valid, cur data? 50 67 51 String d1 = null, d2 = null; 52 68 try { 69 while (true) { 70 // Do any reading necessary 71 if (!v1) { 72 in1.readFully(b1); 73 v1 = true; 74 } 75 if (!v2) { 76 in1.readFully(b2); 77 v2 = true; 78 } 79 80 int c = compareBlocks(b1, b2); 81 if (c == 0) { 82 out.write(b1); 83 // Assume streams are already filtered for dupes! 84 v1 = v2 = false; 85 ++stats.both; 86 } else if (c < 0) { 87 out.write(b1); 88 v1 = false; 89 ++stats.onlyFirst; 90 } else { 91 out.write(b2); 92 v2 = false; 93 ++stats.onlySecond; 94 } 95 } 96 } catch (EOFException e) { 97 // Just output whatever's left 98 if (v1) 99 out.write(b1); 100 if (v2) 101 out.write(b2); 102 copy(in1, out, b1); 103 copy(in2, out, b2); 104 } finally { 105 out.close(); 106 } 53 107 54 108 return stats;
