156 private String filename;
158 private void readMatrices(StreamTokenizer st,
int r,
int k,
int dim)
throws IOException, NumberFormatException {
161 genMat =
new int[
dim * k][r];
162 for (
int i = 0; i <
dim; i++)
163 for (
int c = 0; c < k; c++) {
164 for (
int j = 0; j < r; j++) {
166 genMat[i * numCols + c][j] = (int) st.nval;
169 for (
int j = r; j < numRows; j++) {
175 void readData(StreamTokenizer st)
throws IOException, NumberFormatException {
177 st.eolIsSignificant(
false);
178 st.slashSlashComments(
true);
179 int i = st.nextToken();
180 if (i != StreamTokenizer.TT_NUMBER)
181 throw new NumberFormatException(
" readData: cannot read base");
184 numCols = (int) st.nval;
186 numRows = (int) st.nval;
192 throw new IllegalArgumentException(
" dimension dim <= 0");
195 static BufferedReader openURL(String filename)
throws MalformedURLException, IOException {
197 URL url =
new URL(filename);
198 BufferedReader input =
new BufferedReader(
new InputStreamReader(url.openStream()));
201 }
catch (MalformedURLException e) {
202 System.err.println(e +
" Invalid URL address: " + filename);
205 }
catch (IOException e) {
207 System.err.println(e +
" in openURL with " + filename);
212 static BufferedReader openFile(String filename)
throws IOException {
214 BufferedReader input;
215 File f =
new File(filename);
220 throw new IOException(filename +
" is a directory");
221 input =
new BufferedReader(
new FileReader(filename));
223 String pseudo =
"umontreal/ssj/hups/data/";
224 StringBuffer pathname =
new StringBuffer(pseudo);
225 for (
int ci = 0; ci < filename.length(); ci++) {
226 char ch = filename.charAt(ci);
227 if (ch == File.separatorChar)
228 pathname.append(
'/');
232 InputStream dataInput =
DigitalNetFromFile.class.getClassLoader().getResourceAsStream(pathname.toString());
233 if (dataInput ==
null)
234 throw new FileNotFoundException();
235 input =
new BufferedReader(
new InputStreamReader(dataInput));
239 }
catch (FileNotFoundException e) {
240 System.err.println(e +
" *** cannot find " + filename);
243 }
catch (IOException e) {
246 System.err.println(e +
" cannot read from " + filename);
272 public DigitalNetFromFile(String filename,
int r1,
int w,
int s1)
throws MalformedURLException, IOException {
274 BufferedReader input =
null;
275 StreamTokenizer st =
null;
277 if (filename.startsWith(
"http:") || filename.startsWith(
"ftp:"))
278 input = openURL(filename);
280 input = openFile(filename);
281 st =
new StreamTokenizer(input);
284 }
catch (MalformedURLException e) {
285 System.err.println(
" Invalid URL address: " + filename);
287 }
catch (FileNotFoundException e) {
288 System.err.println(
" Cannot find " + filename);
290 }
catch (NumberFormatException e) {
291 System.err.println(
" Cannot read number from " + filename);
293 }
catch (IOException e) {
294 System.err.println(
" IOException: " + filename);
299 System.err.println(
" base = 2, use DigitalNetBase2FromFile");
300 throw new IllegalArgumentException(
"base = 2, use DigitalNetBase2FromFile");
302 if ((
double) numCols * Math.log((
double) b) > (31.0 * Math.log(2.0)))
303 throw new IllegalArgumentException(
"DigitalNetFromFile: too many points" +
PrintfFormat.
NEWLINE);
305 throw new IllegalArgumentException(
308 throw new IllegalArgumentException(
315 throw new IllegalArgumentException(
319 readMatrices(st, r1, numCols, s1);
320 }
catch (NumberFormatException e) {
321 System.err.println(e +
" cannot read matrices from " + filename);
323 }
catch (IOException e) {
324 System.err.println(e +
" cannot read matrices from " + filename);
329 this.filename = filename;
334 for (
int i = 1; i < numCols; i++)
337 System.out.println(
"DigitalNetFromFile: numPoints != b^k");
338 throw new IllegalArgumentException(
" numPoints != b^k");
342 normFactor = 1.0 / Math.pow((
double) b, (
double) outDigits);
343 double invb = 1.0 / b;
344 factor =
new double[outDigits];
346 for (
int j = 1; j < outDigits; j++)
347 factor[j] = factor[j - 1] * invb;
358 this(filename, -1, -1, s);
367 sb.append(super.toString());
368 return sb.toString();
377 StringBuffer sb =
new StringBuffer(
toString());
380 for (
int i = 0; i <
dim; i++) {
382 for (
int c = 0; c < numCols; c++) {
383 for (
int r = 0; r < numRows; r++)
384 sb.append(genMat[i * numCols + c][r] +
" ");
388 return sb.toString();
395 public int compare(Object o1, Object o2) {
402 if (net1.filename.indexOf(
"_") >= 0 && net2.filename.indexOf(
"_") < 0)
404 if (net2.filename.indexOf(
"_") >= 0 && net1.filename.indexOf(
"_") < 0)
410 if (net1.numRows < net2.numRows)
412 if (net1.numRows > net2.numRows)
418 private static List getListDir(String dirname)
throws IOException {
420 String pseudo =
"umontreal/ssj/hups/data/";
421 StringBuffer pathname =
new StringBuffer(pseudo);
422 for (
int ci = 0; ci < dirname.length(); ci++) {
423 char ch = dirname.charAt(ci);
424 if (ch == File.separatorChar)
425 pathname.append(
'/');
430 File dir =
new File(url.getPath());
431 if (!dir.isDirectory())
432 throw new IllegalArgumentException(dirname +
" is not a directory");
433 File[] files = dir.listFiles();
434 List alist =
new ArrayList(200);
435 if (!dirname.endsWith(File.separator))
436 dirname += File.separator;
437 for (
int i = 0; i < files.length; i++) {
438 if (files[i].isDirectory())
440 if (files[i].getName().endsWith(
"gz") || files[i].getName().endsWith(
"zip"))
443 BufferedReader input = net.openFile(dirname + files[i].getName());
444 StreamTokenizer st =
new StreamTokenizer(input);
446 net.filename = files[i].getName();
449 if (alist !=
null && !files[0].isDirectory())
450 Collections.sort(alist,
new NetComparator());
453 }
catch (NullPointerException e) {
454 System.err.println(
"getListDir: cannot find directory " + dirname);
457 }
catch (NumberFormatException e) {
458 System.err.println(e +
"*** cannot read number ");
461 }
catch (IOException e) {
462 System.err.println(e);
467 private static String listFiles(String dirname) {
469 String pseudo =
"umontreal/ssj/hups/data/";
470 StringBuffer pathname =
new StringBuffer(pseudo);
471 for (
int ci = 0; ci < dirname.length(); ci++) {
472 char ch = dirname.charAt(ci);
473 if (ch == File.separatorChar)
474 pathname.append(
'/');
479 File dir =
new File(url.getPath());
480 File[] list = dir.listFiles();
481 List alist =
new ArrayList(200);
483 StringBuffer sb =
new StringBuffer(1000);
484 for (
int i = 0; i < list.length; i++) {
485 if (list[i].isDirectory()) {
486 sb.append(PrintfFormat.s(-2, list[i].getName()));
487 sb.append(File.separator + PrintfFormat.NEWLINE);
489 sb.append(PrintfFormat.s(-25, list[i].getName()));
491 sb.append(PrintfFormat.NEWLINE);
494 if (list.length % NPRI > 0)
495 sb.append(PrintfFormat.NEWLINE);
496 return sb.toString();
498 }
catch (NullPointerException e) {
499 System.err.println(
"listFiles: cannot find directory " + dirname);
511 public static String
listDir(String dirname)
throws IOException {
513 List list = getListDir(dirname);
514 if (list ==
null || list.size() == 0)
515 return listFiles(dirname);
516 StringBuffer sb =
new StringBuffer(1000);
523 for (
int i = 0; i < list.size(); i++) {
528 "----------------------------------------------------------------------" +
PrintfFormat.
NEWLINE);
535 return sb.toString();
537 }
catch (NullPointerException e) {
538 System.err.println(
"formatPlain: cannot find directory " + dirname);
551 public static void listDirHTML(String dirname, String filename)
throws IOException {
552 String list =
listDir(dirname);
553 StreamTokenizer st =
new StreamTokenizer(
new StringReader(list));
554 st.eolIsSignificant(
true);
555 st.ordinaryChar(
'/');
556 st.ordinaryChar(
'_');
557 st.ordinaryChar(
'-');
558 st.wordChars(
'-',
'-');
559 st.wordChars(
'_',
'_');
560 st.slashSlashComments(
false);
561 st.slashStarComments(
false);
562 PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter(filename)));
564 while (st.nextToken() != st.TT_EOL)
568 out.println(
"<body>");
569 out.println(
"<table border>");
570 out.println(
"<caption> Directory: " + dirname +
"</caption>");
573 while (st.sval.compareTo(
"File") != 0)
575 out.print(
"<tr align=center><th>" + st.sval +
"</th>");
576 while (st.nextToken() != st.TT_EOL) {
577 out.print(
"<th>" + st.sval +
"</th>");
580 while (st.nextToken() != st.TT_EOF) {
582 case StreamTokenizer.TT_EOL:
583 out.println(
"</tr>");
585 case StreamTokenizer.TT_NUMBER:
586 out.print(
"<td>" + (
int) st.nval +
"</td>");
588 case StreamTokenizer.TT_WORD:
589 if (st.sval.indexOf(
"---") >= 0) {
593 out.print(
"<tr align=center><td>" + st.sval +
"</td>");
601 out.println(
"</table>");