1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.bootstrap.partition;
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Iterator;
27
28
29
30
31
32
33
34 public class SchemaPartitionExtractor
35 {
36 private DbFileListing listing;
37 private File outputDirectory;
38
39
40 public SchemaPartitionExtractor( File outputDirectory ) throws IOException
41 {
42 this.outputDirectory = outputDirectory;
43 this.listing = new DbFileListing();
44 }
45
46
47 public void extract() throws IOException
48 {
49 if ( ! outputDirectory.exists() )
50 {
51 outputDirectory.mkdirs();
52 }
53
54 File schemaDirectory = new File( outputDirectory, "schema" );
55 if ( ! schemaDirectory.exists() )
56 {
57 schemaDirectory.mkdirs();
58 }
59
60 Iterator<String> ii = listing.iterator();
61
62 while ( ii.hasNext() )
63 {
64 extract( ii.next() );
65 }
66 }
67
68
69 public DbFileListing getDbFileListing()
70 {
71 return listing;
72 }
73
74
75 private void extract( String resource ) throws IOException
76 {
77 byte[] buf = new byte[512];
78 InputStream in = DbFileListing.getUniqueResourceAsStream( resource, "database file in bootstrap partition" );
79
80 try
81 {
82 FileOutputStream out = new FileOutputStream( new File( outputDirectory, resource ) );
83 try
84 {
85 while ( in.available() > 0 )
86 {
87 int readCount = in.read( buf );
88 out.write( buf, 0, readCount );
89 }
90 out.flush();
91 } finally
92 {
93 out.close();
94 }
95 }
96 finally
97 {
98 in.close();
99 }
100 }
101 }