From d713c7483270aa12459ea48604111626b0551aec Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 1 Apr 2014 19:59:35 +0000 Subject: [PATCH] Added Binary formater for human string output --- src/zutil/StringUtil.java | 58 +++++++++++++++++++- src/zutil/jee/upload/AjaxFileUpload.java | 42 +++++--------- src/zutil/jee/upload/FileUploadListener.java | 7 +-- src/zutil/net/update/Zupdater.java | 30 ++++------ src/zutil/test/StringUtilTest.java | 36 ++++++++++-- 5 files changed, 114 insertions(+), 59 deletions(-) diff --git a/src/zutil/StringUtil.java b/src/zutil/StringUtil.java index f479639..ca50be6 100644 --- a/src/zutil/StringUtil.java +++ b/src/zutil/StringUtil.java @@ -22,6 +22,8 @@ package zutil; +import zutil.converters.Converter; + /** * This is a class whit utility methods. * @@ -33,10 +35,10 @@ public class StringUtil { /** * Present a size (in bytes) as a human-readable value * - * @param size size (in bytes) + * @param bytes size (in bytes) * @return string */ - public static String formatBytesToString(long bytes){ + public static String formatByteSizeToString(long bytes){ int total = sizes.length-1; double value = bytes; @@ -143,7 +145,7 @@ public class StringUtil { /** * Trims the whitespace and quotes if the string starts and ends with one * - * @param str is the string to trim + * @param str is the string to trim * @return */ public static String trimQuotes(String str){ @@ -155,4 +157,54 @@ public class StringUtil { } return str; } + + /** + * Presents a binary array in HEX and ASCII + * + * @param data The source binary data to format + * @return A multiline String with human readable HEX and ASCII + */ + public static String formatBytesToString(byte[] data){ + StringBuffer output = new StringBuffer(); + + //000 XX XX XX XX XX XX XX XX '........' + int maxOffset = (""+data.length).length(); + for(int offset=0; offset * Example web.xml: @@ -240,7 +228,7 @@ public abstract class AjaxFileUpload extends HttpServlet { } else{ files.add( fileItem ); - logger.info("Recieved file: "+fileItem.getName()+" ("+StringUtil.formatBytesToString( fileItem.getSize() )+")"); + logger.info("Recieved file: "+fileItem.getName()+" ("+StringUtil.formatByteSizeToString(fileItem.getSize())+")"); } } // Process the upload diff --git a/src/zutil/jee/upload/FileUploadListener.java b/src/zutil/jee/upload/FileUploadListener.java index e304d2e..3134de8 100644 --- a/src/zutil/jee/upload/FileUploadListener.java +++ b/src/zutil/jee/upload/FileUploadListener.java @@ -23,7 +23,6 @@ package zutil.jee.upload; import org.apache.commons.fileupload.ProgressListener; - import zutil.StringUtil; import zutil.parser.DataNode; import zutil.parser.DataNode.DataType; @@ -152,9 +151,9 @@ public class FileUploadListener implements ProgressListener{ node.set("filename", filename); node.set("percent", getPercentComplete()); - node.set("uploaded", StringUtil.formatBytesToString( bytes )); - node.set("total", StringUtil.formatBytesToString( length )); - node.set("speed", StringUtil.formatBytesToString( speed )+"/s"); + node.set("uploaded", StringUtil.formatByteSizeToString(bytes)); + node.set("total", StringUtil.formatByteSizeToString(length)); + node.set("speed", StringUtil.formatByteSizeToString(speed)+"/s"); return node; } } diff --git a/src/zutil/net/update/Zupdater.java b/src/zutil/net/update/Zupdater.java index c0228f3..e1b75ae 100644 --- a/src/zutil/net/update/Zupdater.java +++ b/src/zutil/net/update/Zupdater.java @@ -22,25 +22,17 @@ package zutil.net.update; -import java.awt.EventQueue; - -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.border.EmptyBorder; -import javax.swing.GroupLayout; -import javax.swing.GroupLayout.Alignment; -import javax.swing.JLabel; -import javax.swing.JProgressBar; -import javax.swing.LayoutStyle.ComponentPlacement; -import javax.swing.SwingConstants; -import javax.swing.JSeparator; -import javax.swing.JButton; - import zutil.ProgressListener; import zutil.StringUtil; -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; + +import javax.swing.*; +import javax.swing.GroupLayout.Alignment; +import javax.swing.LayoutStyle.ComponentPlacement; +import javax.swing.border.EmptyBorder; import java.awt.Dialog.ModalExclusionType; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; public class Zupdater extends JFrame implements ProgressListener{ @@ -71,10 +63,10 @@ public class Zupdater extends JFrame implements ProgressListener