1 24 package org.objectweb.jalisto.se.storage.raf.log.backup; 25 26 import java.io.*; 27 28 public class LogFileLengthModification extends LogObject { 29 30 public LogFileLengthModification() { 31 } 32 33 public LogFileLengthModification(byte[] deletedBytes) { 34 this.deletedBytes = deletedBytes; 35 this.isShrink = true; 36 this.sizeModification = deletedBytes.length; 37 } 38 39 public LogFileLengthModification(long sizeModification) { 40 this.sizeModification = sizeModification; 41 this.isShrink = false; 42 } 43 44 public Object internalReadFrom(DataInput in) throws IOException { 45 LogFileLengthModification result; 46 boolean isShrink = in.readBoolean(); 47 long sizeModification = in.readLong(); 48 if (isShrink) { 49 byte[] datas = new byte[(new Long (sizeModification)).intValue()]; 50 in.readFully(datas); 51 result = new LogFileLengthModification(datas); 52 } else { 53 result = new LogFileLengthModification(sizeModification); 54 } 55 return result; 56 } 57 58 public void writeTo(DataOutput out) throws IOException { 59 if (isShrink) { 60 LogObject.writeLogFileLengthModification(out, deletedBytes); 61 } else { 62 LogObject.writeLogFileLengthModification(out, sizeModification); 63 } 64 } 65 66 public void undoOn(RandomAccessFile raf) throws IOException { 67 System.out.println("undo " + this.toString()); 68 if (isShrink) { 69 raf.seek(raf.length()); 70 raf.write(deletedBytes); 71 } else { 72 raf.setLength(raf.length() - sizeModification); 73 } 74 } 75 76 public void corrupt(RandomAccessFile raf) throws IOException { 77 if (isShrink) { 78 raf.seek(raf.length()); 79 for (int i = 0; i < (deletedBytes.length / 2); i++) { 80 raf.writeByte(deletedBytes[i]); 81 } 82 } else { 83 raf.setLength(raf.length() - (sizeModification / 2)); 84 } 85 } 86 87 88 public String toString() { 89 StringBuffer sb = new StringBuffer (); 90 sb.append("LoggFileLengthModification("); 91 sb.append("isShrink=").append(isShrink).append(","); 92 sb.append(sizeModification); 93 if (isShrink) { 94 sb.append(",b[").append(deletedBytes.length).append("]"); 95 } 96 sb.append(")"); 97 return sb.toString(); 98 } 99 100 101 private boolean isShrink; 102 private long sizeModification; 103 private byte[] deletedBytes; 104 105 static final long serialVersionUID = -7589636669041261459L; 106 } 107 | Popular Tags |