1 package com.explosion.expfmodules.fileutils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.awt.event.ActionEvent;
23 import java.io.DataInputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.apache.log4j.LogManager;
31 import org.apache.log4j.Logger;
32
33 import com.explosion.expf.Application;
34 import com.explosion.expf.ExpActionListener;
35 import com.explosion.expfmodules.rdbmsconn.dbom.utils.SQLEngine;
36 import com.explosion.utilities.FileSystemUtils;
37
38 /***
39 * @author Stephen Cowx
40 */
41 public class FileUtilsListener implements ExpActionListener
42 {
43 private static Logger log = LogManager.getLogger(SQLEngine.class);
44 private HashMap map;
45 private int newDocumentNumbers = 1;
46
47 /***
48 * Constructor for TextEditorListener.
49 */
50 public FileUtilsListener()
51 {
52 map = new HashMap();
53 map.put(FileUtilsConstants.MENU_FILE_SPLIT,FileUtilsConstants.MENU_FILE_SPLIT);
54 map.put(FileUtilsConstants.MENU_FILE_UNSPLIT,FileUtilsConstants.MENU_FILE_UNSPLIT);
55 }
56
57 /***
58 * @see package com.explosion.expf.Interfaces.ExpActionListener#getListensFor()
59 */
60 public Map getListensFor()
61 {
62 return map;
63 }
64
65 /***
66 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
67 */
68 public void actionPerformed(ActionEvent e)
69 {
70 try
71 {
72 if (e.getActionCommand().equals(FileUtilsConstants.MENU_FILE_SPLIT))
73 {
74 File[] files = FileSystemUtils.chooseFiles(Application.getApplicationFrame(), FileSystemUtils.OPENTYPE, false, new File(System.getProperty("user.dir")), FileSystemUtils.FILES_ONLY, "Select file to split.");
75
76 if (files != null && files.length > 0 && files[0] != null)
77 {
78 FileInputStream fin = new FileInputStream(files[0].getAbsolutePath());
79 DataInputStream din = new DataInputStream(fin);
80 split(files[0].getAbsolutePath(), din);
81 }
82 }
83 else if (e.getActionCommand().equals(FileUtilsConstants.MENU_FILE_UNSPLIT))
84 {
85 File[] files1 = FileSystemUtils.chooseFiles(Application.getApplicationFrame(), FileSystemUtils.OPENTYPE, false, new File(System.getProperty("user.dir")), FileSystemUtils.FILES_ONLY, "Select file1 of two to unsplit.");
86 File[] files2 = FileSystemUtils.chooseFiles(Application.getApplicationFrame(), FileSystemUtils.OPENTYPE, false, new File(System.getProperty("user.dir")), FileSystemUtils.FILES_ONLY, "Select file2 to two to unsplit.");
87
88 if (files1 != null && files1.length > 0 && files1[0] != null
89 && files2 != null && files2.length > 0 && files2[0] != null)
90 {
91 File[] files = new File[2];
92 files[0] = files1[0];
93 files[1] = files2[0];
94 unsplit(files[0].getAbsolutePath() + ".joined", files);
95 }
96 }
97 }
98 catch (Exception ex)
99 {
100 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while responding to event." );
101 }
102
103 }
104
105 /***
106 * Splits files previously unsplit.
107 * @param fileName
108 * @throws Exception
109 */
110 public void split(String fileName, DataInputStream din) throws Exception
111 {
112 File file = new File(fileName);
113 long s = file.length();
114 long segment1Length = 0;
115 long segment2Length = 0;
116 if (s%2 > 0)
117 {
118 segment1Length = (s+1)/2;
119 segment2Length = (s-1)/2;
120 }
121 else
122 {
123 segment1Length = s/2;
124 segment2Length = s/2;
125 }
126
127 FileOutputStream fout1 = new FileOutputStream(fileName+"_1");
128 FileOutputStream fout2 = new FileOutputStream(fileName+"_2");
129 long read = 0;
130 while (din.available() > 0)
131 {
132 if (read < segment1Length)
133 {
134 if (din.available() >= 1024)
135 {
136 byte[] bytes = new byte[1024];
137 din.read(bytes);
138 }
139 fout1.write(din.read());
140 read++;
141 }
142 else
143 {
144 fout2.write(din.read());
145 }
146 }
147
148 fout1.close();
149 fout2.close();
150 din.close();
151 }
152
153 /***
154 * Unsplits files previously split.
155 * @param fileName
156 * @throws Exception
157 */
158 public void unsplit(String newFilename, File[] files) throws Exception
159 {
160 FileOutputStream stream = new FileOutputStream(new File(newFilename));
161 for (int i=0;i<files.length;i++)
162 {
163 FileInputStream fin = new FileInputStream(files[i].getAbsolutePath());
164 DataInputStream din = new DataInputStream(fin);
165 while (din.available() > 0)
166 {
167 stream.write(din.read());
168 }
169 din.close();
170 fin.close();
171 }
172 stream.close();
173 }
174
175
176 }