Getting exception when I assign large String value to Rserve in Java
public void run(String data) {//800
RConnection c =null;
long startTime = System.nanoTime();
ObjectMapper mapper = new ObjectMapper();
try {
c = new RConnection();
if (c.isConnected()) {
System.out.println("*** R serve is connected..");
} else {
System.err.println("R serve is not available");
}
System.out.println("data set length-->"+data.length());
c.assign("poleData",data);//This line is throwing error
c.eval(String.format("source(" + rScript + ")"));
REXP result =c.eval("imputResult");
System.out.println("Completed.."+imputeData.toPrettyString());
long endTime = System.nanoTime();
//System.out.println("Time end -->"+endTime);
long duration = endTime - startTime;
// System.out.println("Total time diff in nanosecond-->"+(endTime-startTime));
double diff = (double) duration / 1000000000;
// System.out.println("Total time diff -->" + diff + " second");
// c.close();
} catch (RserveException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(c!=null) {
c.close();
System.out.println("Connection closed..");
}
}
}
}
Exception
*** R serve is connected..
data set length-->58784021
java.lang.ArrayIndexOutOfBoundsException: Index 8452396 out of bounds for length 8452396
at org.rosuda.REngine.Rserve.RConnection.assign(RConnection.java:254)
at com.app.ImputaionJson$Task.run(ImputaionJson.java:190)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Connection closed..
Please help on this..
Related
I am making a game in Java and wanted to implement a deltatime system. However I am not sure if I have implemented it correctly. Is the way I have done it correct, or should I change it.
My code looks like this:
long oldtime = System.nanoTime();
while (true) {
long newtime = System.nanoTime();
long deltatime = (newtime - oldtime) / 1000000;
System.out.println(deltatime);
oldtime = newtime;
// render code
try {
Thread.sleep(Math.max(0, 32 - deltatime));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
It looks like you want to measure how long the rendering took. Therefore, I suggest a cleaner approach by storing the starting time in a variable (start) and then calculating the difference to the current time after the rendering took place. This would allow you to measure sub-steps easily by just adding another comparison to the current time in between.
Always be careful with the units (ms, µs, ms) and make it obvious by naming the variable accordingly (e.g. deltaMs) or by using a comment. It's also a good idea to protect the reference by declaring it final.
Here is a simple example:
while (true) {
final long start = System.nanoTime(); // initial reference
// simulate render code
try { Thread.sleep(32); } catch (InterruptedException e) { e.printStackTrace(); }
final long deltaMs = (System.nanoTime() - start) / 1_000_000;
System.out.println("Render took " + deltaMs + "ms");
}
Here is a nested example:
while (true) {
final long start = System.nanoTime();
/* A */ try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); }
final long deltaMsPartA = (System.nanoTime() - start) / 1_000_000;
System.out.println("Render part A took " + deltaMsPartA + "ms");
final long startPartB = System.nanoTime();
/* B */ try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); }
final long deltaMsPartB = (System.nanoTime() - startPartB) / 1_000_000;
System.out.println("Render part B took " + deltaMsPartB + "ms");
final long deltaMs = (System.nanoTime() - start) / 1_000_000;
System.out.println("Overall render took " + deltaMs + "ms");
}
my concept for download file is as below:
if the adapter restart at 7.00am,
first time download will be starting from 00:00:00 until 7am..
after 5 minute will download again, so is the download timing will be 6.58am to 7.05am.
Timezome will be different, like Asia/Singapore,Asia/Hong_Kong etc..
but this timezone need convert to GMT for some purpose.
the below is my coding
private String TIME_ZONE_ID = "America/New_York";
private final String TIME_ZONE_ID_TRKD_DEFAULT = "Etc/GMT";
public void run() {
// TODO Auto-generated method stub
if (sessionFactory.fileServer == null)
throw new NullPointerException("sessionfactory.fileServer is null");
// hsding#debug
out.println("d> 2.1");
while (true && isStarted) {
try {
if (!isConnected) {
out.println("d> 2.2");
openTransport(); // Connect
}// end if
} catch (Exception e) {
e.printStackTrace();
out.println(expMsg + " run():" + e.getMessage());
}
try {
boolean isValid = getTokenValidity();
if (isConnected) {
// boolean isValid = getTokenValidity();
out.println("Is Token Valid = [" + isValid + "]");
if (isValid) {
try {
for (int i = 0; i < garrNewsFilterCriteria.length; i++) {
giCurrSubscribeSeqNo = i;
// add in keep track of last download time. hsding#debug#2016041x
if (garrLastNewsSubscribeStartTime[i] == null) {
out.println("garrLastNewsSubscribeStartTime= " + garrLastNewsSubscribeStartTime[i]);
garrLastNewsSubscribeStartTime[i] = getStartDateTime(true);
} else {
out.println("garrLastNewsSubscribeStartTime2= " + garrLastNewsSubscribeStartTime[i]);
garrLastNewsSubscribeStartTime[i] = getStartDateTime();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
out.println("Token not valid!!!");
isConnected = false;
}
} else
isConnected = false;
} catch (Exception e) {
e.printStackTrace();
// TODO Auto-generated catch block
out.println(expMsg + " run():" + e.getMessage());
}
}
}
private XMLGregorianCalendar getStartDateTime() {
return getStartDateTime(false);
}
private XMLGregorianCalendar getStartDateTime(boolean startFromZeroHour) {
XMLGregorianCalendar xmlCal = null;
try {
xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
// hsding#debug#20160419. enhance on the retrieve time FROM and TO.
// Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Canada/Eastern"));
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("Asia/Singapore"));
out.println("D> now 0=" + now);
if (startFromZeroHour) {
// ??????
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
} else {
now.set(Calendar.SECOND, 0); // pre offset the second to zero for easy the polling job in future.
int fallback_prev_N_second = (int) (SLEEP_TIME * 2) / 1000 * -1;
now.add(Calendar.SECOND, fallback_prev_N_second);
}
out.println("D> now 1=" + now);
now.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_ID_TRKD_DEFAULT));
// hsding#debug#2016041x
out.println("D> now 2=" + now);
xmlCal.setDay(now.get(Calendar.DAY_OF_MONTH));
xmlCal.setMonth(now.get(Calendar.MONTH) + 1);
xmlCal.setYear(now.get(Calendar.YEAR));
xmlCal.setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
out.println("D> xmlCal=" + xmlCal);
}
private XMLGregorianCalendar getEndDateTime() {
XMLGregorianCalendar xmlCal = null;
int iDay = 0;
int iMonth = 0;
try {
xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
// out.println("News Retrieve [End Time] =" + xmlCal.toGregorianCalendar().getTime());
// hsding#debug#20160419. enhance on the retrieve time FROM and TO.
// Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Canada/Eastern"));
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("Asia/Singapore"));
out.println("D> End time =" + now);
now.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_ID_TRKD_DEFAULT));
out.println("D> End time2 =" + now);
xmlCal.setDay(now.get(Calendar.DAY_OF_MONTH));
// xmlCal.setDay(12);
// We do a +1 below because XMLGregorianCalendar goes from 1 to 12
// while Calendar.MONTH goes from 0 to 11 !!!
xmlCal.setMonth(now.get(Calendar.MONTH) + 1);
// xmlCal.setMonth(1);
xmlCal.setYear(now.get(Calendar.YEAR));
xmlCal.setTime(now.get(Calendar.HOUR), now.get(Calendar.MINUTE), 59);
out.println("D> End time xmlcal =" + xmlCal);
return xmlCal;
}
private void getNewsHeaderFromReuters(String sParam01, String sParam02, XMLGregorianCalendar xmlParam03) throws Exception {
try{
RetrieveHeadlineMLRequest1 headlineRequest = new RetrieveHeadlineMLRequest1();
NewsRequest newsRequest = new NewsRequest();
Filter filter = new Filter();
MetaDataConstraint constraint = new MetaDataConstraint();
newsRequest.setStartTime(xmlParam03);
newsRequest.setEndTime(getEndDateTime());
out.println("News Retrieve [START Time]=" + newsRequest.getStartTime());
out.println("News Retrieve [END Time]=" + newsRequest.getEndTime());
headlineRequest.setHeadlineMLRequest(newsRequest);
RetrieveHeadlineMLResponse1 headlineResponse;
int i = 0;
try {
String sNewsID = "";
boolean isProcess = true;
headlineResponse = newsPort.retrieveHeadlineML1(headlineRequest);
out.println("SOAP request : " + messageDumper.nextMessage());
out.println("SOAP response : " + messageDumper.nextMessage());
} catch (Exception e) {
e.printStackTrace();
out.println(expMsg + " getNewsHeaderFromReuters()" + e.getMessage());
isConnected = false;
throw new Exception(e.getMessage());
}
}
i use example for present my problem:
When the adapter restart on 2017-02-18 7am on Singapore timing, After convert to GMT Start time is 2017-02-18 00:00:00, but end time is 2017-02-17 11:50:34.
this is wrong de..
When the adapter restart after 2017-02-18 8 am on Singapore timing, After convert to GMT Start time is 2017-02-18 00:00:00, but end time is 2017-02-18 00:05:34( up to the timing convert to GMT). but it is correct..
if i no convert to GMT, all timing is correct , but i don't wan this answer cause it will affected some data, so it need convert to GMT first.
Please kindly advice what wrong for my coding...
Thanks
Regards
Sharon
I am developing a speed test app like OKLA app (http://www.speedtest.net/).
I've been trying to get bandwidth rate with the most common approach:
Get the time before downloading.
Download some file for some time X.
Get the time after downloading and the total size downloaded.
Calculate speed from TIME and BYTES RECEIVED.
Also, I execute this in two different threads at the same time because is required to saturate de connection to achieve good results.
This approach works very well on PC environment with this JAVA code:
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class TestVelocidad {
static long totalBytesReceived = 0; //
static long startSample;
static long endSample ;
private static final long TIME_FOR_DOWNLOAD_MILISECONDS = (long) 10000.0;
private static final long MILI_TO_NANO = 1000000;
public static void main(String[] args) throws InterruptedException, ExecutionException {
try{
final ExecutorService service;
String downloadFileUrl100MB = "http://cachefly.cachefly.net/100mb.test";
startSample = System.nanoTime();
service = Executors.newFixedThreadPool(6);
FutureTask futureTask_1 = new FutureTask(new SpeedTestThread(downloadFileUrl100MB));
service.execute(futureTask_1);
FutureTask futureTask_2 = new FutureTask(new SpeedTestThread(downloadFileUrl100MB));
service.execute(futureTask_2);
service.shutdownNow();
long result1 = (Long) futureTask_1.get();
long result2 = (Long) futureTask_2.get();
endSample = System.nanoTime();
long timeSpent = (long) endSample-startSample;
long totalBytesReceived = result1 + result2;
System.out.println("Time of threads: " + timeSpent/1000000000.0 + " seconds " + "\nbytes received: " + (totalBytesReceived) );
double calculatedSpeed;
// long finalTimeSpent ;
// finalTimeSpent = (long) ((TIME_FOR_DOWNLOAD_MILISECONDS * MILI_TO_NANO - diff));
calculatedSpeed = SpeedInfo.calculate(timeSpent, totalBytesReceived).megabits;
System.out.println("Velocidad calculada: " + calculatedSpeed + " mbps" );
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class SpeedTestThread implements Callable<Long> {
private String url = new String("");
private static final long TIME_FOR_DOWNLOAD_NANOSECONDS = (long) 10000000000.0;
private static final long MILI_TO_NANO = 1000000;
private long bytesThread;
public SpeedTestThread(String urlToDownload){
url = urlToDownload;
}
public void run() {
}
#Override
public Long call() throws Exception {
System.out.println("FileDownload " + " File to download: " + url );
InputStream stream = null;
long startCon = System.nanoTime();
URL urlToDownload = null;
try {
urlToDownload = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection con = null;
try {
con = urlToDownload.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
con.setUseCaches(false);
//Tiempo de acceso al archivo.
long connectionLatency = (System.nanoTime() - startCon)/MILI_TO_NANO;
System.out.println("Connection latency = " + connectionLatency + "");
con.setConnectTimeout(5000);
try {
stream = con.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
long startNano = System.nanoTime();
int currentByte = 0;
try {
while ((currentByte = stream.read()) != -1 ) {
bytesThread++;
if ((System.nanoTime() - startNano) > TIME_FOR_DOWNLOAD_NANOSECONDS){
System.out.println("Time");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Thread bytes received: " + bytesThread);
return bytesThread;
}
}
class SpeedInfo {
public double kilobits = 0;
public double megabits = 0;
public double downspeed = 0;
private static final double BYTE_TO_KILOBIT = 0.008;
private static final double KILOBIT_TO_MEGABIT = 0.001;
/**
* 1 byte = 0.0078125 kilobits
* 1 kilobits = 0.0009765625 megabit
*
* #param downloadTime in miliseconds
* #param bytesIn number of bytes downloaded
* #return SpeedInfo containing current testVelocidadThread
*/
public static SpeedInfo calculate(final long downloadTime, final long bytesIn) {
SpeedInfo info = new SpeedInfo();
//from mil to sec
System.out.println("Bytes transferidos: " + bytesIn + "Tiempo de descarga: " + downloadTime/1000000000);
double time = downloadTime;
double byteIn1 = bytesIn;
double division = (double)(byteIn1 / time);
double bytespersecond = ((division) * 1000000000);
double kilobits = bytespersecond * BYTE_TO_KILOBIT;
double megabits = kilobits * KILOBIT_TO_MEGABIT;
info.downspeed = bytespersecond;
info.kilobits = kilobits;
info.megabits = megabits;
return info;
}
}
The problem is when I run this on a Android application, I had good results on phones with more processing and memory capacity, but poor results on phones with lower capacity.
Any good ideas to achieve good results on most android's phones?.
try to download the file with java nio rather than java io
java io transfer the file first to memory which make the performance poor on low end devices
while java nio using channels you can transfer the file to storage which will make the performance same on all devices approximately
use this code :
len = out.getChannel().transferFrom(readableByteChannel , seekPos , Long.MAX_VALUE);
I am looking for possible methods that can increase download speed and improve cpu, memory performance. Currently I am downloading file in segments and transferring data using java nio transferFrom function.
public void startDownload() {
threadService.execute(() -> {
double currentBytes = bytesDone.doubleValue();
//Download each segment independently.
for (int i = 0; i < segments; i++) {
if (intialState[i] != -1) {
threadService.execute(new Segment((i * sizeOfEachSegment)
+ intialState[i], (i + 1) * sizeOfEachSegment, i));
}
}
if (intialState[segments] != -1) {
threadService.execute(new Segment((segments * sizeOfEachSegment)
+ intialState[segments], sizeofFile, segments));
}
// Keep saving states of threads. And updating speed.
while (bytesDone.get() < sizeofFile) {
for (int i = 0; i < 1; i++) {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
System.out.println("thread interupted while sleeping");
}
System.out.println(speed
= (int) ((bytesDone.doubleValue() - currentBytes) / 5120));
currentBytes = bytesDone.doubleValue();
avgSpeed[0] += speed;
avgSpeed[1]++;
}
states.saveState(stateArray, currentState);
}
// Download Complete.
try {
fileChannel.close();
file.close();
} catch (IOException ex) {
System.out.println("failed to close file");
}
currentState.set(2);
states.saveState(stateArray, currentState);
System.out.println("Alhamdullilah Done :)");
System.out.println("Average Speed : " + avgSpeed[0] / avgSpeed[1]);
});
}
public class Segment implements Runnable {
long start;
long end;
long delta;
int name;
public Segment(long start, long end, int name) {
this.start = start;
this.end = end;
this.name = name;
}
#Override
public void run() {
try {
HttpGet get = new HttpGet(uri);
// Range header for defining which segment of file we want to receive.
String byteRange = start + "-" + end;
get.setHeader("Range", "bytes=" + byteRange);
try (CloseableHttpResponse response = client.execute(get)) {
ReadableByteChannel inputChannel = Channels.newChannel(
response.getEntity().getContent());
while (start < end && currentState.get() == 1) {
delta = fileChannel.transferFrom(inputChannel, start, 8192);
start += delta;
bytesDone.addAndGet(delta);
stateArray.set(name, start);
}
stateArray.set(name, -1);
}
System.out.println("Thread done: " + name);
} catch (IOException ex) {
System.out.println("thread " + name + " failed to download");
}
}
}
This implementation gives 400+ kb/s but Internet Download Manager downloads same file at 500+ kb/s.
Are there any resources I can reuse(I noticed every connection initially takes time to reach its maximum speed so is there any way i can reuse the same thread to download the next portion of file as soon as it complete downloading previous)?
I have this program, sending request to receive weather information from the web in JSON format. I will sending one request per city in a for loop. However after the first JSON is received and parsed whenever it tries to parse the second one it gives end of file error and does not write the information into the database. What might be the problem ?
public static void getHistory()
{
Connection connection = connectToDatabase();; // manages connection
System.out.println("Getting History Data");
//Loops through every single city and requests, parses and inserts the history weather data.
PreparedStatement pStatement = null;
Scanner scanner = new Scanner(System.in);
for(int i = 35 ; 0 < cities.length ; i++)
{
int errorCount = 0;
int userInput;
System.out.printf("City no : %d. 1 to continue\n", i+1);
JSONParser parser = new JSONParser();
JSONObject jsonObj = new JSONObject();
Object obj = new Object();
String resultString = new String();
userInput = scanner.nextInt();
if(userInput != 1)
{
System.out.println("Terminating the program!");
break;
}else if(userInput == 1)
{
System.out.println("Continue...");
}
System.out.println("For Loop");
System.out.printf("!%d: %s!\n", i, cities[i]);
resultString = sendRequestForHistoryData(cities[i]);
try
{
obj = parser.parse(resultString);
jsonObj = (JSONObject)obj;
parseHistoryData(jsonObj);
}
catch(ParseException pe)
{
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
//Iterates through all the WeatherData objects and writes them into the database.
Iterator<WeatherData> vectorIterator = parsedWeatherObjects.iterator();
System.out.printf("Vector Length: %d\n", parsedWeatherObjects.size());
Statement statement = null;
while(vectorIterator.hasNext())
{
WeatherData weather = vectorIterator.next();
/*
* Table Structure for HistoryData Table in SQL Server
* 1) City_ID - int
* 2)City_Name - nvarchar(50)
* 3)Time - datetime
* 4)Received_Time - datetime
* 5)Humidity - float
* 6)Rain_info - float
* 7)Snow_info - float
* 8)Temperature - float
* 9)Min_Temperature - float
* 10)Max_Temperature - float
* 11)Description - nvarchar(50)
*/
try
{
String statementString = "INSERT INTO WeatherHistory("
+"City_ID,"
+"City_Name,"
+"Time,"
+"Received_Time,"
+"Humidity,"
+"Rain_Info,"
+"Snow_Info,"
+"Temperature,"
+"Min_Temperature,"
+"Max_Temperature,"
+"Description)"
+"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
pStatement = connection.prepareStatement(statementString);
java.sql.Date sqlDate = new java.sql.Date(weather.getDate());
java.sql.Date currentSqlDate = new java.sql.Date(currentTimestamp.getTime());
//java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(weather.getDate());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2"));
String formattedDate = convertEpochToDateString(weather.getDate());
//System.out.printf("Formatted Date: " + formattedDate + "\n");
java.sql.Timestamp sqlTimestamp = Timestamp.valueOf(formattedDate);
pStatement.setInt(1, weather.getCityCode());
pStatement.setString(2, weather.getCity());
pStatement.setTimestamp(3, sqlTimestamp, cal);
pStatement.setDate(4, currentSqlDate);
pStatement.setFloat(5, weather.getHumidity());
pStatement.setFloat(6, weather.getRainInfo());
pStatement.setFloat(7, weather.getSnowInfo());
pStatement.setFloat(8, weather.getTemperature());
pStatement.setFloat(9, weather.getMinTemperature());
pStatement.setFloat(10, weather.getMaxTemperature());
pStatement.setString(11, weather.getDescription());
pStatement.execute();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
//e.printStackTrace();
//System.out.println("SQL Key error.");
errorCount++;
}
}//While Loop End
System.out.printf("SQL Error Count: %d\n", errorCount);
try
{
if (pStatement != null)
{
pStatement.close();
System.out.println("Statement close.");
}
}catch (SQLException ex)
{
// TODO Auto-generated catch block
ex.printStackTrace();
}
}//For Loop End
try
{
if (connection != null)
{
connection.close();
System.out.println("Connection close.");
}
} catch (SQLException ex)
{
// TODO Auto-generated catch block
ex.printStackTrace();
}
}//Function End