5 |
import java.io.*; |
import java.io.*; |
6 |
import java.util.*; |
import java.util.*; |
7 |
|
|
8 |
|
/** A StormPool storing blocks in individual files in a directory. |
9 |
|
* File names have the form <code>b_</code><i>idstring</i>. |
10 |
|
*/ |
11 |
public class DirPool implements StormPool { |
public class DirPool implements StormPool { |
12 |
|
|
13 |
|
/** The directory we store our blocks in. |
14 |
|
*/ |
15 |
|
File dir; |
16 |
|
|
17 |
|
/** Get the File object for the block |
18 |
|
* corresponding to a given id. |
19 |
|
*/ |
20 |
|
protected File getFile(BlockId id) { |
21 |
|
String hex = gzz.util.HexUtil.byteArrToHex(id.getBytes()); |
22 |
|
return new File(dir, "b_" + hex); |
23 |
|
} |
24 |
|
|
25 |
|
protected class FileBlockOutputStream extends BlockOutputStream { |
26 |
|
protected ByteArrayOutputStream baos; |
27 |
|
protected Header822 header; |
28 |
|
protected Block block; |
29 |
|
|
30 |
|
protected FileBlockOutputStream(Header822 header) throws IOException { |
31 |
|
super(new ByteArrayOutputStream()); |
32 |
|
baos = (ByteArrayOutputStream)out; |
33 |
|
this.header = header; |
34 |
|
header.writeTo(baos); |
35 |
|
} |
36 |
|
public Block getBlock() { return block; } |
37 |
|
public Header822 getHeader() { return null; } |
38 |
|
public void close() throws IOException { |
39 |
|
byte[] bytes = baos.toByteArray(); |
40 |
|
BlockId id = BlockId.getIdForData(bytes); |
41 |
|
File file = getFile(id); |
42 |
|
|
43 |
|
OutputStream os = new FileOutputStream(file); |
44 |
|
os.write(bytes); |
45 |
|
os.close(); |
46 |
|
|
47 |
|
block = new FileBlock(id); |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
|
protected class FileBlock implements Block { |
52 |
|
protected File file; |
53 |
|
|
54 |
|
protected BlockId id; |
55 |
|
|
56 |
|
protected FileBlock(BlockId id) throws IOException { |
57 |
|
this.id = id; |
58 |
|
this.file = getFile(id); |
59 |
|
} |
60 |
|
public BlockId getId() { return id; } |
61 |
|
public StormPool getPool() { return null; } |
62 |
|
public Header822 getHeader() { return null; } |
63 |
|
public InputStream getInputStream() { |
64 |
|
return null; |
65 |
|
} |
66 |
|
public InputStream getRawInputStream() throws IOException { |
67 |
|
return new FileInputStream(file); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
|
/** Create a new DirPool. |
72 |
|
* @param dir The directory blocks are stored in. |
73 |
|
* Must already exist. |
74 |
|
* @throws IllegalArgumentException if the directory |
75 |
|
* does not exist yet. |
76 |
|
*/ |
77 |
|
public DirPool(File dir) { |
78 |
|
this.dir = dir; |
79 |
|
} |
80 |
|
|
81 |
public Block get(BlockId id) { return null; } |
public Block get(BlockId id) { return null; } |
82 |
public void add(Block b) {} |
public void add(Block b) {} |
83 |
public void delete(Block b) {} |
public void delete(Block b) {} |
84 |
public Set getIds() { return null; } |
public Set getIds() { return null; } |
85 |
public BlockOutputStream getBlockOutputStream(String contentType) |
public BlockOutputStream getBlockOutputStream(String contentType) |
86 |
throws IOException { |
throws IOException { |
87 |
return null; |
Header822 hdr = new UniqueHeader822(); |
88 |
|
hdr.add("Content-Type", contentType); |
89 |
|
return new FileBlockOutputStream(hdr); |
90 |
} |
} |
91 |
public BlockOutputStream getBlockOutputStream(Header822 hdr) |
public BlockOutputStream getBlockOutputStream(Header822 hdr) |
92 |
throws IOException { |
throws IOException { |
93 |
return null; |
return new FileBlockOutputStream(new VerbatimHeader822(hdr)); |
94 |
} |
} |
95 |
public String getDefaultPoolName() { return null; } |
public String getDefaultPoolName() { return null; } |
96 |
public Set getPoolNames() { return null; } |
public Set getPoolNames() { return null; } |