下面是要求上传的第二批文件:
com/sienco/sonoclot/signature/SimpleSignature.java (签名算法)
package com.sienco.sonoclot.signature;
import com.sienco.sonoclot.signature.FilterPointList;
import com.sienco.sonoclot.signature.LineSegment;
import com.sienco.sonoclot.signature.SignatureException;
import com.smallplanet.jyd.JYDPoint;
import com.smallplanet.util.SPSCurveData;
import com.smallplanet.util.SPSGraphicsException;
import com.smallplanet.util.SPSUtilities;
import com.smallplanet.util.SPSXYPoint;
import java.text.DecimalFormat;
import java.util.*;
public class SimpleSignature extends FilterPointList {
protected static final float DEFAULT_MINIMUM_SIGNATURE_DATA_SPACING = 0.008333334F;
protected static final float HISTORIC_MINIMUM_SIGNATURE_DATA_SPACING = 0.0016666667F;
static final float MaxAnalysisDelay = 0.1F;
private static DecimalFormat fmt = new DecimalFormat(“###0.0000”);
private SPSCurveData mySPSCurve = new SPSCurveData();
private boolean lastDataPointUncompressed = false;
public SimpleSignature(float tolerance, float minimumSpacing) {
super(minimumSpacing);
}
public SimpleSignature(float tolerance) {
super(0.008333334F);
}
public SimpleSignature(float minimumspacing, List newData) throws SignatureException {
super(minimumspacing);
this.initializeSignature(newData);
}
public SimpleSignature(List newData) throws SignatureException {
super(0.0016666667F);
this.initializeSignature(newData);
}
void removeEndPoint() {
super.removeEndPoint();
}
private void initializeSignature(List newData) throws SignatureException {
ListIterator tempIterator = newData.listIterator();
while(tempIterator.hasNext()) {
Object tempObject = tempIterator.next();
if(tempObject != null) {
SPSXYPoint tempPoint;
if(tempObject instanceof SPSXYPoint) {
tempPoint = (SPSXYPoint)tempObject;
} else {
JYDPoint jydPoint = (JYDPoint)tempObject;
tempPoint = new SPSXYPoint(jydPoint.getX(), jydPoint.getY());
}
super.addEndPoint(tempPoint);
}
}
}
public SPSCurveData getSignatureCurve() {
this.updateSPSCurve();
return this.mySPSCurve;
}
private void updateSPSCurve() {
this.mySPSCurve.clear();
Iterator tempIterator = this.iterator();
while(tempIterator.hasNext()) {
Object tempObject = tempIterator.next();
if(tempObject != null && tempObject instanceof SPSXYPoint) {
SPSXYPoint tempPoint = (SPSXYPoint)tempObject;
try {
this.mySPSCurve.addPoint(tempPoint);
} catch (SPSGraphicsException var5) {
SPSUtilities.throwNullPointerException(“SimpleSignature.updateSPSCurve() bad data”);
}
}
}
}
protected SPSXYPoint getMinClotSignalPointBetweenTimes(double t0, double t1) {
double yMinimum = 100000.0D;
SPSXYPoint minPoint = null;
int index = this.getIndexBeforeT(t0);
for(int maxIndex = this.getIndexBeforeT(t1); index <= maxIndex; ++index) {
SPSXYPoint tempPoint = this.getPoint(index);
double yTest = tempPoint.getY();
if(yTest < yMinimum) {
yMinimum = yTest;
minPoint = tempPoint;
}
}
return minPoint;
}
float getTimeForYNearTime(float time, float yTarget, float timeRange) {
float returnTime = Float.NaN;
int ilow = this.getIndexBeforeT(time);
int ihigh;
LineSegment tempSegment;
for(ihigh = ilow + 1; ilow >= 0; --ilow) {
tempSegment = this.getLineSegment(ilow);
if(tempSegment.isYOnSegment(yTarget)) {
returnTime = tempSegment.getTforY(yTarget);
break;
}
if(tempSegment.getStartTime() <= time - timeRange) {
break;
}
}
if(Float.isNaN(returnTime)) {
while(ihigh < this.size() - 1) {
tempSegment = this.getLineSegment(ihigh);
if(tempSegment.isYOnSegment(yTarget)) {
returnTime = tempSegment.getTforY(yTarget);
break;
}
if(tempSegment.getEndTime() >= time + timeRange) {
break;
}
++ihigh;
}
}
return returnTime;
}
float getLastClotSignal() {
float tempLastClotSignal = this.getClotSignalAtTime(this.getEndTime());
return tempLastClotSignal;
}
public float getClotSignalAtTime(float t) {
float y = this.getYAtT((double)t);
return y;
}
double getTimeForClotSignalAfterTime(double testClotSignal, double initialTime) {
double tempReturnTime = Double.NaN;
double t0 = initialTime;
double y0 = (double)this.getYAtT(initialTime);
int i = this.getIndexAfterT((float)initialTime);
double t1 = (double)this.getTimeAtIndex(i);
double y1 = this.getClotSignalAtIndex(i);
for(int indexLimit = this.size(); (y0 - testClotSignal) * (y1 - testClotSignal) > 0.0D && i < indexLimit; t1 = (double)this.getTimeAtIndex(i)) {
y0 = y1;
t0 = t1;
i;
y1 = this.getClotSignalAtIndex(i);
}
System.out.println("SimpleSignature.getTimeForClotSignalAfterTime():\n y0 = " + y0 + "\n y1 = " + y1 + "\n t0 = " + t0 + "\n t1 = " + t1 + "\n testClotSignal = " + testClotSignal);
if((y0 - testClotSignal) * (y1 - testClotSignal) <= 0.0D) {
double tempSlope = (y1 - y0) / (t1 - t0);
double tempOffset = y0 - tempSlope * t0;
tempReturnTime = Math.abs(tempSlope) > 1.0E-7D?(testClotSignal - tempOffset) / tempSlope:t0;
}
System.out.println("SimpleSignature.getTimeForClotSignalAfterTime():\n testClotSignal = " + testClotSignal + "\n initialTime = " + initialTime + "\n tempReturnTime = " + tempReturnTime);
return tempReturnTime;
}
public SPSXYPoint getPointAtTime(float t) {
float y = this.getYAtT((double)t);
SPSXYPoint tempPoint = new SPSXYPoint(t, y);
return tempPoint;
}
private double getTimeLength() {
double tempTimeLength = (double)this.getEndTime();
return tempTimeLength;
}
private float getYAtT(double t) {
float returnValue = Float.NaN;
if(this.size() == 0) {
returnValue = 0.0F;
} else if(this.size() == 1) {
SPSXYPoint index = this.getFirstPoint();
returnValue = (float)index.getY();
} else {
int index1 = this.getIndexBeforeT(t);
SPSXYPoint point0;
SPSXYPoint point1;
if(index1 < this.size() - 1) {
point0 = this.getPoint(index1);
point1 = this.getPoint(index1 + 1);
} else {
point0 = this.getPoint(index1 - 1);
point1 = this.getPoint(index1);
}
float y = (float)(point0.getY() + (point1.getY() - point0.getY()) * (t - point0.getX()) / (point1.getX() - point0.getX()));
returnValue = y;
}
return returnValue;
}
protected int getIndexBeforeT(double t) {
int returnInt;
if(this.size() < 2) {
returnInt = 0;
} else {
int iLow = 0;
int iHigh = this.size() - 1;
boolean index = false;
double testTime = this.getPoint(iLow).getX();
if(t < testTime) {
returnInt = iLow;
} else {
testTime = this.getPoint(iHigh).getX();
if(t > testTime) {
returnInt = iHigh;
} else {
while(iHigh - iLow > 1) {
int index1 = (iHigh + iLow) / 2;
testTime = this.getPoint(index1).getX();
if(t < testTime) {
iHigh = index1;
} else {
iLow = index1;
}
}
returnInt = iLow;
}
}
}
return returnInt;
}
public float getAnalysisEndTime(float tempAnalysisPeriod) {
float returnTime = Float.NaN;
int tempSize = this.size();
if(tempSize > 1 && this.getEndTime() - this.getStartTime() > tempAnalysisPeriod * 2.0F) {
float tempTime0 = (float)this.getPoint(tempSize - 2).getX();
float tempTime1 = this.getEndTime();
if(tempTime1 - tempTime0 < tempAnalysisPeriod) {
returnTime = tempTime0;
} else {
returnTime = tempTime1 - tempAnalysisPeriod;
}
}
return returnTime;
}
protected float getTimeAtIndex(int index) {
float tempFloat;
try {
SPSXYPoint e = this.getPoint(index);
tempFloat = (float)e.getX();
} catch (ArrayIndexOutOfBoundsException var4) {
tempFloat = 0.0F;
}
return tempFloat;
}
LineSegment getLineSegment(int index) {
LineSegment returnSegment = null;
if(this.size() > 1) {
SPSXYPoint point0 = this.getPoint(index);
SPSXYPoint point1 = this.getPoint(index + 1);
returnSegment = new LineSegment(point0, point1);
}
return returnSegment;
}
public void addUncompressedPoint(SPSXYPoint newPoint) {
if(this.size() > 0) {
SPSXYPoint currentEndPoint = this.getLastPoint();
if(!this.isTooClose(currentEndPoint, newPoint)) {
if(this.lastDataPointUncompressed) {
this.removeEndPoint();
}
super.addEndPoint(newPoint);
this.updateSPSCurve();
this.lastDataPointUncompressed = true;
}
}
}
public void addEndPoint(SPSXYPoint newPoint) {
SPSUtilities.throwNullPointerException(“SimpleSignature.addEndPoint() this should never be called”);
}
public void addCompressedPoint(SPSXYPoint newPoint) {
if(this.lastDataPointUncompressed) {
this.removeEndPoint();
}
super.addEndPoint(newPoint);
this.updateSPSCurve();
this.lastDataPointUncompressed = false;
}
protected double getMaximumSlopeBetweenTimes(float tLow, float tHigh) {
double maxSlope = Double.NaN;
int iMin = this.getIndexBeforeT(tLow);
int iMax = this.getIndexAfterT(tHigh);
double y0 = Double.NaN;
double y1 = Double.NaN;
double t0 = Double.NaN;
double t1 = Double.NaN;
double tempSlope = Double.NaN;
while(Double.isNaN(y0) || iMin < iMax) {
t0 = (double)this.getTimeAtIndex(iMin);
y0 = this.getClotSignalAtIndex(iMin);
t1 = (double)this.getTimeAtIndex(iMin);
y1 = this.getClotSignalAtIndex(iMin);
tempSlope = (y1 - y0) / (t1 - t0);
if(Double.isNaN(maxSlope) || tempSlope > maxSlope) {
maxSlope = tempSlope;
}
}
if(Double.isNaN(maxSlope)) {
System.out.println(“SimpleSignature.getMaximumSlopeBetweenTimes(): unexpected NaN:”);
System.out.println(" tLow = " + tLow);
System.out.println(" tHigh = " + tHigh);
System.out.println(" iMin = " + iMin);
System.out.println(" iMax = " + iMax);
System.out.println(" y0 = " + y0);
System.out.println(" y1 = " + y1);
System.out.println(" t0 = " + t0);
System.out.println(" t1 = " + t1);
System.out.println(" tempSlope = " + tempSlope);
SPSUtilities.throwNullPointerException("SimpleSignature.getMaximumSlopeBetweenTimes()😊;
}
return maxSlope;
}
double getClotSignalAtIndex(int index) {
double tempDouble;
try {
SPSXYPoint e = this.getPoint(index);
tempDouble = e.getY();
} catch (ArrayIndexOutOfBoundsException var5) {
tempDouble = 0.0D;
}
return tempDouble;
}
protected double getAlignedTimeBefore(double t) {
int index = this.getIndexBeforeT(t);
double t0 = (double)this.getTimeAtIndex(index);
return t0;
}
protected double getAlignedTimeAfter(double t) {
int index = this.getIndexBeforeT(t);
if(index + 1 < this.size()) {
index;
}
double t0 = (double)this.getTimeAtIndex(index);
return t0;
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(“SimpleSignature”);
buf.append(“\n Number of points = " + this.size());
Iterator iter = this.getPointsIterator();
while(iter.hasNext()) {
SPSXYPoint tempPoint = (SPSXYPoint)iter.next();
buf.append(”\n " + tempPoint);
}
return buf.toString();
}
public List<Map<String,String>> getPointListData(){
List<Map<String,String>> result = new ArrayList<Map<String, String>>();
Iterator iter = this.getPointsIterator();
Map<String,String> data = null;
int index = 0;
while(iter.hasNext()) {
SPSXYPoint tempPoint = (SPSXYPoint)iter.next();
data = new HashMap<String, String>();
result.add(data);
data.put(“i”, index + “”);
data.put(“x”, tempPoint.getXData());
data.put(“y”, tempPoint.getYData());
index;
}
return result;
}
}
com/sienco/sonoclot/sigwindow/SignatureDisplay.java (签名显示)
package com.sienco.sonoclot.sigwindow;
import com.sienco.sonoclot.signature.SimpleSignature;
import com.sienco.sonoclot.test.SignatureTest;
import com.sienco.sonoclot.test.Test;
import com.sienco.sonoclot.test.TestManager;
import com.sienco.sonoclot.wrapper.WrapperManager;
import com.smallplanet.exceptions.NiceExceptionsManager;
import com.smallplanet.jyd.JYDBusyFlagThread;
import com.smallplanet.util.SPSGraphObject;
import com.smallplanet.util.SPSLinearCurve;
import com.smallplanet.util.SPSStrokeIcon;
import com.smallplanet.util.SPSXYAutoGraph;
import com.smallplanet.util.SPSXYPoint;
import my.bean.TLVType;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SignatureDisplay implements Comparable {
static Color DEFAULT_COLOR;
SignatureResultsPanel theResultsPanel;
SigWindow mySigWindow;
SPSXYPoint previousLastPoint;
SPSXYAutoGraph myGraph;
SPSLinearCurve myGraphCurve;
private SPSGraphObject myHighlightGraphics;
SignatureTest myTest;
int myStrokeIndex;
Color myColor;
private boolean highlightedFlag = false;
static final boolean DEBUG_PLATELET_FUNCTION = false;
private static final Color HIGHLIGHT_COLOR;
private static final Color HIGHLIGHT_PLATELET_COLOR;
ChangeListener myTypeOfTestChangeListener;
ChangeListener myDataCollectionChangeListener;
ChangeListener myResultChangeListener;
SignatureDisplay(SignatureDisplayWrapper newSignatureDisplayWrapper, SigWindow newSigWindow, SignatureTest newTest) throws SigWindowException {
this.myTypeOfTestChangeListener = new NamelessClass_1();
this.myDataCollectionChangeListener = new NamelessClass_2();
this.myResultChangeListener = new NamelessClass_3();
this.myTest = newTest;
this.mySigWindow = newSigWindow;
this.initialize(newSignatureDisplayWrapper, this.mySigWindow.getGraph());
this.theResultsPanel = new SignatureResultsPanel(this.mySigWindow, this);
}
SignatureDisplay(SignatureDisplay tempSignatureDisplay, SPSXYAutoGraph newGraph) {
this.myTypeOfTestChangeListener = new NamelessClass_1();
this.myDataCollectionChangeListener = new NamelessClass_2();
this.myResultChangeListener = new NamelessClass_3();
this.myTest = tempSignatureDisplay.getSignatureTest();
this.myGraph = newGraph;
this.myStrokeIndex = tempSignatureDisplay.getStrokeIndex();
this.myColor = tempSignatureDisplay.getColor();
this.myGraphCurve = new SPSLinearCurve(this.myTest.getSignatureCurve(), this.myColor, SignatureStrokes.getStroke(this.myStrokeIndex, false), SignatureStrokes.getStroke(this.myStrokeIndex, true));
this.myGraph.addGraphObject(this.myGraphCurve);
}
public String toString() {
String tempString = "SignatureDislay : SignatureTestId = " + this.myTest.getId();
return tempString;
}
public String getId() {
return this.myTest.getId();
}
public SignatureTest getSignatureTest() {
return this.myTest;
}
SPSStrokeIcon getStrokeIcon(int tempLength) {
SPSStrokeIcon tempStrokeIcon = new SPSStrokeIcon(this.getColor(), this.getStroke(), tempLength);
return tempStrokeIcon;
}
void initialize(SignatureDisplayWrapper newSignatureDisplayWrapper, SPSXYAutoGraph newGraph) throws SigWindowException {
this.myTest = this.getSignatureTest();
if (this.myTest == null) {
NiceExceptionsManager.newDebug(“null SignatureTest in SignatureDisplay()”);
throw new SigWindowException(“No SignatureTest found for null test”);
} else {
this.mySigWindow.getBusyFlag();
this.myTest.getBusyFlag();
JYDBusyFlagThread tempThread = (JYDBusyFlagThread)Thread.currentThread();
tempThread.callChangeListenerLater(this.myTest, “addTypeOfTestChangeListener”, this.myTypeOfTestChangeListener);
tempThread.callChangeListenerLater(this.myTest, “addDataCollectionChangeListener”, this.myDataCollectionChangeListener);
tempThread.callChangeListenerLater(this.myTest, “addResultChangeListener”, this.myResultChangeListener);
this.myTest.freeBusyFlag();
this.myGraph = newGraph;
this.myStrokeIndex = newSignatureDisplayWrapper.getStrokeIndex();
this.myColor = new Color(newSignatureDisplayWrapper.getColorRGB());
this.myGraphCurve = new SPSLinearCurve(this.myTest.getSignatureCurve(), this.myColor, SignatureStrokes.getStroke(this.myStrokeIndex, false), SignatureStrokes.getStroke(this.myStrokeIndex, true));
this.myGraph.addGraphObject(this.myGraphCurve);
this.mySigWindow.freeBusyFlag();
}
}
void initializeGraphics(Graphics2D g) {
this.theResultsPanel.initializeGraphics(g);
}
String getTestName() {
return this.getSignatureTest().getTestName();
}
String getApplication() {
return this.getSignatureTest().getApplication();
}
Color getNonHighlightedColor() {
return this.myGraphCurve.getColor();
}
void setHighlighted(boolean newFlag) {
this.highlightedFlag = newFlag;
this.myGraphCurve.setHighlighted(this.highlightedFlag);
if (newFlag) {
if (this.myHighlightGraphics != null) {
this.myGraph.addGraphObject(this.myHighlightGraphics);
}
} else if (this.myHighlightGraphics != null) {
this.myGraph.removeGraphObject(this.myHighlightGraphics);
}
}
void setHighlightGraphics() {
SignatureTest tempTest = this.getSignatureTest();
SPSGraphObject tempHighlightGraphics = tempTest.getHighlightGraphics();
if (tempHighlightGraphics != null) {
if (this.myHighlightGraphics != null) {
this.myGraph.removeGraphObject(this.myHighlightGraphics);
}
this.myHighlightGraphics = tempHighlightGraphics;
}
}
boolean isHighlighted() {
return this.highlightedFlag;
}
SignatureResultsPanel getResultsPanel() throws SigWindowException {
if (this.theResultsPanel == null) {
throw new SigWindowException(“Unexpected resultsPanel == null”);
} else {
return this.theResultsPanel;
}
}
void setStrokeIndex(int tempStrokeIndex) {
this.myStrokeIndex = tempStrokeIndex;
this.myGraphCurve.setUnscaledStroke(SignatureStrokes.getStroke(tempStrokeIndex, false), SignatureStrokes.getStroke(tempStrokeIndex, true));
}
int getStrokeIndex() {
return this.myStrokeIndex;
}
BasicStroke getStroke() {
return SignatureStrokes.getStroke(this.getStrokeIndex(), this.isHighlighted());
}
protected Color getColor() {
return this.myColor;
}
void setColor(Color newColor) {
this.myColor = newColor;
this.myGraphCurve.setColor(newColor);
}
public int compareTo(Object testObject) {
String primaryKey = this.mySigWindow.getPrimarySortKey();
String secondaryKey = this.mySigWindow.getSecondarySortKey();
SignatureDisplay testSignatureDisplay = (SignatureDisplay)testObject;
SignatureTest refSignatureTest = this.getSignatureTest();
SignatureTest testSignatureTest = testSignatureDisplay.getSignatureTest();
Comparable testObjectP0 = refSignatureTest.getSortFieldValue(primaryKey);
Comparable testObjectS0 = refSignatureTest.getSortFieldValue(secondaryKey);
Comparable testObjectP1 = testSignatureTest.getSortFieldValue(primaryKey);
Comparable testObjectS1 = testSignatureTest.getSortFieldValue(secondaryKey);
int primaryTest = testObjectP0 != null ? testObjectP0.compareTo(testObjectP1) : 0;
int secondaryTest = testObjectS0 != null ? testObjectS0.compareTo(testObjectS1) : 0;
int returnInt = primaryTest != 0 ? primaryTest : secondaryTest;
return returnInt;
}
void dispose() {
Test tempTest = this.getSignatureTest();
tempTest.getBusyFlag();
tempTest.freeBusyFlag();
this.theResultsPanel.dispose();
this.myGraph.removeGraphObject(this.myGraphCurve);
this.theResultsPanel = null;
this.mySigWindow = null;
this.myGraphCurve = null;
this.myGraph = null;
}
void removeListeners() {
Test tempTest = this.getSignatureTest();
tempTest.removeTypeOfTestChangeListener(this.myTypeOfTestChangeListener);
tempTest.removeDataCollectionChangeListener(this.myDataCollectionChangeListener);
tempTest.removeResultChangeListener(this.myResultChangeListener);
this.theResultsPanel.removeListeners();
}
static {
DEFAULT_COLOR = Color.lightGray;
HIGHLIGHT_COLOR = Color.red;
HIGHLIGHT_PLATELET_COLOR = HIGHLIGHT_COLOR.darker().darker();
}
class NamelessClass_1 implements ChangeListener {
NamelessClass_1() {
}
public void stateChanged(ChangeEvent newEvent) {
SigWindowManager.getBusyFlag();
SignatureDisplay.this.mySigWindow.getBusyFlag();
SignatureDisplay.this.myTest = (SignatureTest)newEvent.getSource();
SignatureDisplay.this.theResultsPanel.changeTypeOfTest();
SignatureDisplay.this.mySigWindow.rebuildSummaryTableModel();
SignatureDisplay.this.mySigWindow.setSelectedSignatureTest();
SignatureDisplay.this.mySigWindow.freeBusyFlag();
SigWindowManager.freeBusyFlag();
SignatureDisplay.this.mySigWindow.repaintSummaryTable();
}
}
class NamelessClass_2 implements ChangeListener {
NamelessClass_2() {
}
public void stateChanged(ChangeEvent e) {
SigWindowManager.getBusyFlag();
SignatureDisplay.this.mySigWindow.getBusyFlag();
Test tempTest = SignatureDisplay.this.getSignatureTest();
tempTest.getBusyFlag();
SPSXYPoint newPoint = tempTest.getLastPoint();
if (SignatureDisplay.this.previousLastPoint == null) {
SignatureDisplay.this.previousLastPoint = newPoint;
} else {
float tempLength = SignatureDisplay.this.myGraph.xPixelLength(SignatureDisplay.this.previousLastPoint.getX(), newPoint.getX());
if (tempLength > 1.5F) {
SignatureDisplay.this.previousLastPoint = newPoint;
SignatureDisplay.this.myGraph.updateObjects();
SignatureDisplay.this.mySigWindow.getSignaturePanel().repaint();
}
}
tempTest.freeBusyFlag();
SignatureDisplay.this.mySigWindow.freeBusyFlag();
SigWindowManager.freeBusyFlag();
}
}
class NamelessClass_3 implements ChangeListener {
NamelessClass_3() {
}
public void stateChanged(ChangeEvent newEvent) {
SigWindowManager.getBusyFlag();
SignatureDisplay.this.mySigWindow.getBusyFlag();
TestManager.getBusyFlag();
SignatureTest tempTest = (SignatureTest)newEvent.getSource();
tempTest.getBusyFlag();
SignatureDisplay.this.mySigWindow.rebuildModelColumnWidths();
SignatureDisplay.this.mySigWindow.revalidateSummaryTable();
SignatureDisplay.this.mySigWindow.repaintSummaryTable();
String testName = tempTest.getTestName();
List<Map<String,String>> simpleSignatureData = tempTest.getSimpleSignature().getPointListData();
tempTest.freeBusyFlag();
TestManager.freeBusyFlag();
SignatureDisplay.this.mySigWindow.freeBusyFlag();
SigWindowManager.freeBusyFlag();
//发送实验名称和数据
Map<String, Object> aboutInfo = new HashMap<String, Object>();
aboutInfo.put(“TestName”, testName);
aboutInfo.put(“SignData”, simpleSignatureData);
WrapperManager.SendInfo(TLVType.TestData, aboutInfo);
}
}
}
com/viscell/busyFlag/BusyFlag.java (线程同步)
package com.viscell.busyFlag;
import com.smallplanet.jyd.JYDBusyFlagThread;
import com.smallplanet.util.SPSUtilities;
import java.util.LinkedList;
public class BusyFlag {
private static LinkedList busyFlagList = new LinkedList();
private static boolean debugFlag = false;
private Thread busyThread = null;
private int busyCount = 0;
private int savedPriority;
private Exception lockTraceException;
private boolean blockedFlag = false;
private static final int BUSY_PRIORITY = 9;
private static final int BUSY_AND_BLOCKED_PRIORITY = 8;
private Object myBusyObject;
private Class myClass;
private long timeOfFirstAttempt;
private boolean lockoutExceptionPrinted;
public BusyFlag(Object newBusyObject) {
if (newBusyObject == null) {
SPSUtilities.throwNullPointerException(“BusyFlag.init() newBusyObject = null”);
}
this.myBusyObject = newBusyObject;
this.myClass = this.myBusyObject.getClass();
}
public static void setDebug(boolean newState) {
debugFlag = newState;
}
public synchronized void getBusyFlag() {
BusyFlagThread currentBusyFlagThread = null;
Thread currentThread = Thread.currentThread();
if (currentThread instanceof BusyFlagThread) {
currentBusyFlagThread = (BusyFlagThread)currentThread;
currentBusyFlagThread.setUpcomingBusyFlag(this);
}
if (!this.callingListOK()) {
currentBusyFlagThread.printSyncObjects();
SPSUtilities.throwNullPointerException("BusyFlag.getBusyFlag() !callingListOK for:\n currentThread = " + currentThread + " newCallingObject Class = " + this.myClass);
}
int currentPriority = currentThread.getPriority();
this.timeOfFirstAttempt = System.currentTimeMillis();
this.lockoutExceptionPrinted = false;
while(!this.tryGetBusyFlag()) {
long currentTime = System.currentTimeMillis();
if (!this.lockoutExceptionPrinted && currentTime - this.timeOfFirstAttempt > 999L) {
this.lockoutExceptionPrinted = true;
System.out.println("BusyFlag.getBusyFlag() blocked: Thread = " + Thread.currentThread() + " dumpStack() Thread = ");
Thread.dumpStack();
}
try {
if (currentPriority == 9) {
currentThread.setPriority(8);
}
this.wait(1000L);
} catch (InterruptedException var7) {
}
}
if (currentBusyFlagThread != null) {
currentBusyFlagThread.setUpcomingBusyFlag((BusyFlag)null);
}
}
public Exception getSyncException() {
return this.lockTraceException;
}
public synchronized boolean tryGetBusyFlag() {
boolean returnFlag = false;
if (this.busyThread == null) {
this.busyThread = Thread.currentThread();
this.lockTraceException = new Exception();
this.savedPriority = this.busyThread.getPriority();
this.busyThread.setPriority(9);
this.busyCount = 1;
returnFlag = true;
} else if (this.busyThread == Thread.currentThread()) {
++this.busyCount;
returnFlag = true;
}
if (returnFlag) {
this.addSyncObject();
} else if (debugFlag && !this.blockedFlag) {
System.out.println("tryGetBusyFlag() for " + Thread.currentThread() + " at: “);
(new Exception()).printStackTrace();
System.out.println(” Lock held by " + this.busyThread + " at: ");
this.lockTraceException.printStackTrace();
this.blockedFlag = true;
}
return returnFlag;
}
public synchronized void freeBusyFlag() {
Thread tempBusyFlagThread = this.getBusyFlagOwner();
if (tempBusyFlagThread == Thread.currentThread()) {
–this.busyCount;
this.removeSyncObject();
if (this.busyCount == 0) {
this.busyThread.setPriority(this.savedPriority);
if (debugFlag && this.blockedFlag) {
System.out.println("freeBusyFlag() for Thread = " + Thread.currentThread() + " Object = " + this.myBusyObject);
this.blockedFlag = false;
}
this.busyThread = null;
this.notify();
}
if (this.busyCount < 0) {
SPSUtilities.throwNullPointerException(“BusyFlag.freeBusyFlag() busyCount < 0”);
}
} else {
SPSUtilities.throwNullPointerException("BusyFlag.freeBusyFlag() invalid BusyFlagOwner = " + tempBusyFlagThread);
}
}
public Thread getBusyFlagOwner() {
return this.busyThread;
}
public Object getSyncObject() {
return this.myBusyObject;
}
private boolean callingListOK() {
Thread currentThread = Thread.currentThread();
if (currentThread instanceof BusyFlagThread) {
return ((BusyFlagThread)currentThread).callingListOK(this);
} else {
return currentThread instanceof JYDBusyFlagThread ? ((JYDBusyFlagThread)currentThread).callingListOK(this) : true;
}
}
private void addSyncObject() {
if (this.busyThread instanceof BusyFlagThread) {
((BusyFlagThread)this.busyThread).addSyncObject(this);
}
if (this.busyThread instanceof JYDBusyFlagThread) {
((JYDBusyFlagThread)this.busyThread).addSyncObject(this);
}
}
private void removeSyncObject() {
if (this.busyThread instanceof BusyFlagThread) {
((BusyFlagThread)this.busyThread).removeSyncObject(this);
}
if (this.busyThread instanceof JYDBusyFlagThread) {
((JYDBusyFlagThread)this.busyThread).removeSyncObject(this);
}
}
public int getBusyCount() {
return this.getBusyFlagOwner() == Thread.currentThread() ? this.busyCount : 0;
}
public String toString() {
return "BusyFlag: Class = " + this.myClass + " busyThread = " + this.busyThread;
}
}
my/utils/tlv/TLVDecoder.java (TLV协议解码)
package my.utils.tlv;
import java.util.ArrayList;
import java.util.List;
/**
TLV解码实现
<p/>
Created by lhd on 2015/09/26.
/
public class TLVDecoder {
public static int canDecode(byte[] tlvBytes){
int result = 0;
if (tlvBytes == null || tlvBytes.length == 0) {
return result;
}
int tagBytesSize = 4;
if(tlvBytes.length<tagBytesSize){
return result;
}
int lengthBytesSize = 4;
if(tlvBytes.length<tagBytesSize + lengthBytesSize){
return result;
}
byte[] lengthBytes = new byte[lengthBytesSize];
System.arraycopy(tlvBytes, tagBytesSize, lengthBytes, 0,
lengthBytesSize);
int valueBytesSize = decodeLength(lengthBytes);
if(tlvBytes.length<tagBytesSize + lengthBytesSize + valueBytesSize){
return result;
}
result = tagBytesSize + lengthBytesSize + valueBytesSize;
return result;
}
/*
解析TLV字节数组
@param tlvBytes
@return
/
public static TLVDecodeResult decode(byte[] tlvBytes) throws Throwable {
List list = new ArrayList();
TLVDecodeResult result = null;
try {
result = decodeImpl(tlvBytes, list);
} catch (Throwable throwable) {
throw throwable;
}
return result;
}
/*
递归逐个解析TLV
@param tlvBytes
@param list
@return
/
private static TLVDecodeResult decodeImpl(byte[] tlvBytes, List list) {
if (tlvBytes == null || tlvBytes.length == 0) {
return null;
}
// 截取Tag
// int tagBytesSize = getTagBytesSize(tlvBytes);
int tagBytesSize = 4;
byte[] tagBytes = new byte[tagBytesSize];
System.arraycopy(tlvBytes, 0, tagBytes, 0, tagBytesSize);
// System.out.println(“info:”+tlvBytes.length+" "+tagBytesSize);
// 截取Length
// int lengthBytesSize = getLengthBytesSize(tlvBytes, tagBytesSize);
int lengthBytesSize = 4;
byte[] lengthBytes = new byte[lengthBytesSize];
System.arraycopy(tlvBytes, tagBytesSize, lengthBytes, 0,
lengthBytesSize);
// 截取Value
int valueBytesSize = decodeLength(lengthBytes);
byte[] valueBytes = new byte[valueBytesSize];
System.arraycopy(tlvBytes, tagBytesSize + lengthBytesSize, valueBytes,
0, valueBytesSize);
// 解析数据
TLVDecodeResult result = decodeFirstTLV(tagBytes, lengthBytes,
valueBytes);
if (result != null)
list.add(result);
// int totalSize = tlvBytes.length;
// int firstTLVSize = tagBytesSize + lengthBytesSize + valueBytesSize;
// if (totalSize > firstTLVSize) {// 父V中有多个子TLV结构体
// decodeSecondTLV(tlvBytes, firstTLVSize, list);
// }
return result;
}
/*
解析同级V中的第一个TLV
@param tagBytes
@param lengthBytes
@param valueBytes
@return
/
private static TLVDecodeResult decodeFirstTLV(byte[] tagBytes, byte[] lengthBytes, byte[] valueBytes) {
int dataType = decodeDataType(tagBytes);
TLVDecodeResult result = new TLVDecodeResult();
result.setFrameType(decodeFrameType(tagBytes));
result.setDataType(dataType);
result.setTagBytes(tagBytes);
result.setLengthBytes(lengthBytes);
result.setValueBytes(valueBytes);
result.setTagValue(decodeTagValue(tagBytes));
result.setLength(decodeLength(lengthBytes));
// if (dataType == TLVEncoder.ConstructedData) {
//// List childList = new ArrayList();
//// decodeImpl(valueBytes, childList);
//// result.setValue(childList);
//// } else {
//// result.setValue(valueBytes);
//// }
/
if (result.getTagValue() == 0) {
System.err.println(“RID:”+result.getIntValue()); }
/
return result;
}
/*
解析同级V中的第二个TLV
@param tlvBytes
@param firstTLVSize
@param list
/
private static TLVDecodeResult decodeSecondTLV(byte[] tlvBytes, int firstTLVSize, List list) {
int totalSize = tlvBytes.length;
byte[] nextBytes = new byte[totalSize - firstTLVSize];
System.arraycopy(tlvBytes, firstTLVSize, nextBytes, 0, totalSize - firstTLVSize);
TLVDecodeResult result = decodeImpl(nextBytes, list);
return result;
}
/*
获取到全部的TLV数量
@param tlvBytes
@return
/
public static int getTLVSize(byte[] tlvBytes) {
int size = 0;
// 截取Tag
int tagBytesSize = getTagBytesSize(tlvBytes);
byte[] tagBytes = new byte[tagBytesSize];
System.arraycopy(tlvBytes, 0, tagBytes, 0, tagBytesSize);
// 截取Length
int lengthBytesSize = getLengthBytesSize(tlvBytes, tagBytesSize);
byte[] lengthBytes = new byte[lengthBytesSize];
System.arraycopy(tlvBytes, tagBytesSize, lengthBytes, 0,
lengthBytesSize);
// 截取Value
int valueBytesSize = decodeLength(lengthBytes);
byte[] valueBytes = new byte[valueBytesSize];
System.arraycopy(tlvBytes, tagBytesSize + lengthBytesSize, valueBytes,
0, valueBytesSize);
size++;
// 解析数据
int dataType = decodeDataType(tagBytes);
TLVDecodeResult result = new TLVDecodeResult();
result.setFrameType(decodeFrameType(tagBytes));
result.setDataType(dataType);
result.setTagValue(decodeTagValue(tagBytes));
result.setLength(decodeLength(lengthBytes));
if (dataType == TLVEncoder.ConstructedData) {
size = size + getTLVSize(valueBytes);
} else {
// size++;
}
int totalSize = tlvBytes.length;
int firstTLVSize = tagBytesSize + lengthBytesSize + valueBytesSize;
if (totalSize > firstTLVSize) {
byte[] nextBytes = new byte[totalSize - firstTLVSize];
System.arraycopy(tlvBytes, firstTLVSize, nextBytes, 0, totalSize
- firstTLVSize);
size = size + getTLVSize(nextBytes);
}
return size;
}
/*
获取Tag占用的字节数
@param tlvBytes
@return
/
public static int getTagBytesSize(byte[] tlvBytes) {
int length = 0;
for (byte b : tlvBytes) {
length++;
int test = b & 0x80;
if (test == 0) {
return length;
}
}
return 0;
}
/*
获取Length占用的字节数
@param tlvBytes
@return
/
public static int getLengthBytesSize(byte[] tlvBytes, int offset) {
int size = 0;
for (int i = offset; i < tlvBytes.length; i++) {
size++;
int test = tlvBytes[i] & 0x80;
if (test == 0x00) {
return size;
}
}
return 0;
}
/*
解析TLV的Tag中的frameType
@param tagBytes
@return
/
public static int decodeFrameType(byte[] tagBytes) {
return TLVEncoder.PrivateFrame & tagBytes[0];
}
/*
解析TLV的Tag中的dataType
@param tagBytes
@return
/
public static int decodeDataType(byte[] tagBytes) {
return TLVEncoder.ConstructedData & tagBytes[0];
}
/*
解析TLV的Tag中的tagValue
@param tagBytes
@return
/
public static int decodeTagValue(byte[] tagBytes) {
// int tagValue = 0x80 & tagBytes[0];
// int result = 0;
// if (tagValue != 0x80) {
// result = tagBytes[0] & 0x1f;
// } else {
// //高位到低位解析
//// result = decodeTagValueFromHighToLowBit(tagBytes);
// //低位到高位解析
// result = decodeValueFromLowToHighBit(tagBytes);
// }
// // System.out.println(result);
// return result;
byte[] r = TLVUtils.swapHL(tagBytes);
return (int) TLVUtils.byteArrayToLong®;
}
/*
从高位到低位解析tagValue
@param bytes
@return
/
private static int decodeValueFromHighToLowBit(byte[] bytes) {
int result = 0;
for (int i = 1; i < bytes.length; i++) {
result |= (0x7f & bytes[i]) << 7 * (bytes.length - i - 1);
}
return result;
}
/*
从低位到高位解析tagValue
@param bytes
@return
/
private static int decodeValueFromLowToHighBit(byte[] bytes) {
int result = 0;
for (int i = 1; i < bytes.length; i++) {
result |= (0x7f & bytes[i]) << 7 * (i - 1);
}
return result;
}
/*
解析TLV中的Length
@param lengthBytes
@return
*/
public static int decodeLength(byte[] lengthBytes) {
// int result = 0;
// int len = 0x80 & lengthBytes[0];
// if (len != 0x80) {
// result = (int) TLVUtils.byteArrayToLong(lengthBytes);
// } else {
// result |= 0x7f & lengthBytes[0];
// for (int i = 1; i < lengthBytes.length; i++) {
// result |= (0x7f & lengthBytes[i]) << 7 * (lengthBytes.length - i);
// }
// }
// return result;
byte[] r = TLVUtils.swapHL(lengthBytes);
return (int) TLVUtils.byteArrayToLong®;
}
}
my/utils/tlv/TLVEncoder.java (TLV协议编码)
package my.utils.tlv;
/**
TLV编码实现
<p/>
Created by lhd on 2015/09/26.
/
public class TLVEncoder {
/*
基本数据类型
/
public static final int PrimitiveFrame = 0x00;
/*
私有类型
/
public static final int PrivateFrame = 0x40;
/*
基本类型数据编码
/
public static final int PrimitiveData = 0x00;
/*
TLV类型数据编码
/
public static final int ConstructedData = 0x20;
/*
TLV格式编码
@param frameType
@param dataType
@param tagValue
@param value
@return
/
public static TLVEncodeResult encode(int frameType, int dataType,
int tagValue, byte[] value) {
byte[] tagBytes = encodeTag(frameType, dataType, tagValue);
// System.out.println(“tag:”+new BigInteger(1, tagBytes).toString(2));
byte[] lengthBytes = encodeLength(value == null ? 0 : value.length);
// System.out.println(“length:” + value.length);
// System.out.println(“lengthBytes:” + new BigInteger(1, lengthBytes).toString(2));
TLVEncodeResult result = new TLVEncodeResult();
result.setTagBytes(tagBytes);
result.setTagSize(tagBytes.length);
result.setLengthBytes(lengthBytes);
result.setLengthSize(lengthBytes.length);
result.setValueBytes(value);
result.setValueSize(value == null ? 0 : value.length);
return result;
}
/*
TLV格式编码
@param frameType
@param dataType
@param tagValue
@param value
@return
/
public static TLVEncodeResult encode(int frameType, int dataType,
int tagValue, String value) {
if (value != null)
return encode(frameType, dataType, tagValue, value.getBytes());
else
return encode(frameType, dataType, tagValue, (byte[]) null);
}
public static TLVEncodeResult encode(int frameType, int dataType,
int tagValue, long value) {
return encode(frameType, dataType, tagValue,
TLVUtils.longToByteArray(value));
}
/*
<p>
生成 Tag ByteArray
</p>
<p>
其中 tagValue <= 2097151,超过之后编码的结果是错误的
</p>
@param tagValue Tag 值,即协议中定义的交易类型 或 基本数据类型
@param frameType TLV类型,Tag首字节最左两bit为00:基本类型,01:私有类型(自定义类型)
@param dataType 数据类型,Tag首字节第5位为0:基本数据类型,1:结构类型(TLV类型,即TLV的V为一个TLV结构)
@return Tag ByteArray
/
public static byte[] encodeTag(int frameType, int dataType, int tagValue) {
int result = frameType | dataType | tagValue;
// int digit = 0;
// if (tagValue >= 0x1f) {
// result = frameType | dataType | 0x80;
// digit = (int) computeTagDigit(tagValue);
// result <<= 8 * digit;
// //高位到低位
//// result = encodeTagValueFromHighToLowBit(result, digit, tagValue);
// //低位到高位
// result = encodeValueFromLowToHighBit(result, digit, tagValue);
// }
// return intToByteArrayForTag(result, digit);
// result = encodeValueFromLowToHighBit(result, 4, tagValue);
// return TLVUtils.intToByteArray(result);
// return result;
byte[] r = TLVUtils.intToByteArray(result);
return TLVUtils.swapHL®;
}
/*
从高位到低位对tagValue进行编码
@param result
@param digit
@param value
@return
/
private static int encodeValueFromHighToLowBit(int result, int digit, int value) {
//高位到低位
for (int i = digit - 1; i > 0; i–) {
result |= ((value >> i * 7 & 0x7f) | 0x80) << i * 8;
}
result |= value & 0x7f;
return result;
}
/*
从低位到高位对tagValue进行编码
@param result 形参,int类型的只传值进来,因此必须反馈编码结果
@param digit
@param value
@return
/
private static int encodeValueFromLowToHighBit(int result, int digit, int value) {
//低位到高位
for (int i = 0; i < digit - 1; i++) {
result |= ((value >> i * 7 & 0x7f) | 0x80) << (digit - 1 - i) * 8;
// System.out.println(“tag:”+Integer.toBinaryString(((tagValue >> i * 7 & 0x7f) | 0x80) << (digit - 1 - i) * 8));
// System.out.println(“rawTag:” + Integer.toBinaryString(rawTag));
}
// System.out.println(“high:” + Integer.toBinaryString((tagValue >> (digit - 1) * 7) & 0x7f));
result |= (value >> (digit - 1) * 7) & 0x7f;
return result;
}
private static byte[] intToByteArrayForTag(int value, int digit) {
byte[] result = new byte[digit + 1];
int length = result.length;
result[0] = (byte) ((value >> (8 * digit)) & 0xff);
for (int i = 0; i < length; i++) {
//这里有两种方法来实现高位到低位或者低位到高位的编码,具体选择哪种待评估
// if (i == length -1) {
// result[i] = (byte) ((value >> (8 * (i - 1))) & 0x7f);
// } else {
// result[i] = (byte) ((value >> (8 * (i - 1))) & 0xff | 0x80);
// }
result[i] = (byte) ((value >> (8 * (digit - i))) & 0xff);//高位到低位
}
return result;
}
private static byte[] intToByteArrayForLength(int value, int digit) {
byte[] result = new byte[digit];
int length = result.length;
result[0] = (byte) ((value >> (8 * (digit - 1))) & 0xff);
// for (int i = 1; i < length; i++) {
// //这里有两种方法来实现高位到低位或者低位到高位的编码,具体选择哪种待评估
//// if (i == length -1) {
//// result[i] = (byte) ((value >> (8 * (i - 1))) & 0x7f);
//// } else {
//// result[i] = (byte) ((value >> (8 * (i - 1))) & 0xff | 0x80);
//// }
// result[i] = (byte) ((value >> (8 * (digit - i - 1))) & 0xff);//高位到低位
// }
return result;
}
/*
对数计算换底公式
@param value
@param base
@return
/
public static double log(double value, double base) {
return Math.log(value) / Math.log(base);
}
/*
计算Tag字节数,推导出来的计算公式
@param value
@return
/
private static double computeTagDigit(double value) {
if (value < 0x1f) {
throw new IllegalArgumentException(
“the tag value must not less than 31.”);
}
return Math.ceil(log(value + 1, 128));
}
/*
生成Length的byte数组
@param length
@return
/
public static byte[] encodeLength(int length) {
if (length < 0) {
throw new IllegalArgumentException(
“the length must not less than 0.”);
}
// if (length < 128) {
// byte[] lengthBytes = new byte[1];
// lengthBytes[0] = (byte) (0x7f & length);
// return lengthBytes;
// } else {
// int digit = (int) computeLengthDigit(length);
// int result = 0;
// result = encodeValueFromLowToHighBit(result, digit, length);
// return intToByteArrayForLength(result, digit);
// }
byte[] r = TLVUtils.intToByteArray(length);
return TLVUtils.swapHL®;
}
/*
计算length的字节数,推导出来的计算公式
@param length
@return
*/
private static double computeLengthDigit(int length) {
return Math.ceil(log(length + 1, 128));
}
}