1 24 package org.objectweb.jalisto.se.storage.raf.log.backup; 25 26 import org.objectweb.jalisto.se.storage.raf.log.synraf.action.LogRecord; 27 28 import java.io.*; 29 30 public abstract class LogObject implements Serializable { 31 32 public abstract void writeTo(DataOutput out) throws IOException; 33 34 public abstract Object internalReadFrom(DataInput in) throws IOException; 35 36 public abstract void undoOn(RandomAccessFile raf) throws IOException; 37 38 public abstract void corrupt(RandomAccessFile raf) throws IOException; 39 40 public static Object readFrom(DataInput in) throws IOException { 41 short classCode = in.readShort(); 42 if (classCode == FILELENGTH_SERIAL_CODE) { 43 return logFileLengthModification.internalReadFrom(in); 44 } else if (classCode == RECORD_SERIAL_CODE) { 45 return logRecord.internalReadFrom(in); 46 } else if (classCode == BEGIN_SERIAL_CODE) { 47 return new LogBoundary(true); 48 } else if (classCode == END_SERIAL_CODE) { 49 return new LogBoundary(false); 50 } 51 return null; 52 } 53 54 public static void writeLogRecord(DataOutput out, long address, byte[] before, int newDatasLength) 55 throws IOException { 56 out.writeShort(RECORD_SERIAL_CODE); 57 out.writeLong(address); 58 out.writeInt(newDatasLength); 59 out.writeInt(before.length); 60 out.write(before); 61 } 62 63 public static void writeLogFileLengthModification(DataOutput out, byte[] deletedBytes) 64 throws IOException { 65 out.writeShort(FILELENGTH_SERIAL_CODE); 66 out.writeBoolean(true); 67 out.writeLong(deletedBytes.length); 68 out.write(deletedBytes); 69 } 70 71 public static void writeLogFileLengthModification(DataOutput out, long sizeModification) 72 throws IOException { 73 out.writeShort(FILELENGTH_SERIAL_CODE); 74 out.writeBoolean(false); 75 out.writeLong(sizeModification); 76 } 77 78 public static void writeLogBoundary(DataOutput out, boolean isBegin) 79 throws IOException { 80 if (isBegin) { 81 out.writeShort(BEGIN_SERIAL_CODE); 82 } else { 83 out.writeShort(END_SERIAL_CODE); 84 } 85 } 86 87 private static LogRecord logRecord = new LogRecord(); 88 private static LogFileLengthModification logFileLengthModification = new LogFileLengthModification(); 89 90 91 public static final short RECORD_SERIAL_CODE = 1; 92 public static final short FILELENGTH_SERIAL_CODE = 2; 93 public static final short BEGIN_SERIAL_CODE = 3; 94 public static final short END_SERIAL_CODE = 4; 95 96 static final long serialVersionUID = -7699636669241161460L; 97 } 98 | Popular Tags |