/*
* 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; version 3 of the License.
*
* 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.
*
* Author: Damian Waradzyn
*/
package com.mapmidlet.misc;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Image;
import com.mapmidlet.CloudGps;
import com.mapmidlet.options.Options;
public class IOTool {
public static String NMEA_LOGS_DIR = "GpsLogs";
private static Worker ioWorker = new Worker("I/O worker", false, 64, 1);
public static Image readFile(String fileUrl) throws IOException {
FileConnection fileConn = (FileConnection) Connector.open(fileUrl, Connector.READ);
if (!fileConn.exists()) {
return null;
}
return Image.createImage(fileConn.openInputStream());
}
public static Image downloadAndSaveImage(String url, final String fileUrl) throws IOException {
Image result = null;
// long beforeRequest = System.currentTimeMillis();
HttpConnection http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
Options options = Options.getInstance();
http
.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12 (.NET CLR 3.5.30729");
int responseCode = http.getResponseCode();
if (responseCode == HttpConnection.HTTP_OK) {
try {
InputStream iStrm = http.openInputStream();
int length = (int) http.getLength();
if (length > 0) {
final byte imageData[] = new byte[length];
int position = 0;
// long beforeDownload = System.currentTimeMillis();
int read = 0;
while (position < length && read != -1) {
read = iStrm.read(imageData, position, length - position);
position += read;
options.downloaded += read;
}
iStrm.close();
// long beforeImageCreation = System.currentTimeMillis();
result = Image.createImage(imageData, 0, length);
// long beforeFileSave = System.currentTimeMillis();
if (Options.getInstance().useFileApi) {
Runnable saveFile = new Runnable() {
public void run() {
FileConnection fileConn;
try {
fileConn = (FileConnection) Connector.open(fileUrl, Connector.READ_WRITE);
if (!fileConn.exists()) {
fileConn.create();
}
DataOutputStream outputStream = fileConn.openDataOutputStream();
outputStream.write(imageData);
outputStream.close();
fileConn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
ioWorker.addTask(saveFile);
}
// long beforePrintln = System.currentTimeMillis();
// System.out.println(Thread.currentThread().getName() +
// ": http get = "
// + (beforeDownload - beforeRequest) + " ms, download = "
// + (beforeImageCreation - beforeDownload) +
// " ms, image creation = "
// + (beforeFileSave - beforeImageCreation) +
// " ms, file save = "
// + (beforePrintln - beforeFileSave));
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("HTTP GET (" + url + ") failed with code " + responseCode);
}
return result;
}
public static String download(String url) throws IOException {
ContentConnection conn = (ContentConnection) Connector.open(url);
return new String(readStream(conn.openDataInputStream()));
}
private static byte[] readStream(InputStream inputStream) throws IOException {
byte[] b = new byte[2048];
Options options = Options.getInstance();
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int c;
do {
c = inputStream.read(b);
if (c > 0) {
outputStream.write(b, 0, c);
options.downloaded += c;
}
} while (c != -1);
return outputStream.toByteArray();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
public static void ensureDirExist(String dir) {
Options options = Options.getInstance();
if (!options.useFileApi) {
return;
}
String url = ("file:///" + options.rootName + dir);
try {
FileConnection conn = (FileConnection) Connector.open(url, Connector.READ_WRITE);
if (!conn.exists()) {
conn.mkdir();
} else if (!conn.isDirectory()) {
CloudGps.setError(url + " exists but is not a directory. Saving tiles will fail.");
}
} catch (IOException e) {
CloudGps.setError(e);
}
}
public static DataOutputStream createLogFile() {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
String fileName = "GpsLog_" + cal.get(Calendar.YEAR) + "-" + prefixWithZero(month) + "-" + prefixWithZero(day)
+ "_" + prefixWithZero(hour) + "." + prefixWithZero(minute) + "." + prefixWithZero(second) + ".txt";
try {
FileConnection fileConn = (FileConnection) Connector.open("file:///" + Options.getInstance().rootName
+ IOTool.NMEA_LOGS_DIR + "/" + fileName, Connector.WRITE);
fileConn.create();
return fileConn.openDataOutputStream();
} catch (IOException e) {
CloudGps.setError(e);
}
return null;
}
private static String prefixWithZero(int i) {
return i < 10 ? "0" + i : "" + i;
}
public static String[] listDir(String dirUrl) throws IOException {
FileConnection fileConn = (FileConnection) Connector.open(dirUrl, Connector.READ);
Enumeration list = fileConn.list();
Vector files = new Vector();
while (list.hasMoreElements()) {
files.addElement(list.nextElement());
}
String[] result = new String[files.size()];
files.copyInto(result);
return result;
}
public static String getDefaultNmeaLogsDir() {
return "file:///" + Options.getInstance().rootName + IOTool.NMEA_LOGS_DIR;
}
}