01: package org.apache.lucene.store;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.IOException;
21:
22: /**
23: * Used by MockRAMDirectory to create an output stream that
24: * will throw an IOException on fake disk full, track max
25: * disk space actually used, and maybe throw random
26: * IOExceptions.
27: */
28:
29: public class MockRAMOutputStream extends RAMOutputStream {
30: private MockRAMDirectory dir;
31: private boolean first = true;
32:
33: byte[] singleByte = new byte[1];
34:
35: /** Construct an empty output buffer. */
36: public MockRAMOutputStream(MockRAMDirectory dir, RAMFile f) {
37: super (f);
38: this .dir = dir;
39: }
40:
41: public void close() throws IOException {
42: super .close();
43:
44: // Now compute actual disk usage & track the maxUsedSize
45: // in the MockRAMDirectory:
46: long size = dir.getRecomputedActualSizeInBytes();
47: if (size > dir.maxUsedSize) {
48: dir.maxUsedSize = size;
49: }
50: }
51:
52: public void flush() throws IOException {
53: dir.maybeThrowDeterministicException();
54: super .flush();
55: }
56:
57: public void writeByte(byte b) throws IOException {
58: singleByte[0] = b;
59: writeBytes(singleByte, 0, 1);
60: }
61:
62: public void writeBytes(byte[] b, int offset, int len)
63: throws IOException {
64: long freeSpace = dir.maxSize - dir.sizeInBytes();
65: long realUsage = 0;
66:
67: // Enforce disk full:
68: if (dir.maxSize != 0 && freeSpace <= len) {
69: // Compute the real disk free. This will greatly slow
70: // down our test but makes it more accurate:
71: realUsage = dir.getRecomputedActualSizeInBytes();
72: freeSpace = dir.maxSize - realUsage;
73: }
74:
75: if (dir.maxSize != 0 && freeSpace <= len) {
76: if (freeSpace > 0 && freeSpace < len) {
77: realUsage += freeSpace;
78: super .writeBytes(b, offset, (int) freeSpace);
79: }
80: if (realUsage > dir.maxUsedSize) {
81: dir.maxUsedSize = realUsage;
82: }
83: throw new IOException("fake disk full at "
84: + dir.getRecomputedActualSizeInBytes() + " bytes");
85: } else {
86: super .writeBytes(b, offset, len);
87: }
88:
89: dir.maybeThrowDeterministicException();
90:
91: if (first) {
92: // Maybe throw random exception; only do this on first
93: // write to a new file:
94: first = false;
95: dir.maybeThrowIOException();
96: }
97: }
98: }
|