Project

General

Profile

GenDumpFiles.java

Sergey Ivanovskiy, 11/06/2019 06:24 AM

Download (3.73 KB)

 
1
package test;
2

    
3
import java.io.*;
4
import java.nio.charset.*;
5
import java.util.*;
6

    
7

    
8
public class GenDumpFiles
9
{
10
   private final static String UTF_8  = "UTF-8";
11
   private final static String KOI8_R = "KOI8-R";
12
   
13
   private final static String WINDOWS_1251 = "WINDOWS-1251";
14
   
15
   private final static String ISO8859_1 = "ISO-8859-1";
16
   private final static String ISO8859_5 = "ISO-8859-5";
17
   
18
   private final static String IBM866  = "IBM866";
19
   private final static String USASCII = "US-ASCII";
20
   
21
   private final static String TEMPLATE_PATH = "dump_file_template.txt";
22
   
23
   private final String VALUE = "КОШКА";
24
   
25
   private String[] encodings = new String[] {UTF_8,
26
                                              KOI8_R,
27
                                              WINDOWS_1251,
28
                                              ISO8859_1,
29
                                              ISO8859_5,
30
                                              IBM866,
31
                                              USASCII};
32
   
33
   private Map<String, Charset> charsets = new HashMap<>(encodings.length);
34
   
35
   private Map<Charset, byte[]> valuesMap = new HashMap<>(encodings.length);
36
   
37
   private List<String> lines;
38
   
39
   private String dumpFileName;
40
   
41
   public GenDumpFiles()
42
   {
43
      for(String encoding : encodings)
44
      {
45
         Charset charset = Charset.forName(encoding);
46
         charsets.put(encoding, charset);
47
         valuesMap.put(charset, VALUE.getBytes(charset));
48
      }
49
      
50
      lines = readTemplate(TEMPLATE_PATH, charsets.get(USASCII));
51
      dumpFileName = lines.get(3).substring("filename=".length());
52
   }
53
   
54
   private List<String> readTemplate(String filePath, Charset charset)
55
   {
56
      List<String> lines = new ArrayList<>(13);
57
      
58
      String line;
59
      try(BufferedReader reader = new BufferedReader(
60
               new InputStreamReader(new FileInputStream(filePath), charset));)
61
      {
62
         
63
         while((line = reader.readLine()) != null)
64
         {
65
            lines.add(line);
66
         }
67
      }
68
      catch (IOException e)
69
      {
70
         e.printStackTrace();
71
      }
72
      
73
      return lines;
74
   }
75

    
76
   public void buildDumpFiles()
77
   {
78
      for(String encoding : encodings)
79
      {
80
         StringBuilder dumpFilePath = new StringBuilder(dumpFileName);
81
         dumpFilePath.append("_").append(encoding.replace("-", "_")).append(".d");
82
         buildDumpFile(dumpFilePath.toString(), encoding);
83
      }
84
   }
85
   
86
   private void buildDumpFile(String filePath, String encoding)
87
   {
88
      Charset charset = charsets.get(encoding);
89
      try (ByteArrayOutputStream out = new ByteArrayOutputStream(256);
90
           FileOutputStream fout = new FileOutputStream(filePath);
91
           BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, charset));)
92
      {
93
         writer.append(lines.get(0)).append('"')
94
         .append(new String(valuesMap.get(charset), charset)).append('"');
95
         writer.newLine();
96
         writer.append(lines.get(1));
97
         writer.newLine();
98
         writer.flush();
99
         int offset = out.size();
100
         for (int i = 2; i < 10; i++)
101
         {
102
            writer.append(lines.get(i));
103
            writer.newLine();
104
         }
105
         writer.append(lines.get(10)).append(encoding);
106
         writer.newLine();
107
         writer.append(lines.get(11));
108
         writer.newLine();
109
         
110
         writer.append(String.format("%1$010d", offset));
111
         writer.newLine();
112
         
113
         writer.flush();
114
         
115
         fout.write(out.toByteArray());
116
         fout.flush();
117
      }
118
      catch(IOException e)
119
      {
120
         e.printStackTrace();
121
      }
122
      
123
   }
124
   
125
   public static void main(String[] args)
126
   {
127
      GenDumpFiles gdf = new GenDumpFiles();
128
      gdf.buildDumpFiles();
129
   }
130

    
131
}