Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
299 lines (267 loc) · 9.37 KB

File metadata and controls

299 lines (267 loc) · 9.37 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package PamController;
import java.awt.Window;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import PamUtils.PamCalendar;
import PamUtils.PamFileChooser;
import PamUtils.PamFileFilter;
import PamView.dialog.warn.WarnOnce;
import binaryFileStorage.BinaryFooter;
import binaryFileStorage.BinaryHeader;
import binaryFileStorage.BinaryStore;
import binaryFileStorage.ModuleNameObject;
/**
* Read and write psfx type configuration files.
* @author dg50
*
*/
public class PSFXReadWriter {
private static PSFXReadWriter singleInstance = null;
private static final String SETTINGSSTORE = "SettingsStore";
// private static final String TEMPNAME = "_tmp";
private static final String TEMPEXT = ".tmp";
private PSFXReadWriter() {
// TODO Auto-generated constructor stub
}
public static PSFXReadWriter getInstance() {
if (singleInstance == null) {
singleInstance = new PSFXReadWriter();
}
return singleInstance;
}
/**
* Save settings to psfx file. Note that this method first saves to a temp file
* (using the filename passed with 'tmp' appended to the end) and then renames to
* the correct filename at the end. This way, if something happens and the file
* becomes corrupted during the save, the original will still be safe
*
* @param fileName the name to use for the save file (full name, including path)
* @return true if successful, false otherwise
*/
public synchronized boolean writePSFX(String fileName) {
// return writePSFX(fileName, PamCalendar.getTimeInMillis());
String tempName = generateTempFilename(fileName);
boolean success = writePSFX(tempName, PamCalendar.getTimeInMillis());
if (success) {
File origFile = new File(fileName);
origFile.delete();
File tempFile = new File(tempName);
success = tempFile.renameTo(origFile);
}
return success;
}
/**
* Generate a temporary filename to save the settings to.
* @param fileName The original filename, with full path and including .psfx extension
* @return a String with the temporary filename
*/
private String generateTempFilename(String fileName) {
// int idx = fileName.lastIndexOf(".");
// String tempName = fileName.substring(0, idx) + TEMPNAME + fileName.substring(idx, fileName.length());
String tempName = fileName + TEMPEXT;
return tempName;
}
/**
* Write the settings to the file<br>
* Initially write to a temp file, then rename the temp file.
* @param fileName the name to use for the save file (full name, including path)
* @param timeStamp time stamp in milliseconds
* @return true on success
*/
public synchronized boolean writePSFX(String fileName, long timeStamp) {
// get an object containing everything we'll need to know.
PamSettingsGroup psg = PamSettingManager.getInstance().getCurrentSettingsGroup();
// force the time stamp to be that given - might need to be exact !
psg.setSettingsTime(timeStamp);
return writePSFX(fileName, psg);
}
/**
* Write settings to the given file<br>
* Initially write to a temp file, then rename the temp file.
* @param fileName the name to use for the save file (full name, including path)
* @param psg PAMGuard settings group
* @return true on success
*/
public synchronized boolean writePSFX(String fileName, PamSettingsGroup psg) {
BinaryHeader header = new BinaryHeader(SETTINGSSTORE, SETTINGSSTORE, SETTINGSSTORE, 0);
long timeStamp = psg.getSettingsTime();
header.setAnalysisDate(timeStamp);
header.setDataDate(timeStamp);
BinaryFooter footer = new BinaryFooter(timeStamp, timeStamp, 2, BinaryStore.getCurrentFileFormat());
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
header.writeHeader(dos);
// write out a list of modules.
ArrayList<UsedModuleInfo> umiList = psg.getUsedModuleInfo();
UsedModuleInfo umi;
ModuleNameObject moduleNameObject;
int nMods;
if (umiList != null) {
nMods = umiList.size();
for (int i = 0; i < nMods; i++) {
umi = umiList.get(i);
moduleNameObject = new ModuleNameObject(umi.className, umi.getUnitType(), umi.unitName);
writeData(dos, ModuleNameObject.typeId, moduleNameObject.createBinaryWriteObject());
}
}
// now write out the serialized settings.
nMods = psg.getUnitSettings().size();
PamControlledUnitSettings pcsu;
for (int i = 0; i < nMods; i++) {
pcsu = psg.getUnitSettings(i);
writeData(dos, 2, pcsu.getNamedSerialisedByteArray());
}
footer.writeFooter(dos, BinaryStore.getCurrentFileFormat());
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
/**
* Select a psfx file.
* @param toRead if true, only selects existing files with the open dialog.
* @return File or null.
*/
public File selectSettingsFile(Window frame, boolean toRead) {
File start = new File(PamFolders.getDefaultProjectFolder());
JFileChooser jFileChooser = new PamFileChooser(start);
// jFileChooser.setFileFilter(new SettingsFileFilter());
jFileChooser.setApproveButtonText("Select");
PamFileFilter fileFilter = new PamFileFilter("PAMGUARD Settings files", PamSettingManager.getCurrentSettingsFileEnd());
jFileChooser.setFileFilter(fileFilter);
jFileChooser.setAcceptAllFileFilterUsed(false);
// jFileChooser.setFileFilter(new FileNameExtensionFilter("PAMGUARD Settings files", defaultFile));
int state = 0;
if (toRead) {
jFileChooser.showOpenDialog(frame);
}
else {
jFileChooser.showSaveDialog(frame);
}
if (state != JFileChooser.APPROVE_OPTION) return null;
File newFile = jFileChooser.getSelectedFile();
newFile = PamFileFilter.checkFileEnd(newFile, PamSettingManager.getCurrentSettingsFileEnd(), true);
return newFile;
}
private boolean writeData(DataOutputStream dos, int objectId, byte[] data) {
int totalLen = data.length + 16;
int dataLen = data.length;
try {
dos.writeInt(totalLen);
dos.writeInt(objectId);
dos.writeInt(dataLen);
dos.write(data);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public PamSettingsGroup loadFileSettings(File file) {
if (file == null) {
return null;
}
if (!file.exists()) {
return null;
}
// check for the existence of a temp file, and warn the user if one is found
String tempName = generateTempFilename(file.getAbsolutePath());
File tempFile = new File(tempName);
if (tempFile.exists()) {
String title = "Potential Problem with Settings File";
String msg = "Pamguard has found the following temporary settings file:<br><br>" +
tempFile.getAbsolutePath() + "<br><br>" +
"Temporary files are created when the settings are saved, but are deleted if the save is "+
"successful. Since this settings file still exists, <em>there may have been a problem during the " +
"last save</em>. Please check your settings after Pamguard starts to make sure they are correct.<p>" +
"<br>Note that you can also try to load the temporary file, in order to recover settings that may have " +
"been lost. If you want to do this, you should rename it now or else it may be overwritten when " +
"Pamguard next shuts down. Keep in mind that the temporary file may be corrupt, depending on what " +
"kind of error occurred during the previous save.<br>";
String help = null;
/**
* Only open dialog warning if NOT under Network managed control so that it doesn't
* occurr when running under PAMDog control.
*/
if (pamBuoyGlobals.getNetworkControlPort() == null) {
int ans = WarnOnce.showWarning(PamController.getMainFrame(), title, msg, WarnOnce.WARNING_MESSAGE, help);
}
else {
/*
* PAMDog control - just print the message. The psf won't have changed in any case.
*/
System.out.println(title + " : " + msg);
}
}
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
BinaryHeader bh = new BinaryHeader();
bh.readHeader(dis);
PamSettingsGroup psg = new PamSettingsGroup(bh.getDataDate());
ArrayList<ModuleNameObject> moduleNames = new ArrayList<ModuleNameObject>();
/*
*
dos.writeInt(totalLen);
dos.writeInt(objectId);
dos.writeInt(dataLen);
*/
int totalLen;
int objectId;
int dataLen;
byte[] data = null;
while(true) {
try {
totalLen = dis.readInt();
objectId = dis.readInt();
dataLen = dis.readInt();
if (dataLen < 0) {
System.out.println("Error reading psfx file " + file.getAbsolutePath());
break;
}
data = new byte[dataLen];
dis.read(data);
if (objectId == ModuleNameObject.typeId) {
ModuleNameObject mno = new ModuleNameObject(data);
moduleNames.add(mno);
}
else if (objectId == 2) {
PamControlledUnitSettings pcsu = PamControlledUnitSettings.createFromNamedByteArray(data);
if (pcsu != null) {
psg.addSettings(pcsu);
}
}
} catch (EOFException e) {
break;
} catch (IOException e) {
e.printStackTrace();
}
}
// close the input stream and return
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
return psg;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.