Java library to generate TomTom GPS poi data - java

I wonder, if there exists any Java library, which could generate poi-data for Tomtom navigation devices (usually the file has .ov2 extension).
I use Tomtom makeov2.exe util from Tomtom, but it is not stable and it seems that not longer supported.

I wasn't able to find a library that does writing, although I did find this class to read .ov2 files:
package readers;
import java.io.FileInputStream;
import java.io.IOException;
public class OV2RecordReader {
public static String[] readOV2Record(FileInputStream inputStream){
String[] record = null;
int b = -1;
try{
if ((b = inputStream.read())> -1) {
// if it is a simple POI record
if (b == 2) {
record = new String[3];
long total = readLong(inputStream);
double longitude = (double) readLong(inputStream) / 100000.0;
double latitude = (double) readLong(inputStream) / 100000.0;
byte[] r = new byte[(int) total - 13];
inputStream.read(r);
record[0] = new String(r);
record[0] = record[0].substring(0,record[0].length()-1);
record[1] = Double.toString(latitude);
record[2] = Double.toString(longitude);
}
//if it is a deleted record
else if(b == 0){
byte[] r = new byte[9];
inputStream.read(r);
}
//if it is a skipper record
else if(b == 1){
byte[] r = new byte[20];
inputStream.read(r);
}
else{
throw new IOException("wrong record type");
}
}
else{
return null;
}
}
catch(IOException e){
e.printStackTrace();
}
return record;
}
private static long readLong(FileInputStream is){
long res = 0;
try{
res = is.read();
res += is.read() <<8;
res += is.read() <<16;
res += is.read() <<24;
}
catch(IOException e){
e.printStackTrace();
}
return res;
}
}
I also found this PHP code to write the file:
<?php
$csv = file("File.csv");
$nbcsv = count($csv);
$file = "POI.ov2";
$fp = fopen($file, "w");
for ($i = 0; $i < $nbcsv; $i++) {
$table = split(",", chop($csv[$i]));
$lon = $table[0];
$lat = $table[1];
$des = $table[2];
$TT = chr(0x02).pack("V",strlen($des)+14).pack("V",round($lon*100000)).pack("V",round($lat*100000)).$des.chr(0x00);
#fwrite($fp, "$TT");
}
fclose($fp);
I'm not sure how you'd go about writing a Java class (or extending the one above) to write the file like the PHP function does, but you may be able to get some insight into how the file is encoded from it.

Related

base64_encode() from php function to Java does not return same data

I'm working on a java project that needs to have an authentication from accounts created on a local website. Those account's passwords are encrypted by a php function before inserted to the database :
public function crypter($mdp){
$j = 0;
$tmp = 0;
$key = $mdp;
$res = "";
for($i = 0; $i < strlen($mdp) ; $i++)
{
$tmp = ord($mdp[$i]) + ord($key[$i]);
if($tmp > 255){
$tmp = $tmp - 256;
}
$res[$i] = chr($tmp);
if($j == strlen($key)-1){
$j = 0;
}
else{
$j = (($j % (strlen($key))) +1 );
}
}
$res = base64_encode($res);
return $res;
}
NOTE : This function has not been written by me, so if you guys figure out what the $j variable is used for let me know. I also (obviously) can not change this code.
So I've tried to translate that function in Java which gave this :
public String cryptMdp(String mdp){
String res = "";
String key = mdp;
int j = 0;
int tmp = 0;
for (int i = 0; i < mdp.length(); i++) {
char c = mdp.charAt(i);
char c2 = key.charAt(i);
tmp = (int) c + (int) c2;
if (tmp > 255) {
tmp = tmp - 256;
}
res += (char) tmp;
if (j == key.length() - 1){
j = 0;
}
else{
j = (j % key.length()) + 1;
}
}
return Base64.getMimeEncoder().encodeToString(res.getBytes());
}
Unfortunately, those do not return the same string (ex: For the string a, php returns wg== and Java returns w4I=.), even though according to this question this is supposed to be a standard.
Can you guys see what I'm doing wrong ?
Use the existing Java functionality for Base64:
https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html
You are probably looking for this method:
encode(byte[] src)
Encodes all bytes from the specified byte array into a newly-allocated byte array using the Base64 encoding scheme.
I've been able to do what I want using URLConnection to send a HTTP request to my PHP page, like so :
public String cryptMdp(String mdp){
try {
URL url = new URL("http://myaddress/someproject/create_pwd.php?pwd=" + mdp);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputline = "";
while((inputline = in.readLine()) != null){
return inputline;
}
in.close();
} catch (MalformedURLException ex) {
System.out.println("error URL");
} catch (IOException ex) {
System.out.println("error connection");
}
return null;
}
The problem was that the PHP was using unknown characters before encoding them, which means that all of the passwords are incorrectly crypted...
EDIT :
Actually the password is crypted differently in the IDE (Netbeans) and in the builded app (.jar), so Base64.getMimeEncoder().encodeToString(res.getBytes()); actually produce the same as the PHP method...

How to create an audio wave in JavaFX?

I would like to get an Audio wave plot using Chart Area in JavaFX. Unfortunately, I am not clear how to do, what are the values ​​to be extracted from the sound to assign to x-axis and y-axis?
I tried to read other posts, but I found nothing on javafx.
You can help me?
Sample Image:
Below is the code that extract the waveform .
I'm pulling out the right parameters for my scope?
How can I use it to print the graph with JavaFX?
public class SimpleWaveformExtractor implements WaveformExtractor {
private static final int DEFAULT_BUFFER_SIZE = 32768;
#Override
public double[] extract(File inputFile) {
AudioInputStream in = null;
try {
in = AudioSystem.getAudioInputStream(inputFile);
} catch (Exception e) {
System.out.println("Cannot read audio file");
return new double[0];
}
AudioFormat format = in.getFormat();
byte[] audioBytes = readBytes(in);
int[] result = null;
if (format.getSampleSizeInBits() == 16) {
int samplesLength = audioBytes.length / 2;
result = new int[samplesLength];
if (format.isBigEndian()) {
for (int i = 0; i < samplesLength; ++i) {
byte MSB = audioBytes[i * 2];
byte LSB = audioBytes[i * 2 + 1];
result[i] = MSB << 8 | (255 & LSB);
}
} else {
for (int i = 0; i < samplesLength; i += 2) {
byte LSB = audioBytes[i * 2];
byte MSB = audioBytes[i * 2 + 1];
result[i / 2] = MSB << 8 | (255 & LSB);
}
}
} else {
int samplesLength = audioBytes.length;
result = new int[samplesLength];
if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
for (int i = 0; i < samplesLength; ++i) {
result[i] = audioBytes[i];
}
} else {
for (int i = 0; i < samplesLength; ++i) {
result[i] = audioBytes[i] - 128;
}
}
}
return ArraysHelper.normalize(result);
}
private byte[] readBytes(AudioInputStream in) {
byte[] result = new byte[0];
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
try {
int bytesRead = 0;
do {
bytesRead = in.read(buffer);
result = ArrayUtils.addAll(result, buffer);
} while (bytesRead != -1);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
this is the interface:
public interface WaveformExtractor {
double[] extract(File in);
}
This is the code that return the array of double:
private double[] extractWaveform(File file) throws IOException, UnsupportedAudioFileException {
return new WavFileExtractor().extract(file);
}

How make a app in android that shows the hardware specifications of a device

I have tried make a app in Android that show the hardware specifications of a device, example:
Processor: Quad Core 1.2 Ghz
1 GB Memory RAM
8 GB Storage
Android Version 4.4
Would someone help me to find a library that allows me do it?
You can use this code
Log.i("ManuFacturer :", Build.MANUFACTURER);
Log.i("Board : ", Build.BOARD);
Log.i("Display : ", Build.DISPLAY);
More info can be found at from http://developer.android.com/reference/android/os/Build.html
I do not know a library that can extract specific hardware specifications, however, the Facebook Device-Year-Class library can classify devices into 'years' based on Hardware specs:
Github: Device-Year-Class
Additionally, you can look through their code to detect how they get info such as the Max Freq KHz:
public static int getCPUMaxFreqKHz() {
int maxFreq = DEVICEINFO_UNKNOWN;
try {
for (int i = 0; i < getNumberOfCPUCores(); i++) {
String filename =
"/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq";
File cpuInfoMaxFreqFile = new File(filename);
if (cpuInfoMaxFreqFile.exists()) {
byte[] buffer = new byte[128];
FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);
try {
stream.read(buffer);
int endIndex = 0;
//Trim the first number out of the byte buffer.
while (Character.isDigit(buffer[endIndex]) && endIndex < buffer.length) {
endIndex++;
}
String str = new String(buffer, 0, endIndex);
Integer freqBound = Integer.parseInt(str);
if (freqBound > maxFreq) {
maxFreq = freqBound;
}
} catch (NumberFormatException e) {
//Fall through and use /proc/cpuinfo.
} finally {
stream.close();
}
}
}
if (maxFreq == DEVICEINFO_UNKNOWN) {
FileInputStream stream = new FileInputStream("/proc/cpuinfo");
try {
int freqBound = parseFileForValue("cpu MHz", stream);
freqBound *= 1000; //MHz -> kHz
if (freqBound > maxFreq) maxFreq = freqBound;
} finally {
stream.close();
}
}
} catch (IOException e) {
maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown.
}
return maxFreq;
}

External authentication in ejabberd using java

I am working on task to implement external authentication in ejabberd using java.
I searched for the examples on internet and found examples in PHP, Perl, Python but could not find any example in java.
I know the configuration that is required to be made in 'ejabberd.cfg' file.
Any code sample in java will be very helpful.
Try this:
public static void main(String[] args) {
try {
outerloop: while (true) {
byte[] lB = new byte[2];
int startPos = 0;
while (startPos < lB.length) {
int ret = System.in.read(lB, startPos,
(lB.length - startPos));
if (ret < 0) {
break outerloop;
}
startPos += ret;
}
int streamLen = System.in.available();
byte[] rd = new byte[streamLen];
startPos = 0;
while (startPos < streamLen) {
int ret = System.in.read(rd, startPos,
(streamLen - startPos));
if (ret < 0) {
break outerloop;
}
startPos += ret;
}
String inputArgs = new String(rd, "ASCII");
String[] arguments = inputArgs.split(":");
String userName = arguments[1];
String password = arguments[3];
//
// Here do the authentication
//
boolean resultOfAuthentication = // Result of Authentication;
byte[] res = new byte[4];
res[0] = 0;
res[1] = 2;
res[2] = 0;
if (resultOfAuthentication) {
res[3] = 1;
} else {
res[3] = 0;
}
System.out.write(res, 0, res.length);
System.out.flush();
}
} catch (Exception e) {
System.out.println("ERROR");
}
}

How to implement a universal file loader in Java?

This is what I'm trying to do:
public String load(String path) {
//...
}
load("file:/tmp/foo.txt"); // loads by absolute file name
load("classpath:bar.txt"); // loads from classpath
I think it's possible to do with JDK, but can't find out how exactly.
I can think of two approaches:
Just write plain Java code to extract the "scheme" from those URI-like strings, and then dispatch to the different code to load the file in different ways.
Register a custom URL stream handler to deal with the "classpath" case and then use URL.openStream() to open the stream to read the object.
The package documentation for java.net has some information about how stream handlers are discovered.
From my libraries omino roundabout, the two methods you'll need... I need them everywhere. The resource reader is relative to a class, at least to know which jar to read. But the path can start with / to force it back to the top. Enjoy!
(You'll have to make our own top level wrapper to look for "file:" and "classpath:".)
see also http://code.google.com/p/omino-roundabout/
public static String readFile(String filePath)
{
File f = new File(filePath);
if (!f.exists())
return null;
String result = "";
try
{
FileReader in = new FileReader(f);
boolean doing = true;
char[] bunch = new char[10000];
int soFar = 0;
while (doing)
{
int got = in.read(bunch, 0, bunch.length);
if (got <= 0)
doing = false;
else
{
String k = new String(bunch, 0, got);
result += k;
soFar += got;
}
}
} catch (Exception e)
{
return null;
}
// Strip off the UTF-8 front, if present. We hate this. EF BB BF
// see http://stackoverflow.com/questions/4897876/reading-utf-8-bom-marker for example.
// Mysteriously, when I read those 3 chars, they come in as 212,170,248. Fine, empirically, I'll strip that, too.
if(result != null && result.length() >= 3)
{
int c0 = result.charAt(0);
int c1 = result.charAt(1);
int c2 = result.charAt(2);
boolean leadingBom = (c0 == 0xEF && c1 == 0xBB && c2 == 0xBF);
leadingBom |= (c0 == 212 && c1 == 170 && c2 == 248);
if(leadingBom)
result = result.substring(3);
}
// And because I'm a dictator, fix up the line feeds.
result = result.replaceAll("\\r\\n", "\n");
result = result.replaceAll("\\r","\n");
return result;
}
static public String readResource(Class<?> aClass,String srcResourcePath)
{
if(aClass == null || srcResourcePath==null || srcResourcePath.length() == 0)
return null;
StringBuffer resultB = new StringBuffer();
URL resourceURL = null;
try
{
resourceURL = aClass.getResource(srcResourcePath);
}
catch(Exception e) { /* leave result null */ }
if(resourceURL == null)
return null; // sorry.
try
{
InputStream is = resourceURL.openStream();
final int BLOCKSIZE = 13007;
byte[] bytes = new byte[BLOCKSIZE];
int bytesRead = 0;
while(bytesRead >= 0)
{
bytesRead = is.read(bytes);
if(bytesRead > 0)
{
char[] chars = new char[bytesRead];
for(int i = 0; i < bytesRead; i++)
chars[i] = (char)bytes[i];
resultB.append(chars);
}
}
}
catch(IOException e)
{
return null; // sorry
}
String result = resultB.toString();
return result;
}
(edit -- removed a stray reference to OmString, to keep it standalone here.)

Categories

Resources