KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > storage > raf > log > backup > LogFileLengthModification


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc 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 JavaDoc(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 JavaDoc toString() {
89         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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