FYI SIGBUS java.io.BufferedWriter.flushBuffer()V
Michael Hall
mik3hall at gmail.com
Mon Oct 17 03:54:55 PDT 2011
On Oct 17, 2011, at 5:41 AM, Michael McMahon wrote:
> What was the nature of the bug you fixed? Was there native
> code involved?
No native. I think I was just throwing prior exceptions before reaching the flush/close.
Earlier version of the following code. I was taking similar crashes on the use of Calendar in here that I did bug report although it seemed to duplicate another issue. Mike Swingler indicated that might be a 32 bit build compiler edge case issue? Fix that might fix this. Mostly I work/test now with JDK 6 on this code for that reason.
public static void main(String[] args) {
String symbol = args[0];
String dataFormat="&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
String yqlURL = "http://query.yahooapis.com/v1/public/yql?q=";
String outputFileName;
BufferedWriter writer;
if (args.length > 1) outputFileName = args[1];
else if (args[0].equals("%5EDJI")) outputFileName = "dow.csv";
else outputFileName = "sp.csv";
try {
writer = new BufferedWriter(new FileWriter(outputFileName,true));
}
catch (IOException ioex) {
ioex.printStackTrace();
return;
}
/*Get the start and end date into strings to build the query*/
Calendar now = Calendar.getInstance();
Calendar start = Calendar.getInstance();
start.set(now.get(Calendar.YEAR),0,1);
String startDate = new String(new SimpleDateFormat("yyyy-MM-dd").format(start.getTime()));
String endDate = new String(new SimpleDateFormat("MM-dd-yyyy").format(now.getTime()));
String trailing = "%22&format=json&env=http://datatables.org/alltables.env";
StringBuilder sb = new StringBuilder(yqlURL);
sb.append("select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22");
sb.append(symbol);
sb.append("%22)%20and%20startDate=%22");
int prefixLength = sb.length();
char[] writeBuffer = null;
StringBuilder csv = new StringBuilder("Date,Open,High,Low,Close,Volume,Adj Close").append(LS);
try {
writer.write(csv.toString());
csv.setLength(0);
}
catch (IOException ioex) { ioex.printStackTrace(); }
sb.append(startDate);
sb.append("%22%20and%20endDate=%22");
sb.append(endDate);
sb.append(trailing);
JSONObject json = RestClient.connect(sb.toString());
if (json != null) {
try {
json = json.getJSONObject("query");
json = json.getJSONObject("results");
JSONArray quote = json.getJSONArray("quote");
for (int i=0;i<quote.length();i++) {
JSONObject row = (JSONObject)quote.get(i);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse(row.getString("Date"));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek != FRIDAY) continue;
addCSVRow(csv,row);
}
if (writeBuffer == null || writeBuffer.length < csv.length())
writeBuffer = new char[csv.length()];
csv.getChars(0,csv.length(),writeBuffer,0);
writer.write(writeBuffer,0,csv.length());
}
catch (JSONException jex) { jex.printStackTrace(); }
catch (IOException ioex) { ioex.printStackTrace(); }
catch (java.text.ParseException pex) { pex.printStackTrace(); }
}
try {
writer.flush();
writer.close();
}
catch (IOException ioex) { ioex.printStackTrace(); }
}
More information about the macosx-port-dev
mailing list