Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
95 Kevin 1
// WARNING: This program uses the Assertion class. When it is run,
2
// assertions must be turned on. For example, under Linux, use:
3
// java -ea Genfile
4
 
5
/** Generate a data file. The size is a multiple of 4096 bytes.
6
    The records are short ints, with each record having a value
7
    less than 30,000.
8
*/
9
 
10
import java.io.*;
11
import java.util.*;
12
import java.math.*;
13
 
14
public class Genfile2 {
15
 
16
static final int BlockSize = 4096;
17
static final int NumRecs = 2048; // Because they are short ints
18
 
19
/** Initialize the random variable */
20
static private Random value = new Random(); // Hold the Random class object
21
 
22
static int random(int n) {
23
    return Math.abs(value.nextInt()) % n;
24
}
25
 
26
public static void main(String args[]) throws IOException {
27
 
28
    assert (args.length == 3) && (args[0].charAt(0) == '-') :
29
         "\nUsage: Genfile <option> <filename> <size>" +
30
	 "\nOptions ust be '-a' for ASCII, or '-b' for binary." +
31
	 "\nSize is measured in blocks of 4096 bytes";
32
 
33
  int filesize = Integer.parseInt(args[2]); // Size of file in blocks
34
  DataOutputStream file = new DataOutputStream(
35
      new BufferedOutputStream(new FileOutputStream(args[1])));
36
 
37
  int recs = NumRecs * filesize;
38
 
39
  if (args[0].charAt(1) == 'b') // Write out random numbers
40
      for (int i=0; i<filesize; i++)
41
	  for (int j=0; j<NumRecs; j++) {
42
	      short val = (short)(random(29999) + 1);
43
	      file.writeShort(val);
44
	  }
45
  else if (args[0].charAt(1) == 'a') // Write out ASCII-readable values
46
      for (int i=0; i<filesize; i++)
47
	  for (int j=0; j<NumRecs; j++) {
48
	      short val = (short)((8224 << 16) + random(26) + 0x2041);
49
	      file.writeShort(val);
50
          }
51
  else assert false : "Bad parameters";
52
 
53
  file.flush();
54
  file.close();
55
}
56
 
57
}