-
Notifications
You must be signed in to change notification settings - Fork 3
/
SaveValueAsQuery.java
290 lines (256 loc) · 10.1 KB
/
SaveValueAsQuery.java
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
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG, IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
* Andrew Johnson - progress indicators
*******************************************************************************/
package org.eclipse.mat.ui.snapshot.actions;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.List;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.query.IContextObject;
import org.eclipse.mat.query.IQuery;
import org.eclipse.mat.query.IResult;
import org.eclipse.mat.query.annotations.Argument;
import org.eclipse.mat.query.annotations.HelpUrl;
import org.eclipse.mat.query.annotations.Argument.Advice;
import org.eclipse.mat.query.annotations.Icon;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.IPrimitiveArray;
import org.eclipse.mat.ui.Messages;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
@Icon("/icons/copy.gif")
@HelpUrl("/org.eclipse.mat.ui.help/reference/inspections/copy.html#ref_inspections_copy__savevalueas")
public class SaveValueAsQuery implements IQuery
{
@Argument
public ISnapshot snapshot;
@Argument
public List<IContextObject> objects;
@Argument(advice = Advice.SAVE)
public File file;
@Argument
public Display display;
public IResult execute(IProgressListener listener) throws Exception
{
checkIfFileExists();
if (objects.size() > 1)
writeStringData(listener);
else if (objects.size() == 1)
writeRawData(listener);
// let the UI ignore this query
throw new IProgressListener.OperationCanceledException();
}
private void checkIfFileExists()
{
if (file.exists())
{
try
{
// message box will popup in background...
Thread.sleep(500);
}
catch (InterruptedException ignore)
{}
final boolean[] goAhead = new boolean[1];
display.syncExec(new Runnable()
{
public void run()
{
MessageBox box = new MessageBox(display.getActiveShell(), //
SWT.ICON_WARNING | SWT.YES | SWT.NO);
box.setText(Messages.SaveValueAsQuery_Overwrite);
box.setMessage(MessageUtil.format(Messages.SaveValueAsQuery_FileExists, file.getAbsolutePath()));
int retValue = box.open();
goAhead[0] = retValue == SWT.YES;
}
});
if (!goAhead[0])
throw new IProgressListener.OperationCanceledException();
}
}
private void writeStringData(IProgressListener listener) throws Exception
{
try
{
listener.beginTask(Messages.SaveValueAsQuery_Saving, objects.size());
Charset cs;
try
{
String encoding = ResourcesPlugin.getEncoding();
if (encoding != null)
cs = Charset.forName(encoding);
else
cs = StandardCharsets.UTF_8;
}
catch (UnsupportedCharsetException e)
{
cs = StandardCharsets.UTF_8;
}
try (PrintWriter writer = new PrintWriter(file, cs.name()))
{
boolean isFirst = true;
for (IContextObject object : objects)
{
if (object.getObjectId() < 0)
continue;
if (!isFirst)
writer.append('\n');
IObject subject = snapshot.getObject(object.getObjectId());
ExportInfo info = ExportInfo.of(subject);
if (info == null)
{
String name = subject.getClassSpecificName();
writer.append(name != null ? name : subject.getTechnicalName());
}
else
{
final int length = info.getLength();
final int end = Math.min(info.getOffset() + info.getCount(), length);
int offset = info.getOffset();
while (offset < end)
{
int read = Math.min(4092, end - offset);
char[] array = info.getChars(offset, read);
writer.append(new String(array));
offset += read;
}
}
isFirst = false;
listener.worked(1);
if (listener.isCanceled())
break;
}
writer.flush();
}
}
finally
{
listener.done();
}
}
private void writeRawData(IProgressListener listener) throws Exception
{
IContextObject obj = objects.get(0);
if (obj.getObjectId() < 0)
return;
IObject object = snapshot.getObject(obj.getObjectId());
if (!(object instanceof IPrimitiveArray))
{
writeStringData(listener);
return;
}
IPrimitiveArray array = (IPrimitiveArray) object;
FileOutputStream out = new FileOutputStream(file);
try
{
DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(out));
try
{
int size = array.getLength();
listener.beginTask(Messages.SaveValueAsQuery_SavingBinaryValue, size);
int offset = 0;
while (offset < size)
{
int read = Math.min(4092, size - offset);
Object valueArray = array.getValueArray(offset, read);
switch (array.getType())
{
case IObject.Type.BOOLEAN:
{
boolean[] a = (boolean[]) valueArray;
for (int ii = 0; ii < a.length; ii++)
writer.writeBoolean(a[ii]);
break;
}
case IObject.Type.BYTE:
{
byte[] a = (byte[]) valueArray;
writer.write(a);
break;
}
case IObject.Type.CHAR:
{
char[] a = (char[]) valueArray;
for (int ii = 0; ii < a.length; ii++)
writer.writeChar(a[ii]);
break;
}
case IObject.Type.DOUBLE:
{
double[] a = (double[]) valueArray;
for (int ii = 0; ii < a.length; ii++)
writer.writeDouble(a[ii]);
break;
}
case IObject.Type.FLOAT:
{
float[] a = (float[]) valueArray;
for (int ii = 0; ii < a.length; ii++)
writer.writeFloat(a[ii]);
break;
}
case IObject.Type.INT:
{
int[] a = (int[]) valueArray;
for (int ii = 0; ii < a.length; ii++)
writer.writeInt(a[ii]);
break;
}
case IObject.Type.LONG:
{
long[] a = (long[]) valueArray;
for (int ii = 0; ii < a.length; ii++)
writer.writeLong(a[ii]);
break;
}
case IObject.Type.SHORT:
{
short[] a = (short[]) valueArray;
for (int ii = 0; ii < a.length; ii++)
writer.writeShort(a[ii]);
break;
}
default:
throw new SnapshotException(MessageUtil.format(
Messages.SaveValueAsQuery_UnrecognizedPrimitiveArrayType, array.getType()));
}
offset += read;
listener.worked(1);
if (listener.isCanceled())
break;
}
writer.flush();
}
finally
{
listener.done();
writer.close();
}
}
finally
{
out.close();
}
}
}