Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr15.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2009.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.mdr;
14
15 import java.io.BufferedOutputStream;
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.nio.ByteBuffer;
22 import java.nio.channels.FileChannel;
23 import java.nio.charset.Charset;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import uk.me.parabola.imgfmt.ExitException;
28 import uk.me.parabola.imgfmt.MapFailedException;
29 import uk.me.parabola.imgfmt.Utils;
30 import uk.me.parabola.imgfmt.app.ImgFileWriter;
31
32 /**
33 * The string table. This is not used by the device.
34 *
35 * There is a compressed and non-compressed version of this section.
36 * We are starting with the regular string version.
37 *
38 * @author Steve Ratcliffe
39 */
40 public class Mdr15 extends MdrSection {
41 private final OutputStream stringFile;
42 private int nextOffset;
43
44 private Map<String, Integer> strings = new HashMap<>();
45 private final Charset charset;
46 private final File tempFile;
47
48 public Mdr15(MdrConfig config) {
49 setConfig(config);
50
51 charset = config.getSort().getCharset();
52 if (config.isForDevice()) {
53 tempFile = null;
54 stringFile = null;
55 return;
56 }
57 try {
58 tempFile = File.createTempFile("strings", null, config.getOutputDir());
59 tempFile.deleteOnExit();
60
61 stringFile = new BufferedOutputStream(new FileOutputStream(tempFile), 64 * 1024);
62
63 // reserve the string at offset 0 to be the empty string.
64 stringFile.write(0);
65 nextOffset = 1;
66
67 } catch (IOException e) {
68 throw new ExitException("Could not create temporary file");
69 }
70 }
71
72 public int createString(String str) {
73 Integer offset = strings.get(str);
74 if (offset != null)
75 return offset;
76
77 int off;
78 try {
79 off = nextOffset;
80
81 byte[] bytes = str.getBytes(charset);
82 stringFile.write(bytes);
83 stringFile.write(0);
84
85 // Increase offset for the length of the string and the null byte
86 nextOffset += bytes.length + 1;
87 } catch (IOException e) {
88 // Can't convert, return empty string instead.
89 off = 0;
90 }
91
92 strings.put(str, off);
93 return off;
94 }
95
96 /**
97 * Tidy up after reading files.
98 * Close the temporary file, and release the string table which is no longer
99 * needed.
100 */
101 @Override
102 public void releaseMemory() {
103 strings = null;
104 try {
105 if (stringFile != null) {
106 stringFile.close();
107 }
108 } catch (IOException e) {
109 throw new MapFailedException("Could not close string temporary file");
110 }
111 }
112
113 public void writeSectData(ImgFileWriter writer) {
114 try (FileInputStream stream = new FileInputStream(tempFile)) {
115 FileChannel channel = stream.getChannel();
116 ByteBuffer buf = ByteBuffer.allocate(32 * 1024);
117 while (channel.read(buf) > 0) {
118 buf.flip();
119 writer.put(buf);
120 buf.compact();
121 }
122 } catch (IOException e) {
123 throw new ExitException("Could not write string section of index");
124 }
125 }
126
127 public int getItemSize() {
128 // variable sized records.
129 return 0;
130 }
131
132 /**
133 * The meaning of number of items for this section is the largest string
134 * offset possible. We are taking the total size of the string section
135 * for this.
136 */
137 @Override
138 public int getSizeForRecord() {
139 return Utils.numberToPointerSize(nextOffset);
140 }
141
142 /**
143 * There is no use for this as the records are not fixed length.
144 *
145 * @return Always zero, could return the number of strings.
146 */
147 protected int numberOfItems() {
148 return 0;
149 }
150 }