Skip to content

Commit

Permalink
- issue168: allow to save / open binary from / to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
agirbal committed Oct 2, 2013
1 parent 3140c07 commit 957a746
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 18 deletions.
25 changes: 19 additions & 6 deletions resource/xml/umongo.xml
Expand Up @@ -27,13 +27,24 @@
<MenuItem id="set" call="set"/>
<MenuItem id="unset" call="unset"/>
<MenuSeparator/>
<MenuItem id="compressionStats" call="compressionStats" showDialog="false">
<InfoDialog id="compressionStatsDialog" text="/html/about.html"/>
<MenuItem id="compressionStats" call="compressionStats" showDialog="false" buttons="OK">
<FormDialog>
<Text id="compressionStatsOriginal" label="Original" field="true"/>
<Text id="compressionStatsDeflate" label="Deflate" field="true"/>
<Text id="compressionStatsGZip" label="GZip" field="true"/>
</FormDialog>
</MenuItem>
<MenuItem id="saveBinaryToFile" call="saveBinaryToFile">
<FileSelectorField id="saveBinaryOutputFile">
<FileSelectorDialog type="SAVE" mode="FILES_ONLY"/>
</FileSelectorField>
<FormDialog>
<FileSelectorField id="saveBinaryOutputFile">
<FileSelectorDialog type="SAVE" mode="FILES_ONLY"/>
</FileSelectorField>
</FormDialog>
</MenuItem>
<MenuItem id="decodeBinary" call="decodeBinary">
<FormDialog buttons="OK" preferredSize="640;480">
<TextArea id="expandTextArea" field="false" zone="CENTER" editable="false"/>
</FormDialog>
</MenuItem>
<MenuSeparator/>
<MenuItem id="copyField" call="copyField"/>
Expand Down Expand Up @@ -827,7 +838,9 @@
</Zone>
</com.edgytech.umongo.RouterPanel>
<com.edgytech.umongo.EditBinaryDialog>
<IntSpinner id="size"/>
<FileSelectorField id="inputFile">
<FileSelectorDialog type="OPEN" mode="FILES_ONLY"/>
</FileSelectorField>
</com.edgytech.umongo.EditBinaryDialog>
<com.edgytech.umongo.EditBooleanDialog>
<CheckBox id="value" label="True"/>
Expand Down
50 changes: 43 additions & 7 deletions src/com/edgytech/umongo/DocumentFieldMenu.java
Expand Up @@ -24,6 +24,8 @@
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.DeflaterOutputStream;
Expand All @@ -40,9 +42,13 @@ enum Item {
set,
unset,
compressionStats,
compressionStatsDialog,
compressionStatsOriginal,
compressionStatsDeflate,
compressionStatsGZip,
saveBinaryToFile,
saveBinaryOutputFile,
decodeBinary,
decodeBinaryText,
copyField,
copyValue
}
Expand Down Expand Up @@ -186,7 +192,7 @@ public void compressionStats(ButtonBase button) throws IOException {
bytes = MongoUtils.getJSON(value).toString().getBytes(Charset.forName("UTF-8"));
}

String msg = "Initial Size: " + bytes.length + "\n";
setStringFieldValue(Item.compressionStatsOriginal, String.valueOf(bytes.length));
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
// System.out.println("Bytes before: " + bytes.length);

Expand All @@ -195,24 +201,54 @@ public void compressionStats(ButtonBase button) throws IOException {
dos.write(bytes);
dos.close();
byte[] deflate = baos.toByteArray();
msg += "Deflate: " + deflate.length + "\n";
setStringFieldValue(Item.compressionStatsDeflate, String.valueOf(deflate.length));
// System.out.println("Bytes deflate: " + deflate.length);

baos.reset();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(bytes);
gos.close();
byte[] gzip = baos.toByteArray();
msg += "GZip: " + gzip.length + "\n";
setStringFieldValue(Item.compressionStatsGZip, String.valueOf(gzip.length));
// System.out.println("Bytes gzip: " + gzip.length);

InfoDialog dia = (InfoDialog)getBoundComponentUnit(Item.compressionStatsDialog);
dia.text = msg;
Showable dia = ((MenuItem)getBoundComponentUnit(Item.compressionStats)).getDialog();
dia.show();
}

public void saveBinaryToFile(ButtonBase button) throws IOException {
String path = ((FileSelectorField)getBoundComponentUnit(Item.saveBinaryOutputFile)).getPath();

final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit());
TreeNodeDocumentField node = (TreeNodeDocumentField) dv.getSelectedNode().getUserObject();
Object value = node.getValue();
byte[] bytes = null;
if (value instanceof byte[]) {
bytes = (byte[]) value;
} else if (value instanceof Binary) {
bytes = ((Binary)value).getData();
} else {
bytes = MongoUtils.getJSON(value).toString().getBytes(Charset.forName("UTF-8"));
}

FileOutputStream fos = new FileOutputStream(path);
fos.write(bytes);
fos.close();
}


// public void decodeBinary(ButtonBase button) throws IOException {
// final DocView dv = (DocView) (UMongo.instance.getTabbedResult().getSelectedUnit());
// TreeNodeDocumentField node = (TreeNodeDocumentField) dv.getSelectedNode().getUserObject();
// Object value = node.getValue();
// byte[] bytes = null;
// if (value instanceof byte[]) {
// bytes = (byte[]) value;
// } else if (value instanceof Binary) {
// bytes = ((Binary)value).getData();
// } else {
// bytes = MongoUtils.getJSON(value).toString().getBytes(Charset.forName("UTF-8"));
// }
//
// BinaryDecoder
// }
}
30 changes: 25 additions & 5 deletions src/com/edgytech/umongo/EditBinaryDialog.java
Expand Up @@ -16,15 +16,22 @@

package com.edgytech.umongo;

import com.edgytech.swingfast.FileSelectorField;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bson.types.Binary;
import sun.misc.IOUtils;

/**
*
* @author antoine
*/
public class EditBinaryDialog extends EditFieldDialog {
enum Item {
size
inputFile
}

public EditBinaryDialog() {
Expand All @@ -33,13 +40,26 @@ public EditBinaryDialog() {

@Override
public Object getValue() {
int size = getIntFieldValue(Item.size);
return new Binary((byte)0, new byte[size]);
FileInputStream fis = null;
try {
String path = ((FileSelectorField) getBoundComponentUnit(Item.inputFile)).getPath();
fis = new FileInputStream(path);
byte[] bytes = IOUtils.readFully(fis, -1, true);
return new Binary((byte)0, bytes);
} catch (Exception ex) {
getLogger().log(Level.WARNING, null, ex);
} finally {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(EditBinaryDialog.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}

@Override
public void setValue(Object value) {
Binary bin = (Binary) value;
setIntFieldValue(Item.size, bin.length());
// nothing to do here...
}
}

0 comments on commit 957a746

Please sign in to comment.