File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\fs\FilePathRec.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store.fs;
8
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.nio.ByteBuffer;
12 import java.nio.channels.FileChannel;
13 import java.nio.channels.FileLock;
14
15 /**
16 * A file system that records all write operations and can re-play them.
17 */
18 public class FilePathRec extends FilePathWrapper {
19
20 private static final FilePathRec INSTANCE = new FilePathRec();
21
22 private static Recorder recorder;
23
24 private boolean trace;
25
26 /**
27 * Register the file system.
28 */
29 public static void register() {
30 FilePath.register(INSTANCE);
31 }
32
33 /**
34 * Set the recorder class.
35 *
36 * @param recorder the recorder
37 */
38 public static void setRecorder(Recorder recorder) {
39 FilePathRec.recorder = recorder;
40 }
41
42 public boolean createFile() {
43 log(Recorder.CREATE_NEW_FILE, name);
44 return super.createFile();
45 }
46
47 public FilePath createTempFile(String suffix, boolean deleteOnExit, boolean inTempDir)
48 throws IOException {
49 log(Recorder.CREATE_TEMP_FILE, unwrap(name) + ":" + suffix + ":" + deleteOnExit + ":" + inTempDir);
50 return super.createTempFile(suffix, deleteOnExit, inTempDir);
51 }
52
53 public void delete() {
54 log(Recorder.DELETE, name);
55 super.delete();
56 }
57
58 public FileChannel open(String mode) throws IOException {
59 return new FileRec(this, super.open(mode), name);
60 }
61
62 public OutputStream newOutputStream(boolean append) {
63 log(Recorder.OPEN_OUTPUT_STREAM, name);
64 return super.newOutputStream(append);
65 }
66
67 public void moveTo(FilePath newPath) {
68 log(Recorder.RENAME, unwrap(name) + ":" + unwrap(newPath.name));
69 super.moveTo(newPath);
70 }
71
72 public boolean isTrace() {
73 return trace;
74 }
75
76 public void setTrace(boolean trace) {
77 this.trace = trace;
78 }
79
80 /**
81 * Log the operation.
82 *
83 * @param op the operation
84 * @param fileName the file name(s)
85 */
86 void log(int op, String fileName) {
87 log(op, fileName, null, 0);
88 }
89
90 /**
91 * Log the operation.
92 *
93 * @param op the operation
94 * @param fileName the file name
95 * @param data the data or null
96 * @param x the value or 0
97 */
98 void log(int op, String fileName, byte[] data, long x) {
99 if (recorder != null) {
100 recorder.log(op, fileName, data, x);
101 }
102 }
103
104 /**
105 * Get the prefix for this file system.
106 *
107 * @return the prefix
108 */
109 public String getScheme() {
110 return "rec";
111 }
112
113 }
114
115 /**
116 * A file object that records all write operations and can re-play them.
117 */
118 class FileRec extends FileBase {
119
120 private final FilePathRec rec;
121 private final FileChannel channel;
122 private final String name;
123
124 FileRec(FilePathRec rec, FileChannel file, String fileName) {
125 this.rec = rec;
126 this.channel = file;
127 this.name = fileName;
128 }
129
130 public void implCloseChannel() throws IOException {
131 channel.close();
132 }
133
134 public long position() throws IOException {
135 return channel.position();
136 }
137
138 public long size() throws IOException {
139 return channel.size();
140 }
141
142 public int read(ByteBuffer dst) throws IOException {
143 return channel.read(dst);
144 }
145
146 public FileChannel position(long pos) throws IOException {
147 channel.position(pos);
148 return this;
149 }
150
151 public FileChannel truncate(long newLength) throws IOException {
152 rec.log(Recorder.TRUNCATE, name, null, newLength);
153 channel.truncate(newLength);
154 return this;
155 }
156
157 public void force(boolean metaData) throws IOException {
158 channel.force(metaData);
159 }
160
161 public int write(ByteBuffer src) throws IOException {
162 byte[] buff = src.array();
163 int len = src.remaining();
164 if (src.position() != 0 || len != buff.length) {
165 byte[] b = new byte[len];
166 System.arraycopy(buff, src.position(), b, 0, len);
167 buff = b;
168 }
169 int result = channel.write(src);
170 rec.log(Recorder.WRITE, name, buff, channel.position());
171 return result;
172 }
173
174 public synchronized FileLock tryLock(long position, long size, boolean shared) throws IOException {
175 return channel.tryLock(position, size, shared);
176 }
177
178 }
179