mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-12 16:47:31 -05:00
Updates pull request from Roman Tsypuk to convert the build system from
ant to gradle. Removes linux and windows application launch scripts and produces an executable jar with all resources included.
This commit is contained in:
32
src/main/java/io/github/dsheirer/util/Angle.java
Normal file
32
src/main/java/io/github/dsheirer/util/Angle.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
******************************************************************************/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
public class Angle
|
||||
{
|
||||
|
||||
public static double degreesFromRadians( double radians )
|
||||
{
|
||||
return radians * ( 180 / Math.PI );
|
||||
}
|
||||
|
||||
public static double radiansFromDegrees( double degrees )
|
||||
{
|
||||
return degrees * ( Math.PI / 180 );
|
||||
}
|
||||
}
|
||||
262
src/main/java/io/github/dsheirer/util/ArcTangent.java
Normal file
262
src/main/java/io/github/dsheirer/util/ArcTangent.java
Normal file
@@ -0,0 +1,262 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* ----------------------------------------------
|
||||
* Adapted from GNU Radio's gr_fast_atan2f() method, retrieved Jan 2013 from
|
||||
* https://raw.github.com/gnuradio/gnuradio/master/gnuradio-core/src/lib/general/gr_fast_atan2f.cc
|
||||
*
|
||||
* Copyright 2005 Free Software Foundation, Inc.
|
||||
*
|
||||
* This file is part of GNU Radio
|
||||
*
|
||||
* GNU Radio is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* GNU Radio is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GNU Radio; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 51 Franklin Street,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import io.github.dsheirer.sample.complex.Complex;
|
||||
|
||||
public class ArcTangent
|
||||
{
|
||||
public static final double sTANGENT_MAP_RESOLUTION = 0.003921549;
|
||||
public static final double sRADIANS_PER_DEGREE = 0.017453293;
|
||||
public static final int sTANGENT_MAP_SIZE = 256;
|
||||
public static final double sHALF_PI = Math.PI / 2;
|
||||
|
||||
//ArcTangents from 0 to pi/4 radians
|
||||
private static double[] sLOOKUP_TABLE = new double[] {
|
||||
0.000000e+00, 3.921549e-03, 7.842976e-03, 1.176416e-02,
|
||||
1.568499e-02, 1.960533e-02, 2.352507e-02, 2.744409e-02,
|
||||
3.136226e-02, 3.527947e-02, 3.919560e-02, 4.311053e-02,
|
||||
4.702413e-02, 5.093629e-02, 5.484690e-02, 5.875582e-02,
|
||||
6.266295e-02, 6.656816e-02, 7.047134e-02, 7.437238e-02,
|
||||
7.827114e-02, 8.216752e-02, 8.606141e-02, 8.995267e-02,
|
||||
9.384121e-02, 9.772691e-02, 1.016096e-01, 1.054893e-01,
|
||||
1.093658e-01, 1.132390e-01, 1.171087e-01, 1.209750e-01,
|
||||
1.248376e-01, 1.286965e-01, 1.325515e-01, 1.364026e-01,
|
||||
1.402496e-01, 1.440924e-01, 1.479310e-01, 1.517652e-01,
|
||||
1.555948e-01, 1.594199e-01, 1.632403e-01, 1.670559e-01,
|
||||
1.708665e-01, 1.746722e-01, 1.784728e-01, 1.822681e-01,
|
||||
1.860582e-01, 1.898428e-01, 1.936220e-01, 1.973956e-01,
|
||||
2.011634e-01, 2.049255e-01, 2.086818e-01, 2.124320e-01,
|
||||
2.161762e-01, 2.199143e-01, 2.236461e-01, 2.273716e-01,
|
||||
2.310907e-01, 2.348033e-01, 2.385093e-01, 2.422086e-01,
|
||||
2.459012e-01, 2.495869e-01, 2.532658e-01, 2.569376e-01,
|
||||
2.606024e-01, 2.642600e-01, 2.679104e-01, 2.715535e-01,
|
||||
2.751892e-01, 2.788175e-01, 2.824383e-01, 2.860514e-01,
|
||||
2.896569e-01, 2.932547e-01, 2.968447e-01, 3.004268e-01,
|
||||
3.040009e-01, 3.075671e-01, 3.111252e-01, 3.146752e-01,
|
||||
3.182170e-01, 3.217506e-01, 3.252758e-01, 3.287927e-01,
|
||||
3.323012e-01, 3.358012e-01, 3.392926e-01, 3.427755e-01,
|
||||
3.462497e-01, 3.497153e-01, 3.531721e-01, 3.566201e-01,
|
||||
3.600593e-01, 3.634896e-01, 3.669110e-01, 3.703234e-01,
|
||||
3.737268e-01, 3.771211e-01, 3.805064e-01, 3.838825e-01,
|
||||
3.872494e-01, 3.906070e-01, 3.939555e-01, 3.972946e-01,
|
||||
4.006244e-01, 4.039448e-01, 4.072558e-01, 4.105574e-01,
|
||||
4.138496e-01, 4.171322e-01, 4.204054e-01, 4.236689e-01,
|
||||
4.269229e-01, 4.301673e-01, 4.334021e-01, 4.366272e-01,
|
||||
4.398426e-01, 4.430483e-01, 4.462443e-01, 4.494306e-01,
|
||||
4.526070e-01, 4.557738e-01, 4.589307e-01, 4.620778e-01,
|
||||
4.652150e-01, 4.683424e-01, 4.714600e-01, 4.745676e-01,
|
||||
4.776654e-01, 4.807532e-01, 4.838312e-01, 4.868992e-01,
|
||||
4.899573e-01, 4.930055e-01, 4.960437e-01, 4.990719e-01,
|
||||
5.020902e-01, 5.050985e-01, 5.080968e-01, 5.110852e-01,
|
||||
5.140636e-01, 5.170320e-01, 5.199904e-01, 5.229388e-01,
|
||||
5.258772e-01, 5.288056e-01, 5.317241e-01, 5.346325e-01,
|
||||
5.375310e-01, 5.404195e-01, 5.432980e-01, 5.461666e-01,
|
||||
5.490251e-01, 5.518738e-01, 5.547124e-01, 5.575411e-01,
|
||||
5.603599e-01, 5.631687e-01, 5.659676e-01, 5.687566e-01,
|
||||
5.715357e-01, 5.743048e-01, 5.770641e-01, 5.798135e-01,
|
||||
5.825531e-01, 5.852828e-01, 5.880026e-01, 5.907126e-01,
|
||||
5.934128e-01, 5.961032e-01, 5.987839e-01, 6.014547e-01,
|
||||
6.041158e-01, 6.067672e-01, 6.094088e-01, 6.120407e-01,
|
||||
6.146630e-01, 6.172755e-01, 6.198784e-01, 6.224717e-01,
|
||||
6.250554e-01, 6.276294e-01, 6.301939e-01, 6.327488e-01,
|
||||
6.352942e-01, 6.378301e-01, 6.403565e-01, 6.428734e-01,
|
||||
6.453808e-01, 6.478788e-01, 6.503674e-01, 6.528466e-01,
|
||||
6.553165e-01, 6.577770e-01, 6.602282e-01, 6.626701e-01,
|
||||
6.651027e-01, 6.675261e-01, 6.699402e-01, 6.723452e-01,
|
||||
6.747409e-01, 6.771276e-01, 6.795051e-01, 6.818735e-01,
|
||||
6.842328e-01, 6.865831e-01, 6.889244e-01, 6.912567e-01,
|
||||
6.935800e-01, 6.958943e-01, 6.981998e-01, 7.004964e-01,
|
||||
7.027841e-01, 7.050630e-01, 7.073330e-01, 7.095943e-01,
|
||||
7.118469e-01, 7.140907e-01, 7.163258e-01, 7.185523e-01,
|
||||
7.207701e-01, 7.229794e-01, 7.251800e-01, 7.273721e-01,
|
||||
7.295557e-01, 7.317307e-01, 7.338974e-01, 7.360555e-01,
|
||||
7.382053e-01, 7.403467e-01, 7.424797e-01, 7.446045e-01,
|
||||
7.467209e-01, 7.488291e-01, 7.509291e-01, 7.530208e-01,
|
||||
7.551044e-01, 7.571798e-01, 7.592472e-01, 7.613064e-01,
|
||||
7.633576e-01, 7.654008e-01, 7.674360e-01, 7.694633e-01,
|
||||
7.714826e-01, 7.734940e-01, 7.754975e-01, 7.774932e-01,
|
||||
7.794811e-01, 7.814612e-01, 7.834335e-01, 7.853983e-01,
|
||||
7.853983e-01 };
|
||||
|
||||
/**
|
||||
* Lookup radian angle from polar coordinates represented by the
|
||||
* ComplexSample
|
||||
*
|
||||
* @param sample representing polar coordinates
|
||||
* @return - angle in radians
|
||||
*/
|
||||
public static double getAngle( Complex sample )
|
||||
{
|
||||
return getAngle( ( (double) sample.inphase() ),
|
||||
( (double) sample.quadrature() ) );
|
||||
}
|
||||
/**
|
||||
* Lookup radian angle from polar coordinates x and y
|
||||
* @param x - x polar coordinate
|
||||
* @param y - y polar coordinate
|
||||
* @return - angle in radians
|
||||
*/
|
||||
public static double getAngle( double x, double y )
|
||||
{
|
||||
/**
|
||||
* Calculates the angle of the vector (x,y) based on a table lookup and
|
||||
* linear interpolation. The table uses a 256 point table covering
|
||||
* -45 to +45 degrees and uses symmetry to determine the final angle
|
||||
* value in the range of -180 to 180 degrees. Note that this function
|
||||
* uses the small angle approximation for values close to zero. This
|
||||
* routine calculates the arc tangent with an average error of
|
||||
* +/- 0.045 degrees.
|
||||
*/
|
||||
double alpha;
|
||||
double angle;
|
||||
double base_angle;
|
||||
int index;
|
||||
|
||||
//Check for divide by zero
|
||||
if( x == 0.0 && y == 0.0 )
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
//Normalize to +/- 45 degree range
|
||||
double x_abs = Math.abs( x );
|
||||
double y_abs = Math.abs( y );
|
||||
|
||||
//Calculate the ratio ( opposite/adjacent )
|
||||
double z;
|
||||
|
||||
if( x_abs < y_abs )
|
||||
{
|
||||
z = x_abs / y_abs;
|
||||
}
|
||||
else
|
||||
{
|
||||
z = y_abs / x_abs;
|
||||
}
|
||||
|
||||
//When the ratio approaches table resolution, the angle is best
|
||||
//approximated with the argument itself
|
||||
if( z < sTANGENT_MAP_RESOLUTION )
|
||||
{
|
||||
base_angle = z;
|
||||
}
|
||||
else
|
||||
{
|
||||
alpha = z * (double)( sTANGENT_MAP_SIZE - .5 );
|
||||
index = (int)alpha;
|
||||
alpha -= (double)index;
|
||||
|
||||
//Determine base angle based on quadrant and add or subtract table
|
||||
//value from base angle, based on quadrant
|
||||
base_angle = sLOOKUP_TABLE[ index ];
|
||||
base_angle += ( sLOOKUP_TABLE[ index + 1 ] -
|
||||
sLOOKUP_TABLE[ index ] ) * alpha;
|
||||
}
|
||||
|
||||
//Map the angle to the correct quadrant
|
||||
if( x_abs > y_abs ) // -45 to 45, or 135 to -135
|
||||
{
|
||||
if( x >= 0.0 ) // -45 to 45
|
||||
{
|
||||
if( y >= 0.0 )
|
||||
{
|
||||
angle = base_angle; // 0 to 45
|
||||
}
|
||||
else
|
||||
{
|
||||
angle = -base_angle; // -45 to 0
|
||||
}
|
||||
}
|
||||
else // 135 to 180 or -180 to -135
|
||||
{
|
||||
angle = Math.PI;
|
||||
|
||||
if( y >= 0.0 )
|
||||
{
|
||||
angle -= base_angle; // 135 to 180
|
||||
}
|
||||
else
|
||||
{
|
||||
angle = base_angle - angle; // 180 to -135
|
||||
}
|
||||
}
|
||||
}
|
||||
else // 45 to 135, or -135 to -45
|
||||
{
|
||||
if( y >= 0.0 ) //45 to 135
|
||||
{
|
||||
angle = sHALF_PI;
|
||||
|
||||
if( x >= 0.0 )
|
||||
{
|
||||
angle -= base_angle; // 45 to 90
|
||||
}
|
||||
else
|
||||
{
|
||||
angle += base_angle; // 90 to 135
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
angle = -sHALF_PI;
|
||||
|
||||
if( x >= 0.0 )
|
||||
{
|
||||
angle += base_angle; //-90 to -45
|
||||
}
|
||||
else
|
||||
{
|
||||
angle -= base_angle; //-135 to -90
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// //Check for negative radian angle value
|
||||
//
|
||||
// if( angle < 0 )
|
||||
// {
|
||||
// angle += 2 * Math.PI;
|
||||
// }
|
||||
|
||||
return angle;
|
||||
}
|
||||
}
|
||||
57
src/main/java/io/github/dsheirer/util/ColorIcon.java
Normal file
57
src/main/java/io/github/dsheirer/util/ColorIcon.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
******************************************************************************/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class ColorIcon implements Icon
|
||||
{
|
||||
private static final int sSIZE = 12;
|
||||
private static final int sARC_SIZE = 3;
|
||||
private Color mColor;
|
||||
|
||||
public ColorIcon( Color color )
|
||||
{
|
||||
mColor = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintIcon(Component c, Graphics g, int x, int y)
|
||||
{
|
||||
Color temp = g.getColor();
|
||||
|
||||
g.setColor( mColor );
|
||||
|
||||
g.fillRoundRect( x, y, sSIZE, sSIZE, sARC_SIZE, sARC_SIZE );
|
||||
|
||||
g.setColor( temp );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconWidth()
|
||||
{
|
||||
return sSIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIconHeight()
|
||||
{
|
||||
return sSIZE;
|
||||
}
|
||||
}
|
||||
54
src/main/java/io/github/dsheirer/util/MemoryUsageLogger.java
Normal file
54
src/main/java/io/github/dsheirer/util/MemoryUsageLogger.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*******************************************************************************
|
||||
* sdrtrunk
|
||||
* Copyright (C) 2014-2017 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
******************************************************************************/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import ch.qos.logback.classic.pattern.ClassicConverter;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
/**
|
||||
* Custom logging plugin for logging memory usage with logback
|
||||
*
|
||||
* Add the following to the logback.xml configuration file to use this custom converter:
|
||||
*
|
||||
* <conversionRule conversionWord="memory_usage" converterClass="MemoryUsageLogger" />
|
||||
*
|
||||
* Update the encoder pattern to include %memory_usage within the pattern
|
||||
*/
|
||||
public class MemoryUsageLogger extends ClassicConverter
|
||||
{
|
||||
@Override
|
||||
public String convert(ILoggingEvent iLoggingEvent)
|
||||
{
|
||||
//Method argument is ignored - we simply return memory usage statistics
|
||||
long allocated = Runtime.getRuntime().totalMemory();
|
||||
long free = Runtime.getRuntime().freeMemory();
|
||||
long used = allocated - free;
|
||||
|
||||
int usedPercentage = (int)((double)(used) / (double)allocated * 100.0);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("[").append(FileUtils.byteCountToDisplaySize(used).replace(" ", ""));
|
||||
sb.append("/").append(FileUtils.byteCountToDisplaySize(allocated).replace(" ", ""));
|
||||
sb.append(" ").append(usedPercentage).append("%]");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
41
src/main/java/io/github/dsheirer/util/StringUtils.java
Normal file
41
src/main/java/io/github/dsheirer/util/StringUtils.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*******************************************************************************
|
||||
* sdrtrunk
|
||||
* Copyright (C) 2014-2017 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
******************************************************************************/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
public class StringUtils
|
||||
{
|
||||
/**
|
||||
* Compares string a to b and returns true if a is non-null and non-empty, b is non-null and non-empty, and
|
||||
* a is equal to b.
|
||||
* @param a string to compare
|
||||
* @param b string to compare
|
||||
*/
|
||||
public static boolean isEqual(String a, String b)
|
||||
{
|
||||
return !isEmpty(a) && !isEmpty(b) && a.equals(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the string for null or empty condition
|
||||
*/
|
||||
public static boolean isEmpty(String a)
|
||||
{
|
||||
return a == null || a.isEmpty();
|
||||
}
|
||||
}
|
||||
54
src/main/java/io/github/dsheirer/util/ThreadPool.java
Normal file
54
src/main/java/io/github/dsheirer/util/ThreadPool.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*******************************************************************************
|
||||
* sdrtrunk
|
||||
* Copyright (C) 2014-2017 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
******************************************************************************/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import io.github.dsheirer.controller.NamingThreadFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
public class ThreadPool
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger(ThreadPool.class);
|
||||
|
||||
private static int CORES = Runtime.getRuntime().availableProcessors();
|
||||
public static ScheduledExecutorService SCHEDULED;
|
||||
|
||||
static
|
||||
{
|
||||
//Create a scheduled thread pool sized according to the available processors/cores, minimum 2
|
||||
CORES = (CORES < 2 ? 2 : CORES);
|
||||
|
||||
SCHEDULED = Executors.newScheduledThreadPool(CORES, new NamingThreadFactory("sdrtrunk"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Application-wide shared thread pools and scheduled executor service.
|
||||
*/
|
||||
public ThreadPool()
|
||||
{
|
||||
}
|
||||
|
||||
public static void logSettings()
|
||||
{
|
||||
mLog.info("Application thread pool created with [" + CORES + "] threads");
|
||||
}
|
||||
}
|
||||
118
src/main/java/io/github/dsheirer/util/TimeStamp.java
Normal file
118
src/main/java/io/github/dsheirer/util/TimeStamp.java
Normal file
@@ -0,0 +1,118 @@
|
||||
/*******************************************************************************
|
||||
* SDR Trunk
|
||||
* Copyright (C) 2014 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
******************************************************************************/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeStamp
|
||||
{
|
||||
private static SimpleDateFormat mSDFDate = new SimpleDateFormat( "yyyyMMdd" );
|
||||
private static SimpleDateFormat mSDFTime = new SimpleDateFormat( "HHmmss" );
|
||||
private static SimpleDateFormat mSDFTimeWithMilliseconds = new SimpleDateFormat( "HHmmss.SSS" );
|
||||
|
||||
/**
|
||||
* Returns the current system date formatted as yyyy-MM-dd
|
||||
*/
|
||||
public static String getFormattedDate()
|
||||
{
|
||||
return getFormattedDate( System.currentTimeMillis() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the timestamp formatted as a date of yyyy-MM-dd
|
||||
*/
|
||||
public static String getFormattedDate( long timestamp )
|
||||
{
|
||||
return mSDFDate.format( new Date( timestamp ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current system time formatted as HH:mm:ss
|
||||
*/
|
||||
public static String getFormattedTime()
|
||||
{
|
||||
return getFormattedTime( System.currentTimeMillis() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the timestamp formatted as a time of HH:mm:ss
|
||||
*/
|
||||
public static String getFormattedTime( long timestamp )
|
||||
{
|
||||
return mSDFTime.format( new Date( timestamp ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the timestamp formatted as a time of HH:mm:ss
|
||||
*/
|
||||
public static String getFormattedTimeWithMilliseconds( long timestamp )
|
||||
{
|
||||
return mSDFTimeWithMilliseconds.format( new Date( timestamp ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current system time formatted as yyyy-MM-dd*HH:mm:ss
|
||||
* with the * representing the separator attribute
|
||||
*/
|
||||
public static String getTimeStamp( String separator )
|
||||
{
|
||||
return getTimeStamp( System.currentTimeMillis(), separator );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns timestamp formatted as yyyy-MM-dd*HH:mm:ss
|
||||
* with the * representing the separator attribute
|
||||
*/
|
||||
public static String getTimeStamp( long timestamp, String separator )
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append( getFormattedDate( timestamp ) );
|
||||
sb.append( separator );
|
||||
sb.append( getFormattedTime( timestamp ) );
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current system time formatted as yyyy-MM-dd*HH:mm:ss.SSS
|
||||
* with the * representing the separator attribute
|
||||
*/
|
||||
public static String getLongTimeStamp( String separator )
|
||||
{
|
||||
return getLongTimeStamp( System.currentTimeMillis(), separator );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns timestamp formatted as yyyy-MM-dd*HH:mm:ss.SSS
|
||||
* with the * representing the separator attribute
|
||||
*/
|
||||
public static String getLongTimeStamp( long timestamp, String separator )
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append( getFormattedDate( timestamp ) );
|
||||
sb.append( separator );
|
||||
sb.append( getFormattedTimeWithMilliseconds( timestamp ) );
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
186
src/main/java/io/github/dsheirer/util/XTEA.java
Normal file
186
src/main/java/io/github/dsheirer/util/XTEA.java
Normal file
@@ -0,0 +1,186 @@
|
||||
/*******************************************************************************
|
||||
* sdrtrunk
|
||||
* Copyright (C) 2014-2016 Dennis Sheirer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
******************************************************************************/
|
||||
package io.github.dsheirer.util;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class XTEA
|
||||
{
|
||||
//Key Schedule = (sqrt(5) 1) * 2^31
|
||||
private static final int K = 0x9E3779B9;
|
||||
|
||||
//Byte mask values
|
||||
private static final int MASK0 = 0xFF;
|
||||
private static final int MASK1 = 0xFF00;
|
||||
private static final int MASK2 = 0xFF0000;
|
||||
private static final int MASK3 = 0xFF000000;
|
||||
|
||||
//Pre-calculated sub keys derived from encryption key
|
||||
private int mSubKeys1[] = new int[32];
|
||||
private int mSubKeys2[] = new int[32];
|
||||
|
||||
/**
|
||||
* XTEA block cipher encryption using 128 bit encryption key and 64 bit plain text block size with 32 Feistel rounds.
|
||||
* Shorter keys and/or plaintext values can be zero padded to the specified lengths.
|
||||
*
|
||||
* Algorithm source code adapted from: https://people.rit.edu/rab3106/CryptoReport.pdf (as accessed Sep. 2016)
|
||||
*
|
||||
* The encryption key can be changed at any point by invoking the setKey() method.
|
||||
*
|
||||
* @param key for encryption
|
||||
*/
|
||||
public XTEA(String key)
|
||||
{
|
||||
setKey(Arrays.copyOf(key.getBytes(), 16));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the key for this block cipher. Key must be 128 bits or 16 bytes. The key value can be zero padded to
|
||||
* achieve the specified key length.
|
||||
*
|
||||
* @param key encryption key to use
|
||||
*/
|
||||
public void setKey(byte[] key)
|
||||
{
|
||||
Validate.isTrue(key.length == 16);
|
||||
|
||||
int keyInts[] = new int[8];
|
||||
|
||||
//Convert the key to an array of integers
|
||||
for (int i = 0; i < 16; i += 4)
|
||||
{
|
||||
keyInts[i / 4] = ((key[i + 3] << 0)) & MASK0 | ((key[i + 2]) << 8) & MASK1 |
|
||||
((key[i + 1]) << 16) & MASK2 | ((key[i + 0]) << 24) & MASK3;
|
||||
}
|
||||
|
||||
//Calculate subkeys for each Feistel round
|
||||
int sum, cycle;
|
||||
|
||||
for (sum = 0, cycle = 0; cycle < 32; cycle++)
|
||||
{
|
||||
//Calculate round 1 subkey
|
||||
mSubKeys1[cycle] = keyInts[sum & 3];
|
||||
|
||||
//Add K to sum
|
||||
sum += K;
|
||||
|
||||
//Calculate round 2 subkey
|
||||
mSubKeys2[cycle] = keyInts[(sum >>> 11) & 3];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypts the plaintext argument. Textual byte array must be 64 bits or 8 bytes in length and can be zero padded
|
||||
* to achieve the specified length. Encrypted text byte array replaces the original plaintext byte array.
|
||||
*
|
||||
* @param plainText to encrypt
|
||||
* @return cipher text
|
||||
*/
|
||||
public byte[] encrypt(byte[] plainText)
|
||||
{
|
||||
Validate.isTrue(plainText.length == 8);
|
||||
|
||||
//Convert the 8 bytes of plaintext to 2 integers
|
||||
int result0 = (plainText[3] << 0) & MASK0 | (plainText[2] << 8) & MASK1 |
|
||||
(plainText[1] << 16) & MASK2 | (plainText[0] << 24) & MASK3;
|
||||
int result1 = (plainText[7] << 0) & MASK0 | (plainText[6] << 8) & MASK1 |
|
||||
(plainText[5] << 16) & MASK2 | (plainText[4] << 24) & MASK3;
|
||||
|
||||
//Unrolled loop for all Feistel rounds
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 0) + mSubKeys1[0];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 1) + mSubKeys2[0];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 1) + mSubKeys1[1];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 2) + mSubKeys2[1];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 2) + mSubKeys1[2];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 3) + mSubKeys2[2];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 3) + mSubKeys1[3];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 4) + mSubKeys2[3];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 4) + mSubKeys1[4];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 5) + mSubKeys2[4];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 5) + mSubKeys1[5];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 6) + mSubKeys2[5];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 6) + mSubKeys1[6];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 7) + mSubKeys2[6];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 7) + mSubKeys1[7];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 8) + mSubKeys2[7];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 8) + mSubKeys1[8];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 9) + mSubKeys2[8];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 9) + mSubKeys1[9];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 10) + mSubKeys2[9];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 10) + mSubKeys1[10];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 11) + mSubKeys2[10];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 11) + mSubKeys1[11];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 12) + mSubKeys2[11];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 12) + mSubKeys1[12];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 13) + mSubKeys2[12];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 13) + mSubKeys1[13];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 14) + mSubKeys2[13];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 14) + mSubKeys1[14];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 15) + mSubKeys2[14];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 15) + mSubKeys1[15];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 16) + mSubKeys2[15];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 16) + mSubKeys1[16];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 17) + mSubKeys2[16];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 17) + mSubKeys1[17];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 18) + mSubKeys2[17];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 18) + mSubKeys1[18];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 19) + mSubKeys2[18];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 19) + mSubKeys1[19];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 20) + mSubKeys2[19];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 20) + mSubKeys1[20];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 21) + mSubKeys2[20];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 21) + mSubKeys1[21];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 22) + mSubKeys2[21];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 22) + mSubKeys1[22];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 23) + mSubKeys2[22];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 23) + mSubKeys1[23];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 24) + mSubKeys2[23];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 24) + mSubKeys1[24];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 25) + mSubKeys2[24];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 25) + mSubKeys1[25];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 26) + mSubKeys2[25];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 26) + mSubKeys1[26];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 27) + mSubKeys2[26];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 27) + mSubKeys1[27];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 28) + mSubKeys2[27];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 28) + mSubKeys1[28];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 29) + mSubKeys2[28];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 29) + mSubKeys1[29];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 30) + mSubKeys2[29];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 30) + mSubKeys1[30];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 31) + mSubKeys2[30];
|
||||
result0 += (result1 << 4 ^ result1 >>> 5) + result1 ^ (K * 31) + mSubKeys1[31];
|
||||
result1 += (result0 << 4 ^ result0 >>> 5) + result0 ^ (K * 32) + mSubKeys2[31];
|
||||
|
||||
byte[] cipherText = new byte[8];
|
||||
|
||||
cipherText[0] = (byte)(result0 >>> 24);
|
||||
cipherText[1] = (byte)(result0 >>> 16);
|
||||
cipherText[2] = (byte)(result0 >>> 8);
|
||||
cipherText[3] = (byte)(result0 >>> 0);
|
||||
cipherText[4] = (byte)(result1 >>> 24);
|
||||
cipherText[5] = (byte)(result1 >>> 16);
|
||||
cipherText[6] = (byte)(result1 >>> 8);
|
||||
cipherText[7] = (byte)(result1 >>> 0);
|
||||
|
||||
return cipherText;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user