Refactor of glum.zio package.

This commit is contained in:
Norberto Lopez
2017-04-28 12:59:34 +00:00
parent f7b14d6ed8
commit ed4a00ab8b
41 changed files with 735 additions and 497 deletions

View File

@@ -1,8 +1,6 @@
package glum.filter;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import java.io.IOException;
import java.util.List;
@@ -13,7 +11,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public abstract class EnumFilter<G1, G2 extends Enum<?>> implements ZioRaw, Filter<G1>
public abstract class EnumFilter<G1, G2 extends Enum<?>> implements ZioObj, Filter<G1>
{
// Static config vars
private Map<Integer, Enum<?>> fullMap;
@@ -75,7 +73,7 @@ public abstract class EnumFilter<G1, G2 extends Enum<?>> implements ZioRaw, Filt
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
int numItems;
@@ -91,7 +89,7 @@ public abstract class EnumFilter<G1, G2 extends Enum<?>> implements ZioRaw, Filt
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
int numItems;

View File

@@ -1,8 +1,8 @@
package glum.filter;
import glum.zio.raw.ZioRaw;
import glum.zio.ZioObj;
public interface Filter<G1> extends ZioRaw
public interface Filter<G1> extends ZioObj
{
/**
* Method that returns true if aItem passes this filter

View File

@@ -18,13 +18,13 @@ public class NullFilter<G1> implements Filter<G1>
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
; // Nothing to do
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
; // Nothing to do
}

View File

@@ -1,8 +1,6 @@
package glum.filter;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import java.io.IOException;
@@ -11,7 +9,7 @@ import java.io.IOException;
* is the isValid() method and to call the appropriate Constructor. In the isValid() method, you should delegate filter
* logic to the method testIsValid() with the quantity of interest, and return the result from the method call.
*/
public abstract class RangeFilter<G1> implements ZioRaw, Filter<G1>
public abstract class RangeFilter<G1> implements ZioObj, Filter<G1>
{
private boolean isEnabled;
private boolean useMin, useMax;
@@ -61,7 +59,7 @@ public abstract class RangeFilter<G1> implements ZioRaw, Filter<G1>
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
byte bSwitch;
@@ -80,7 +78,7 @@ public abstract class RangeFilter<G1> implements ZioRaw, Filter<G1>
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
byte bSwitch;

View File

@@ -1,12 +1,10 @@
package glum.gui.info;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import java.io.IOException;
public class FilePathInfo implements ZioRaw
public class FilePathInfo implements ZioObj
{
// State vars
protected String filePath;
@@ -22,7 +20,7 @@ public class FilePathInfo implements ZioRaw
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
aStream.readVersion(0);
@@ -30,7 +28,7 @@ public class FilePathInfo implements ZioRaw
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
aStream.writeVersion(0);

View File

@@ -1,14 +1,12 @@
package glum.gui.info;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import glum.zio.util.ZioUtil;
import java.awt.*;
import java.io.*;
public class WindowInfo implements ZioRaw
public class WindowInfo implements ZioObj
{
// Raw vars
protected Point position;
@@ -53,7 +51,7 @@ public class WindowInfo implements ZioRaw
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
aStream.readVersion(0);
@@ -65,7 +63,7 @@ public class WindowInfo implements ZioRaw
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
aStream.writeVersion(0);

View File

@@ -2,9 +2,7 @@ package glum.gui.panel;
import glum.gui.GuiUtil;
import glum.gui.panel.CustomFocusTraversalPolicy;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
@@ -12,6 +10,7 @@ import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Set;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
@@ -19,7 +18,7 @@ import net.miginfocom.swing.MigLayout;
import com.google.common.collect.Sets;
public class GlassPane extends JComponent implements ZioRaw, ComponentListener
public class GlassPane extends JComponent implements ZioObj, ComponentListener
{
// Communicator vars
protected Component parentComp;
@@ -148,15 +147,15 @@ public class GlassPane extends JComponent implements ZioRaw, ComponentListener
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
childComp.zioReadRaw(aStream);
childComp.zioRead(aStream);
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
childComp.zioWriteRaw(aStream);
childComp.zioWrite(aStream);
}
/**

View File

@@ -1,15 +1,13 @@
package glum.gui.panel;
import glum.gui.GuiUtil;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import java.awt.Component;
import java.awt.Dimension;
import java.io.IOException;
public abstract class GlassPanel extends GPanel implements ZioRaw
public abstract class GlassPanel extends GPanel implements ZioObj
{
// State vars
protected Component myGlassPane;
@@ -91,21 +89,21 @@ public abstract class GlassPanel extends GPanel implements ZioRaw
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
aStream.readVersion(0);
if (myGlassPane instanceof ZioRaw)
((ZioRaw)myGlassPane).zioReadRaw(aStream);
if (myGlassPane instanceof ZioObj)
((ZioObj)myGlassPane).zioRead(aStream);
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
aStream.writeVersion(0);
if (myGlassPane instanceof ZioRaw)
((ZioRaw)myGlassPane).zioWriteRaw(aStream);
if (myGlassPane instanceof ZioObj)
((ZioObj)myGlassPane).zioWrite(aStream);
}
}

View File

@@ -2,9 +2,7 @@ package glum.gui.panel;
import glum.gui.panel.nub.HorizontalNub;
import glum.util.MathUtil;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import glum.zio.util.ZioUtil;
import java.awt.Component;
@@ -27,7 +25,7 @@ import net.miginfocom.swing.MigLayout;
* rather than delegating to the Nub classes. The current implementation is slightly inefficient, but probably more
* flexible as it allows other custom Nubs to be swapped in.
*/
public class WaftPanel extends JComponent implements ZioRaw, ComponentListener
public class WaftPanel extends JComponent implements ZioObj, ComponentListener
{
// Gui vars
protected Component childComp;
@@ -198,7 +196,7 @@ public class WaftPanel extends JComponent implements ZioRaw, ComponentListener
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
aStream.readVersion(0);
@@ -207,7 +205,7 @@ public class WaftPanel extends JComponent implements ZioRaw, ComponentListener
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
aStream.writeVersion(0);

View File

@@ -150,23 +150,23 @@ public class LocationPanel extends GlassPanel implements ActionListener, Generic
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
super.zioReadRaw(aStream);
super.zioRead(aStream);
aStream.readVersion(0);
loaderInfo.zioReadRaw(aStream);
loaderInfo.zioRead(aStream);
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
super.zioWriteRaw(aStream);
super.zioWrite(aStream);
aStream.writeVersion(0);
loaderInfo.zioWriteRaw(aStream);
loaderInfo.zioWrite(aStream);
}
/**

View File

@@ -6,10 +6,7 @@ import glum.gui.panel.itemList.query.QueryTableCellRenderer;
import glum.unit.Unit;
import glum.unit.UnitListener;
import glum.unit.UnitProvider;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.raw.ZioRawUtil;
import glum.zio.*;
import java.io.IOException;
import java.util.ArrayList;
@@ -25,7 +22,7 @@ import javax.swing.table.TableColumnModel;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public abstract class BasicItemHandler<G1> implements ZioRaw, ItemHandler<G1>, UnitListener
public abstract class BasicItemHandler<G1> implements ZioObj, ItemHandler<G1>, UnitListener
{
protected JTable myOwner;
protected ArrayList<QueryAttribute> fullAttributeList;
@@ -71,7 +68,7 @@ public abstract class BasicItemHandler<G1> implements ZioRaw, ItemHandler<G1>, U
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
ArrayList<QueryAttribute> newSortedList;
int numItems, index;
@@ -80,7 +77,7 @@ public abstract class BasicItemHandler<G1> implements ZioRaw, ItemHandler<G1>, U
aStream.readVersion(0);
// Payload
ZioRawUtil.readRawList(aStream, fullAttributeList);
ZioObjUtil.readList(aStream, fullAttributeList);
// Reorder the sortedAttributeList based on the serialization
numItems = aStream.readInt();
@@ -99,7 +96,7 @@ public abstract class BasicItemHandler<G1> implements ZioRaw, ItemHandler<G1>, U
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
int numItems;
@@ -107,7 +104,7 @@ public abstract class BasicItemHandler<G1> implements ZioRaw, ItemHandler<G1>, U
aStream.writeVersion(0);
// Payload
ZioRawUtil.writeRawList(aStream, fullAttributeList);
ZioObjUtil.writeList(aStream, fullAttributeList);
// Output the order of the sortedAttributeList
numItems = sortedAttributeList.size();

View File

@@ -4,6 +4,7 @@ import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionEvent;
@@ -26,13 +27,10 @@ import glum.gui.panel.itemList.ItemHandler;
import glum.gui.panel.itemList.ItemListPanel;
import glum.gui.panel.itemList.StaticItemProcessor;
import glum.gui.panel.itemList.query.*;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import net.miginfocom.swing.MigLayout;
public class EditTablePanel extends GlassPanel implements ActionListener, ZioRaw, ListSelectionListener
public class EditTablePanel extends GlassPanel implements ActionListener, ZioObj, ListSelectionListener
{
// GUI vars
protected JLabel titleL;
@@ -189,13 +187,13 @@ public class EditTablePanel extends GlassPanel implements ActionListener, ZioRaw
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
ProfileConfig aProfile;
int numItems, profileIndex;
boolean aBool;
super.zioReadRaw(aStream);
super.zioRead(aStream);
aStream.readVersion(0);
@@ -208,7 +206,7 @@ public class EditTablePanel extends GlassPanel implements ActionListener, ZioRaw
for (int c1 = 0; c1 < numItems; c1++)
{
aProfile = new ProfileConfig("unnamed", myItemProcessor.getItems());
aProfile.zioReadRaw(aStream);
aProfile.zioRead(aStream);
profileBox.addItem(aProfile);
}
@@ -219,18 +217,18 @@ public class EditTablePanel extends GlassPanel implements ActionListener, ZioRaw
profileBox.setSelectedIndex(profileIndex);
profileBox.addActionListener(this);
refItemHandler.zioReadRaw(aStream);
refItemHandler.zioRead(aStream);
updateGui();
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
int numItems, profileIndex;
boolean aBool;
super.zioWriteRaw(aStream);
super.zioWrite(aStream);
aStream.writeVersion(0);
@@ -242,13 +240,13 @@ public class EditTablePanel extends GlassPanel implements ActionListener, ZioRaw
for (ProfileConfig aProfile : profileBox.getAllItems())
{
aProfile.zioWriteRaw(aStream);
aProfile.zioWrite(aStream);
}
profileIndex = profileBox.getSelectedIndex();
aStream.writeInt(profileIndex);
refItemHandler.zioWriteRaw(aStream);
refItemHandler.zioWrite(aStream);
}
@Override

View File

@@ -2,9 +2,7 @@ package glum.gui.panel.itemList.config;
import glum.gui.panel.itemList.query.QueryAttribute;
import glum.gui.panel.itemList.query.QueryComposer;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import java.io.IOException;
import java.util.*;
@@ -18,7 +16,7 @@ import com.google.common.collect.Maps;
* of ItemListPanel and associated table.
*
*/
public class ProfileConfig implements ZioRaw
public class ProfileConfig implements ZioObj
{
// State vars
protected String myName;
@@ -93,7 +91,7 @@ public class ProfileConfig implements ZioRaw
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
Map<Integer, QueryAttribute> newMap;
QueryAttribute queryAttr;
@@ -113,7 +111,7 @@ public class ProfileConfig implements ZioRaw
modelId = aStream.readInt();
queryAttr = itemMap.get(modelId);
queryAttr.zioReadRaw(aStream);
queryAttr.zioRead(aStream);
newMap.put(modelId, queryAttr);
}
@@ -122,7 +120,7 @@ public class ProfileConfig implements ZioRaw
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
QueryAttribute queryAttr;
int numItems;
@@ -139,7 +137,7 @@ public class ProfileConfig implements ZioRaw
queryAttr = itemMap.get(aModelId);
aStream.writeInt(aModelId);
queryAttr.zioWriteRaw(aStream);
queryAttr.zioWrite(aStream);
}
}

View File

@@ -7,11 +7,9 @@ import javax.swing.table.*;
import glum.unit.EmptyUnitProvider;
import glum.unit.UnitProvider;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
public class QueryAttribute implements ZioRaw
public class QueryAttribute implements ZioObj
{
// State vars
public final int modelIndex;
@@ -117,7 +115,7 @@ System.out.println("Are we ready for this ???");
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
aStream.readVersion(0);
@@ -131,7 +129,7 @@ System.out.println("Are we ready for this ???");
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
// Synchronize the attribute before serialization
synchronizeAttribute();

View File

@@ -1,27 +1,31 @@
package glum.io;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.raw.ZioRaw;
import glum.zio.*;
import glum.zio.util.ZioUtil;
import java.awt.Dimension;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.swing.JFileChooser;
import javax.swing.plaf.FileChooserUI;
public class LoaderInfo implements ZioRaw
/**
* Mutable object that contains the details needed to display a load or save GUI component. This object will typically be changed whenever the component is
* modified.
*/
public class LoaderInfo implements ZioObj
{
// Gui vars
private boolean isVisible;
private Point position;
private Dimension dimension;
// Path vars
private String filePath;
public LoaderInfo(File aFilePath)
{
isVisible = false;
@@ -45,18 +49,63 @@ public class LoaderInfo implements ZioRaw
{
if (position != null)
aFileChooser.setLocation(position);
if (dimension != null)
{
aFileChooser.setPreferredSize(dimension);
aFileChooser.setSize(dimension);
}
// Not utilized as this is the incorrect way to load up the standard JFileChooser
// aFileChooser.setVisible(isVisible);
if (filePath != null)
aFileChooser.setCurrentDirectory(new File(filePath));
// Bail if there is no filePath
if (filePath == null)
return;
if (aFileChooser.getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY)
{
File tmpFile = new File(filePath);
// Locate the first folder that exists
// File workPath = tmpFile.getParentFile();
File workPath = tmpFile;
while (workPath != null && workPath.isDirectory() == false)
workPath = workPath.getParentFile();
// Set the FileChooser to the first folder that exists
aFileChooser.setCurrentDirectory(workPath);
// Set the FileChooser's name area to reflect the absolute path
if (workPath != null)
{
String absPathStr = tmpFile.getAbsolutePath();
try
{
FileChooserUI tmpFCUI = aFileChooser.getUI();
Class<? extends FileChooserUI> fcClass = tmpFCUI.getClass();
Method setFileName = fcClass.getMethod("setFileName", String.class);
setFileName.invoke(tmpFCUI, absPathStr);
}
catch(Exception aExp)
{
aExp.printStackTrace();
}
}
}
else
{
File tmpPath = new File(filePath);
if (tmpPath.isFile() == true)
{
aFileChooser.setSelectedFile(tmpPath);
}
else
{
// Set the FileChooser's current directory to the first folder that exists
while (tmpPath != null && tmpPath.getParentFile().isDirectory() == false)
tmpPath = tmpPath.getParentFile();
aFileChooser.setCurrentDirectory(tmpPath);
}
}
}
/**
@@ -67,34 +116,41 @@ public class LoaderInfo implements ZioRaw
position = aFileChooser.getLocation();
dimension = aFileChooser.getSize();
isVisible = aFileChooser.isVisible();
filePath = aFileChooser.getCurrentDirectory().getAbsolutePath();
}
/**
* Sets the filePath of the LoaderInfo.
*/
public void setFilePath(String aFilePath)
{
filePath = aFilePath;
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
aStream.readVersion(0);
isVisible = aStream.readBool();
position = ZioUtil.readPoint(aStream);
dimension = ZioUtil.readDimension(aStream);
filePath = aStream.readString();
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
aStream.writeVersion(0);
aStream.writeBool(isVisible);
ZioUtil.writePoint(aStream, position);
ZioUtil.writeDimension(aStream, dimension);
aStream.writeString(filePath);
}

View File

@@ -49,13 +49,13 @@ public class ConstUnitProvider implements UnitProvider
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
; // Nothing to do
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
; // Nothing to do
}

View File

@@ -133,7 +133,7 @@ public class DateUnitProvider extends BaseUnitProvider
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
// Read the configuration
aStream.readVersion(0);
@@ -151,7 +151,7 @@ public class DateUnitProvider extends BaseUnitProvider
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
// Write the configuration
aStream.writeVersion(0);

View File

@@ -135,7 +135,7 @@ public class DecimalUnitProvider extends BaseUnitProvider
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
Unit protoUnit;
int protoUnitIdx;
@@ -157,7 +157,7 @@ public class DecimalUnitProvider extends BaseUnitProvider
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
int protoUnitIdx;
int decimalPlaces;

View File

@@ -38,13 +38,13 @@ public class EmptyUnitProvider implements UnitProvider
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
; // Nothing to do
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
; // Nothing to do
}

View File

@@ -129,7 +129,7 @@ public class LatLonUnitProvider extends BaseUnitProvider
}
@Override
public void zioReadRaw(ZinStream aStream) throws IOException
public void zioRead(ZinStream aStream) throws IOException
{
int decimalPlaces;
boolean isRawUnit, isZeroCentered, isSecondsShown;
@@ -150,7 +150,7 @@ public class LatLonUnitProvider extends BaseUnitProvider
}
@Override
public void zioWriteRaw(ZoutStream aStream) throws IOException
public void zioWrite(ZoutStream aStream) throws IOException
{
int decimalPlaces;
boolean isRawUnit, isZeroCentered, isSecondsShown;

View File

@@ -14,8 +14,8 @@ public class NumberUnit implements Unit
protected double conversionFactor;
/**
* Constructor
*/
* Constructor
*/
public NumberUnit(String aFullLabel, String aShortLabel, double aConversionFactor)
{
this(aFullLabel, aShortLabel, aConversionFactor, (DecimalFormat)null);
@@ -52,7 +52,7 @@ public class NumberUnit implements Unit
aStr += "0";
}
format = new DecimalFormat(aStr);
}
}
/**
* Returns whether this unit supports floating point numbers
@@ -61,13 +61,13 @@ public class NumberUnit implements Unit
{
if (format != null && format.getMaximumFractionDigits() == 0)
return false;
return true;
//System.out.println("NumFracDigits:" + format.getMaximumFractionDigits());
// return format.isParseIntegerOnly();
}
/**
* Sets in the string representation for NaN
*/
@@ -75,7 +75,7 @@ public class NumberUnit implements Unit
{
nanStr = aStr;
}
@Override
public Format getFormat()
{
@@ -84,7 +84,7 @@ public class NumberUnit implements Unit
return (Format)format.clone();
}
@Override
public String getConfigName()
{
@@ -103,61 +103,42 @@ public class NumberUnit implements Unit
@Override
public String getString(Object aVal)
{
String aStr;
aStr = "N/A";
if (aVal instanceof Number == false)
return aStr;
return nanStr;
double doubleVal = ((Number)aVal).doubleValue();
if (Double.isNaN(doubleVal) == true)
return nanStr;
if (format == null)
return "" + (((Number)aVal).doubleValue() * conversionFactor);
return "" + doubleVal * conversionFactor;
synchronized (format)
{
return format.format(((Number)aVal).doubleValue() * conversionFactor);
return format.format(doubleVal * conversionFactor);
}
}
@Override
public String getString(Object aVal, boolean isDetailed)
{
String aStr;
double aDouble;
if (aVal instanceof Number == false)
return nanStr;
aDouble = ((Number)aVal).doubleValue();
if (Double.isNaN(aDouble) == true)
return nanStr;
// Format the number
if (format == null)
{
aStr = "" + aDouble * conversionFactor;
}
else
{
synchronized (format)
{
aStr = format.format(aDouble * conversionFactor);
}
}
// Delegate
String retStr = getString(aVal);
// Add the label component
if (isDetailed == true)
aStr += " " + fullLabel;
retStr += " " + fullLabel;
else
aStr += " " + shortLabel;
retStr += " " + shortLabel;
return aStr;
return retStr;
}
@Override
public double parseString(String aStr, double eVal)
{
double aVal;
aVal = GuiUtil.readDouble(aStr, Double.NaN);
return toModel(aVal);
}

View File

@@ -1,8 +1,8 @@
package glum.unit;
import glum.zio.raw.ZioRaw;
import glum.zio.ZioObj;
public interface UnitProvider extends ZioRaw
public interface UnitProvider extends ZioObj
{
/**
* Adds a Listener for Unit changes

View File

@@ -1,10 +1,8 @@
package glum.zio;
import glum.zio.raw.ZioRaw;
import java.io.IOException;
public interface ZinStream
public interface ZinStream extends AutoCloseable
{
/**
* Releases any resources associated with the ZioInStream
@@ -117,11 +115,6 @@ public interface ZinStream
*/
public int readVersion(int... validArr) throws IOException;
/**
* Reads in the ZioRaw object from this stream
*/
public void readZioRaw(ZioRaw aBZioRaw) throws IOException;
/**
* Method to skip numBytes.
*

20
src/glum/zio/ZioObj.java Normal file
View File

@@ -0,0 +1,20 @@
package glum.zio;
import java.io.IOException;
/**
* Interface to allow for serialization of (mutable) objects.
*/
public interface ZioObj
{
/**
* Deserialization method to read data from the ZinStream.
*/
public void zioRead(ZinStream aStream) throws IOException;
/**
* Serialization method to write data to the ZoutStream.
*/
public void zioWrite(ZoutStream aStream) throws IOException;
}

View File

@@ -0,0 +1,198 @@
package glum.zio;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class ZioObjUtil
{
/**
* Utility method to read a list of ZioObj items. The objects are assumed to be of the same type.
* <P>
* Format: <numItems> (<ZioObj>)*
*/
public static <G1 extends ZioObj> ArrayList<G1> readList(ZinStream aStream, Class<G1> aClass) throws IOException
{
// Read the item count
int numItems = aStream.readInt();
// Read the actual objects
ArrayList<G1> itemList = new ArrayList<G1>();
for (int c1 = 0; c1 < numItems; c1++)
{
// Serialize the class
try
{
G1 aItem = aClass.newInstance();
aItem.zioRead(aStream);
itemList.add(aItem);
}
catch(Exception aException)
{
throw new IOException("Failed to instantiate: " + aClass, aException);
}
}
return itemList;
}
/**
* Utility method to read a preloaded list of ZioObj items. The passed in list must contain the exact number of items as that stored on disk and in the
* correct order.
* <P>
* Format: <numItems> (<ZioObj>)*
*/
public static void readList(ZinStream aStream, Collection<? extends ZioObj> aItemList) throws IOException
{
// Read the item count
int numItems = aStream.readInt();
if (numItems != aItemList.size())
throw new IOException("Items stored: " + numItems + ". Expected: " + aItemList.size());
// Read the actual BinRaw items
for (ZioObj aItem : aItemList)
aItem.zioRead(aStream);
}
/**
* Utility method to write out a list of ZioObj items.
* <P>
* Format: <numItems> (<ZioObj>)*
*/
public static void writeList(ZoutStream aStream, Collection<? extends ZioObj> aItemList) throws IOException
{
// Write the item count
aStream.writeInt(aItemList.size());
// Write the actual objects
for (ZioObj aItem : aItemList)
aItem.zioWrite(aStream);
}
/**
* Utility method to read a map of binary objects. The ZioObj items are assumed to be of the same type.
* <P>
* Format: <numItems> (<String, ZioObj>)*
*/
public static <G1 extends ZioObj> Map<String, G1> readMap(ZinStream aStream, Class<G1> aClass) throws IOException
{
// Read the item count
int numItems = aStream.readInt();
// Read the actual objects
Map<String, G1> itemMap = new LinkedHashMap<String, G1>();
for (int c1 = 0; c1 < numItems; c1++)
{
// Read the key
String aKey = aStream.readString();
// Read the value
G1 aItem = read(aStream, aClass);
itemMap.put(aKey, aItem);
}
return itemMap;
}
/**
* Utility method to read a map of ZioObj items. The items are assumed to be preloaded. Thus the passed in map must contain as many items (and in the order)
* as that which will be read in from the disk. It is therefore advisable that only LinkedHashMaps be used with this method.
*/
public static void readMap(ZinStream aStream, Map<String, ? extends ZioObj> aItemMap) throws IOException
{
// Read the item count
int numItems = aStream.readInt();
if (numItems != aItemMap.size())
throw new IOException("Items stored: " + numItems + ". Expected: " + aItemMap.size());
// Read the actual key,value pairings
String[] keyArr = aItemMap.keySet().toArray(new String[0]);
for (int c1 = 0; c1 < numItems; c1++)
{
// Read the key and ensure the proper key was read
String aKey = aStream.readString();
if (aKey.equals(keyArr[c1]) == false)
throw new IOException("Key read: " + aKey + ". Expected: " + keyArr[c1]);
// Read the value
ZioObj aItem = aItemMap.get(aKey);
aItem.zioRead(aStream);
}
}
/**
* Utility method to write out a map of ZioObj items.
* <P>
* Format: <numItems> (<String, ZioObj>)*
*/
public static void writeMap(ZoutStream aStream, Map<String, ? extends ZioObj> aItemMap) throws IOException
{
// Write the item count
aStream.writeInt(aItemMap.size());
// Write the actual objects
for (String aKey : aItemMap.keySet())
{
// Write the key
aStream.writeString(aKey);
// Write the value
ZioObj aZioObj = aItemMap.get(aKey);
aZioObj.zioWrite(aStream);
}
}
public static <G1 extends ZioObj> G1 read(ZinStream aStream, Class<G1> aClass) throws IOException
{
G1 aItem;
// Serialize the class
try
{
aItem = aClass.newInstance();
aItem.zioRead(aStream);
}
catch(Exception aException)
{
throw new IOException("Failed to instantiate: " + aClass, aException);
}
return aItem;
}
public static <G1 extends ZioObj> G1 readNullable(ZinStream aStream, Class<G1> aClass) throws IOException
{
boolean aBool = aStream.readBool();
if (aBool == false)
return null;
// Serialize the class
G1 aItem;
try
{
aItem = aClass.newInstance();
aItem.zioRead(aStream);
}
catch(Exception aException)
{
throw new IOException("Failed to instantiate: " + aClass, aException);
}
return aItem;
}
public static void writeNullable(ZoutStream aStream, ZioObj aItem) throws IOException
{
if (aItem == null)
{
aStream.writeBool(false);
return;
}
aStream.writeBool(true);
aItem.zioWrite(aStream);
}
}

19
src/glum/zio/ZioRaw.java Normal file
View File

@@ -0,0 +1,19 @@
package glum.zio;
import java.io.IOException;
/**
* Interface to allow for serialization of (immutable) objects.
* <P>
* The method zioWrite() is provided to allow immutable objects to be serialized.
* <P>
* The method zioRead() is not provided since that would imply mutability. Any deserialization should be done via the constructor.
*/
public interface ZioRaw
{
/**
* Serialization method to write data to the ZoutStream.
*/
public void zioWrite(ZoutStream aStream) throws IOException;
}

View File

@@ -0,0 +1,69 @@
package glum.zio;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
public class ZioRawUtil
{
/**
* Utility method to read a list of ZioRaw items. The following requirements must be met:
* <UL>
* <LI>The objects are assumed to be of the same type.
* <LI>The class must define a Constructor that takes one argument - a ZinStream.
* </UL>
* <P>
* Format: <numItems> (<ZioRaw>)*
*/
public static <G1 extends ZioRaw> ArrayList<G1> readList(ZinStream aStream, Class<G1> aClass) throws IOException
{
// Locate an appropriate Constructor
Constructor<G1> tmpConstructor = null;
try
{
tmpConstructor = aClass.getConstructor(ZinStream.class);
}
catch(NoSuchMethodException | SecurityException aExp)
{
throw new IOException("Failed to locate a proper constructor. Constuctor requires a single argument - ZioStream", aExp);
}
// Read the item count
int numItems = aStream.readInt();
// Read the actual objects
ArrayList<G1> itemList = new ArrayList<G1>();
for (int c1 = 0; c1 < numItems; c1++)
{
// Serialize the class
try
{
G1 aItem = tmpConstructor.newInstance(aStream);
itemList.add(aItem);
}
catch(Exception aException)
{
throw new IOException("Failed to instantiate: " + aClass, aException);
}
}
return itemList;
}
/**
* Utility method to write out a list of ZioRaw items.
* <P>
* Format: <numItems> (<ZioObj>)*
*/
public static void writeList(ZoutStream aStream, Collection<? extends ZioRaw> aItemList) throws IOException
{
// Write the item count
aStream.writeInt(aItemList.size());
// Write the actual objects
for (ZioRaw aItem : aItemList)
aItem.zioWrite(aStream);
}
}

View File

@@ -1,10 +1,8 @@
package glum.zio;
import glum.zio.raw.ZioRaw;
import java.io.IOException;
public interface ZoutStream
public interface ZoutStream extends AutoCloseable
{
/**
* Releases any resources associated with the stream
@@ -14,8 +12,8 @@ public interface ZoutStream
/**
* Returns the checksum (as a string) of the stream.
* <P>
* Note, if the stream is still open, then the returned value will be the checksum evaluated as of the last byte sent
* to this stream (with no buffering effects - closing the stream immediately will not result in a different value).
* Note, if the stream is still open, then the returned value will be the checksum evaluated as of the last byte sent to this stream (with no buffering
* effects - closing the stream immediately will not result in a different value).
*/
public String getCheckSum() throws IOException;
@@ -73,8 +71,8 @@ public interface ZoutStream
public void writeString(String aStr) throws IOException;
/**
* Utility method to write out a raw string. Note the inverse function is {@link ZinStream#readRawStringAndValidate}.
* The string will be interpreted as a US-ASCII string.
* Utility method to write out a raw string. Note the inverse function is {@link ZinStream#readRawStringAndValidate}. The string will be interpreted as a
* US-ASCII string.
*/
public void writeRawString(String aStr) throws IOException;
@@ -95,14 +93,8 @@ public interface ZoutStream
public void writeFully(byte[] dstArr) throws IOException;
/**
* Method to write the version to the stream. To properly read the version, use the inverse function
* {@link ZinStream#readVersion}.
* Method to write the version to the stream. To properly read the version, use the inverse function {@link ZinStream#readVersion}.
*/
public void writeVersion(int aVersion) throws IOException;
/**
* Writes the ZioRaw object to this stream
*/
public void writeZioRaw(ZioRaw aBinRaw) throws IOException;
}

View File

@@ -1,23 +0,0 @@
package glum.zio.raw;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import java.io.IOException;
/**
* Interface to allow for simple serialization of binary objects.
*/
public interface ZioRaw
{
/**
* Deserialization method to read data from the ZinStream
*/
public void zioReadRaw(ZinStream aStream) throws IOException;
/**
* Serialization method to write data to the ZoutStream
*/
public void zioWriteRaw(ZoutStream aStream) throws IOException;
}

View File

@@ -1,222 +0,0 @@
package glum.zio.raw;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class ZioRawUtil
{
/**
* Utility method to read a list of BinRaw items. The objects are assumed to be of the same type.
* <P>
* Format: <numItems> (<BinRaw>)*
*/
public static <G1 extends ZioRaw> ArrayList<G1> readRawList(ZinStream aStream, Class<G1> binClass) throws IOException
{
ArrayList<G1> itemList;
G1 aItem;
int numItems;
// Read the item count
numItems = aStream.readInt();
// Read the actual objects
itemList = new ArrayList<G1>();
for (int c1 = 0; c1 < numItems; c1++)
{
// Serialize the class
try
{
aItem = binClass.newInstance();
aItem.zioReadRaw(aStream);
itemList.add(aItem);
}
catch (Exception aException)
{
throw new IOException("Failed to instantiate: " + binClass, aException);
}
}
return itemList;
}
/**
* Utility method to read a preloaded list of BinRaw items. The passed in list must contain the exact number of items
* as that stored on disk and in the correct order.
* <P>
* Format: <numItems> (<BinRaw>)*
*/
public static void readRawList(ZinStream aStream, Collection<? extends ZioRaw> itemList) throws IOException
{
int numItems;
// Read the item count
numItems = aStream.readInt();
if (numItems != itemList.size())
throw new IOException("Items stored: " + numItems + ". Expected: " + itemList.size());
// Read the actual BinRaw items
for (ZioRaw aItem : itemList)
aItem.zioReadRaw(aStream);
}
/**
* Utility method to write out a list of BinRaw items.
* <P>
* Format: <numItems> (<BinRaw>)*
*/
public static void writeRawList(ZoutStream aStream, Collection<? extends ZioRaw> itemList) throws IOException
{
// Write the item count
aStream.writeInt(itemList.size());
// Write the actual objects
for (ZioRaw aItem : itemList)
aItem.zioWriteRaw(aStream);
}
/**
* Utility method to read a map of binary objects. The BinRaw items are assumed to be of the same type.
* <P>
* Format: <numItems> (<String, BinRaw>)*
*/
public static <G1 extends ZioRaw> Map<String, G1> readRawMap(ZinStream aStream, Class<G1> binClass) throws IOException
{
Map<String, G1> itemMap;
String aKey;
G1 aItem;
int numItems;
// Read the item count
numItems = aStream.readInt();
// Read the actual objects
itemMap = new LinkedHashMap<String, G1>();
for (int c1 = 0; c1 < numItems; c1++)
{
// Read the key
aKey = aStream.readString();
// Read the value
aItem = readRaw(aStream, binClass);
itemMap.put(aKey, aItem);
}
return itemMap;
}
/**
* Utility method to read a map of raw binary items. The items are assumed to be preloaded. Thus the passed in map
* must contain as many items (and in the order) as that which will be read in from the disk. It is therefore
* advisable that only LinkedHashMaps be used with this method.
*/
public static void readRawMap(ZinStream aStream, Map<String, ? extends ZioRaw> itemMap) throws IOException
{
String[] keyArr;
String aKey;
ZioRaw aItem;
int numItems;
// Read the item count
numItems = aStream.readInt();
if (numItems != itemMap.size())
throw new IOException("Items stored: " + numItems + ". Expected: " + itemMap.size());
// Read the actual key,value pairings
keyArr = itemMap.keySet().toArray(new String[0]);
for (int c1 = 0; c1 < numItems; c1++)
{
// Read the key and ensure the proper key was read
aKey = aStream.readString();
if (aKey.equals(keyArr[c1]) == false)
throw new IOException("Key read: " + aKey + ". Expected: " + keyArr[c1]);
// Read the value
aItem = itemMap.get(aKey);
aItem.zioReadRaw(aStream);
}
}
/**
* Utility method to write out a map of binary raw items.
* <P>
* Format: <numItems> (<String, BinRaw>)*
*/
public static void writeRawMap(ZoutStream aStream, Map<String, ? extends ZioRaw> itemMap) throws IOException
{
ZioRaw aBinObj;
// Write the item count
aStream.writeInt(itemMap.size());
// Write the actual objects
for (String aKey : itemMap.keySet())
{
// Write the key
aStream.writeString(aKey);
// Write the value
aBinObj = itemMap.get(aKey);
aBinObj.zioWriteRaw(aStream);
}
}
public static <G1 extends ZioRaw> G1 readRaw(ZinStream aStream, Class<G1> binClass) throws IOException
{
G1 aItem;
// Serialize the class
try
{
aItem = binClass.newInstance();
aItem.zioReadRaw(aStream);
}
catch (Exception aException)
{
throw new IOException("Failed to instantiate: " + binClass, aException);
}
return aItem;
}
public static <G1 extends ZioRaw> G1 readNullableRaw(ZinStream aStream, Class<G1> binClass) throws IOException
{
boolean aBool;
G1 aItem;
aBool = aStream.readBool();
if (aBool == false)
return null;
// Serialize the class
try
{
aItem = binClass.newInstance();
aItem.zioReadRaw(aStream);
}
catch (Exception aException)
{
throw new IOException("Failed to instantiate: " + binClass, aException);
}
return aItem;
}
public static void writeNullableRaw(ZoutStream aStream, ZioRaw aItem) throws IOException
{
if (aItem == null)
{
aStream.writeBool(false);
return;
}
aStream.writeBool(true);
aItem.zioWriteRaw(aStream);
}
}

View File

@@ -1,7 +1,7 @@
package glum.zio;
package glum.zio.stream;
import glum.util.WallTimer;
import glum.zio.raw.ZioRaw;
import glum.zio.ZinStream;
import glum.zio.util.ZioUtil;
import java.io.IOException;
@@ -29,9 +29,8 @@ public abstract class BaseZinStream implements ZinStream
* @param computeCheckSum
* True if a checksum (md5sum) is desired to be computed as the stream is written
* @param streamSizeHint
* A hint which indicates the final size of the source stream. This hint will be used to determine if a
* direct buffer should be allocated. If the hint size is greater than 25 MB then a direct buffer will be
* allocated. A value of 0 implies that a direct buffer should not be allocated.
* A hint which indicates the final size of the source stream. This hint will be used to determine if a direct buffer should be allocated. If the
* hint size is greater than 25 MB then a direct buffer will be allocated. A value of 0 implies that a direct buffer should not be allocated.
*/
public BaseZinStream(boolean computeCheckSum, long streamSizeHint) throws IOException
{
@@ -47,7 +46,7 @@ public abstract class BaseZinStream implements ZinStream
digest = MessageDigest.getInstance("MD5");
digestPos = 0;
}
catch (NoSuchAlgorithmException aExp)
catch(NoSuchAlgorithmException aExp)
{
throw new IOException("Unreconized Algorithm", aExp);
}
@@ -75,7 +74,7 @@ public abstract class BaseZinStream implements ZinStream
digest = MessageDigest.getInstance("MD5");
digestPos = 0;
}
catch (NoSuchAlgorithmException aExp)
catch(NoSuchAlgorithmException aExp)
{
throw new IOException("Unreconized Algorithm", aExp);
}
@@ -86,7 +85,7 @@ public abstract class BaseZinStream implements ZinStream
if (workBuffer == null)
throw new NullPointerException();
}
/**
* Returns the length of time (in milliseconds) this stream has been open
*/
@@ -104,7 +103,7 @@ public abstract class BaseZinStream implements ZinStream
// Stop the timer
wallTimer.stop();
// Force the checksum to be computed
getCheckSum();
@@ -327,12 +326,6 @@ public abstract class BaseZinStream implements ZinStream
throw new IOException("Unreconized version... Read: " + readVersion + " Expected one of the following: " + Arrays.toString(validArr));
}
@Override
public void readZioRaw(ZioRaw aZioRaw) throws IOException
{
aZioRaw.zioReadRaw(this);
}
@Override
public void skipBytes(int numBytes) throws IOException
{
@@ -355,23 +348,21 @@ public abstract class BaseZinStream implements ZinStream
}
/**
* Helper method to refresh the workBuffer with new data from the stream. This method ensures that workBuffer will
* always have enough data to support reading.
* Helper method to refresh the workBuffer with new data from the stream. This method ensures that workBuffer will always have enough data to support
* reading.
* <P>
* If there is no more data on the stream then this method should throw an IOException
*/
protected abstract void refreshWorkBuffer() throws IOException;
/**
* Helper method to release any stream related vars. This method will only be called once, the very first time the
* method {@link #close()} is called.
* Helper method to release any stream related vars. This method will only be called once, the very first time the method {@link #close()} is called.
*/
protected abstract void releaseStreamVars() throws IOException;
/**
* Helper method that ensures the digest has been updated with any data that has been "read" thus far. The definition
* of "read" is any data returned from the stream via one of the read methods. The digest shall not be updated with
* any buffered data - only data that has been read from the stream.
* Helper method that ensures the digest has been updated with any data that has been "read" thus far. The definition of "read" is any data returned from the
* stream via one of the read methods. The digest shall not be updated with any buffered data - only data that has been read from the stream.
*/
protected void updateDigest() throws IOException
{

View File

@@ -1,7 +1,7 @@
package glum.zio;
package glum.zio.stream;
import glum.util.WallTimer;
import glum.zio.raw.ZioRaw;
import glum.zio.ZoutStream;
import glum.zio.util.ZioUtil;
import java.io.IOException;
@@ -27,8 +27,8 @@ public abstract class BaseZoutStream implements ZoutStream
* @param computeCheckSum
* True if a checksum (md5sum) is desired to be computed as the stream is written
* @param isDirect
* True if a direct buffer is desired. This should only be true if the stream is going to a physical I/O
* component (disk, network) and the size of the final stream will be at least ~50 MB.
* True if a direct buffer is desired. This should only be true if the stream is going to a physical I/O component (disk, network) and the size of
* the final stream will be at least ~50 MB.
*/
public BaseZoutStream(boolean computeCheckSum, boolean isDirect) throws IOException
{
@@ -41,7 +41,7 @@ public abstract class BaseZoutStream implements ZoutStream
if (computeCheckSum == true)
digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException aExp)
catch(NoSuchAlgorithmException aExp)
{
throw new IOException("Unreconized Algorithm", aExp);
}
@@ -268,15 +268,8 @@ public abstract class BaseZoutStream implements ZoutStream
ZioUtil.writeCompactInt(this, aVersion);
}
@Override
public void writeZioRaw(ZioRaw aZioRaw) throws IOException
{
aZioRaw.zioWriteRaw(this);
}
/**
* Helper method that ensures the digest has been updated with any buffered data. The buffer will be cleared after
* the digest has been updated.
* Helper method that ensures the digest has been updated with any buffered data. The buffer will be cleared after the digest has been updated.
* <P>
* The method shall be called exclusively from {@link BaseZoutStream#emptyWorkBuffer()}.
*/
@@ -294,14 +287,13 @@ public abstract class BaseZoutStream implements ZoutStream
}
/**
* Helper method to empty the workBuffer and copy the contents to the stream. The contents of the workBuffer will be
* output to the "stream". This method ensures that workBuffer will always have enough data to support writing
* Helper method to empty the workBuffer and copy the contents to the stream. The contents of the workBuffer will be output to the "stream". This method
* ensures that workBuffer will always have enough data to support writing
*/
protected abstract void emptyWorkBuffer() throws IOException;
/**
* Helper method to release any stream related vars. This method will only be called once, the very first time the
* method {@link #close()} is called.
* Helper method to release any stream related vars. This method will only be called once, the very first time the method {@link #close()} is called.
*/
protected abstract void releaseStreamVars() throws IOException;

View File

@@ -1,4 +1,4 @@
package glum.zio;
package glum.zio.stream;
import java.io.EOFException;
import java.io.IOException;

View File

@@ -1,4 +1,4 @@
package glum.zio;
package glum.zio.stream;
import java.io.IOException;
import java.util.Arrays;

View File

@@ -0,0 +1,200 @@
package glum.zio.stream;
import glum.zio.ZoutStream;
import java.io.IOException;
import com.google.common.base.Charsets;
/**
* ZoutStream used for debugging. This is useful when trying to determine when and where a specific byte is being written out.
* <P>
* When the specified nth byte has been written this object will throw an IOException.
*/
public class DebugZoutStream implements ZoutStream
{
private int byteCnt;
private int failByteCnt;
/**
* @param aFailByteCnt
* The nth byte that when written will cause an Exception to be raised.
*/
public DebugZoutStream(int aFailByteCnt)
{
byteCnt = 0;
failByteCnt = aFailByteCnt;
}
/**
* Returns the number of bytes that have been written
*/
public int getNumBytes()
{
return byteCnt;
}
@Override
public void close() throws IOException
{
// Nothing to do
}
@Override
public String getCheckSum() throws IOException
{
throw new IOException("Unsupported operation.");
}
@Override
public long getPosition() throws IOException
{
return byteCnt;
}
@Override
public void writeByte(byte aByte) throws IOException
{
byteCnt++;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeBool(boolean aBool) throws IOException
{
byteCnt++;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeChar(char aChar) throws IOException
{
byteCnt += 2;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeInt(int aInt) throws IOException
{
byteCnt += 4;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeLong(long aLong) throws IOException
{
byteCnt += 8;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeShort(short aShort) throws IOException
{
byteCnt += 2;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeFloat(float aFloat) throws IOException
{
byteCnt += 4;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeDouble(double aDouble) throws IOException
{
byteCnt += 8;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeString(String aStr) throws IOException
{
byte[] data;
int size;
// Null strings are handled in special fashion
if (aStr == null)
{
byteCnt += 2;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
return;
}
// Empty strings are handled in special fashion
if (aStr.equals("") == true)
{
byteCnt += 2;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
return;
}
// Transform the string to it's UTF-8 bytes
data = aStr.getBytes(Charsets.UTF_8);
size = data.length;
// Ensure the string size is less than 0x00FFFF
if (size >= 0x00FFFF)
throw new RuntimeException("Transformed UTF-8 string is too large! Max size: " + (0x00FFFF - 1) + " Curr size:" + size);
byteCnt += 2 + size;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeRawString(String aStr) throws IOException
{
byteCnt += aStr.length();
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeFully(byte[] dstArr, int offset, int length) throws IOException
{
byteCnt += length;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeFully(byte[] dstArr) throws IOException
{
byteCnt += dstArr.length;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
@Override
public void writeVersion(int aVersion) throws IOException
{
if (aVersion < 255)
byteCnt++;
else
byteCnt += 5;
if (byteCnt >= failByteCnt)
throwBadByteWrittenException();
}
/**
* Helper method that throws the actual exception.
*/
public void throwBadByteWrittenException() throws IOException
{
throw new IOException("Bad byte has been written.");
}
}

View File

@@ -1,4 +1,4 @@
package glum.zio;
package glum.zio.stream;
import java.io.EOFException;
import java.io.File;

View File

@@ -1,4 +1,4 @@
package glum.zio;
package glum.zio.stream;
import java.io.File;
import java.io.FileOutputStream;

View File

@@ -1,6 +1,6 @@
package glum.zio;
package glum.zio.stream;
import glum.zio.raw.ZioRaw;
import glum.zio.ZoutStream;
import java.io.IOException;
@@ -12,12 +12,12 @@ import com.google.common.base.Charsets;
public class NullZoutStream implements ZoutStream
{
private int byteCount;
public NullZoutStream()
{
byteCount = 0;
}
/**
* Returns the number of bytes that have been written
*/
@@ -97,29 +97,29 @@ public class NullZoutStream implements ZoutStream
{
byte[] data;
int size;
// Null strings are handled in special fashion
if (aStr == null)
{
byteCount += 2;
return;
}
// Empty strings are handled in special fashion
if (aStr.equals("") == true)
{
byteCount += 2;
return;
}
// Transform the string to it's UTF-8 bytes
data = aStr.getBytes(Charsets.UTF_8);
size = data.length;
// Ensure the string size is less than 0x00FFFF
if (size >= 0x00FFFF)
throw new RuntimeException("Transformed UTF-8 string is too large! Max size: " + (0x00FFFF - 1) + " Curr size:" + size);
byteCount += 2 + size;
}
@@ -150,10 +150,4 @@ public class NullZoutStream implements ZoutStream
byteCount += 5;
}
@Override
public void writeZioRaw(ZioRaw aZioRaw) throws IOException
{
aZioRaw.zioWriteRaw(this);
}
}

View File

@@ -6,7 +6,7 @@ import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import glum.zio.BaseZinStream;
import glum.zio.stream.BaseZinStream;
/**
* Package private class to transform an InputStream to a ZinStream (view).

View File

@@ -1,6 +1,6 @@
package glum.zio.util;
import glum.zio.BaseZoutStream;
import glum.zio.stream.BaseZoutStream;
import java.io.IOException;
import java.io.OutputStream;

View File

@@ -2,10 +2,10 @@ package glum.zio.util;
import glum.task.Task;
import glum.unit.TimeCountUnit;
import glum.zio.FileZinStream;
import glum.zio.FileZoutStream;
import glum.zio.ZinStream;
import glum.zio.ZoutStream;
import glum.zio.stream.FileZinStream;
import glum.zio.stream.FileZoutStream;
import java.awt.Color;
import java.awt.Dimension;