1 24 package org.objectweb.jalisto.se.storage.raf.log.synraf.action; 25 26 import org.objectweb.jalisto.se.storage.raf.log.backup.LogObject; 27 28 import java.io.*; 29 30 public class LogRecord extends LogObject { 31 32 public LogRecord() { 33 } 34 35 public LogRecord(long address, byte[] before, int newDatasLength) { 36 this.address = address; 37 this.before = before; 38 this.newDatasLength = newDatasLength; 39 } 40 41 public long getAddress() { 42 return address; 43 } 44 45 public void setAddress(long address) { 46 this.address = address; 47 } 48 49 public byte[] getBefore() { 50 return before; 51 } 52 53 public void setBefore(byte[] before) { 54 this.before = before; 55 } 56 57 public int getNewDatasLength() { 58 return newDatasLength; 59 } 60 61 public void setNewDatasLength(int newDatasLength) { 62 this.newDatasLength = newDatasLength; 63 } 64 65 public Object internalReadFrom(DataInput in) throws IOException { 66 long address = in.readLong(); 67 int newDatasLength = in.readInt(); 68 byte[] before = new byte[in.readInt()]; 69 in.readFully(before); 70 return new LogRecord(address, before, newDatasLength); 71 } 72 73 public void writeTo(DataOutput out) throws IOException { 74 LogObject.writeLogRecord(out, address, before, newDatasLength); 75 } 76 77 public void undoOn(RandomAccessFile raf) throws IOException { 78 System.out.println("undo " + this.toString()); 79 if (raf.length() == (address + newDatasLength)) { 80 raf.setLength(address); 81 } 82 raf.seek(address); 83 raf.write(before); 84 } 85 86 public void corrupt(RandomAccessFile raf) throws IOException { 87 byte[] newDatas = new byte[newDatasLength / 2]; 88 raf.seek(address); 89 raf.readFully(newDatas); 90 91 if (raf.length() == (address + newDatasLength)) { 92 raf.setLength(address); 93 } 94 raf.seek(address); 95 raf.write(before); 96 97 raf.seek(address); 98 raf.write(newDatas); 99 } 100 101 102 public String toString() { 103 StringBuffer sb = new StringBuffer (); 104 sb.append("LogRecord("); 105 sb.append(address).append(","); 106 sb.append(newDatasLength).append(","); 107 sb.append("b[").append(before.length).append("])"); 108 return sb.toString(); 109 } 110 111 112 private long address; 113 private int newDatasLength; 114 private byte[] before; 115 116 117 static final long serialVersionUID = -7589636669041161459L; 118 } 119 | Popular Tags |