Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as Java by sdfrfrfrsfsd ( 4 years ago )
import java.io.*;
import java.util.*;
// Contains static methods to implement random writing.
public class RandomWriter {
public static void main(String[] args) {
try {
checkUsage(args);
int seedLength = getNonNegativeInt(args[0]);
int fileLength = getNonNegativeInt(args[1]);
String source = getSource(args[2]);
String resultFile = (args.length==4)?args[3]:null;
generateText(seedLength, fileLength, source, getOutput(resultFile));
}
catch (IllegalArgumentException e) {
}
}
// Make sure that we received three or four parameters. If not,
// issue an error message and throw an IllegalArgumentException.
private static void checkUsage (String[] args) {
if (args.length != 3 && args.length != 4) {
System.err.println("Usage: java RandomWriter seedlength " +
"filelength inputfile [outputfile]");
throw new IllegalArgumentException();
}
}
// Parse text into an int and make sure it is non-negative. If
// there's a problem, issue an error message and throw an
// IllegalArgumentException.
private static int getNonNegativeInt (String text) {
try {
int n = Integer.parseInt(text);
if (n >= 0) return n;
}
catch (NumberFormatException e) {
}
System.err.println(text + " is not a positive integer");
throw new IllegalArgumentException();
}
// Extract the contents of filename and return as a string. If
// there's a problem, issue an error message and throw an
// IllegalArgumentException.
private static String getSource (String filename) {
try {
File file = new File(filename);
int size = (int) file.length();
FileReader input = new FileReader(file);
char[] chars = new char[size];
input.read(chars);
input.close();
return new String(chars);
}
catch (IOException e) {
System.err.println("Unable to read " + filename);
throw new IllegalArgumentException();
}
}
// Generate and output text using the random writing algorithm. The
// length of the seed, the number of random characters desired, and
// the source are all provided. If the seed length is not shorter
// than the source file, issue an error message and throw an
// IllegalArgumentException.
private static void generateText(int seedLength, int fileLength,
String source, OutputStream output) {
try {
StringBuffer result = new StringBuffer();
CharGenerator generator = new CharGenerator(source, seedLength);
while (fileLength > 0) {
int c = generator.getNextChar();
if (c < 0) {
generator = new CharGenerator(source, seedLength);
}
else {
output.write(c);
if (output == System.out) output.flush();
fileLength--;
}
}
output.close();
}
catch (IOException e) {
System.err.println("Error writing output file");
throw new IllegalArgumentException();
}
}
// If filename is NULL, returns System.out. Otherwise, tries to open
// the file and returns it if successful. If there's a problem, issues
// an error message and throws an IllegalArgumentException.
private static OutputStream getOutput (String filename) {
try {
if (filename == null) return System.out;
OutputStream output = new FileOutputStream(filename);
return output;
}
catch (IOException e) {
System.err.println("Unable to write " + filename);
throw new IllegalArgumentException();
}
}
public static class CharGenerator {
private String source; // Text from source file
private String seed; // Current seed
private int seedLength; // Length of seed
private Random random; // Random number generator
// Initializes the member variables.
public CharGenerator (String source, int seedLength) {
try {
this.source = source;
this.seedLength = seedLength;
this.random = new Random();
int seedIndex = random.nextInt(source.length() - seedLength);
this.seed = source.substring(seedIndex, seedLength+seedIndex);
}
catch (IllegalArgumentException e) {
System.err.println("Source file is not longer than the seed length");
throw e;
}
}
// Returns a randomly chosen character that follows an occurrence of seed in
// source. Updates the seed by removing its first character and appending
// new randomly chosen character. If there is no such character, returns -1.
public int getNextChar () {
ArrayList options = new ArrayList();
int position = -1;
while ((position = source.indexOf(seed, position+1)) >= 0) {
if (position+seedLength < source.length()) {
options.add(source.charAt(position + seedLength));
}
}
if (options.size() == 0) return -1;
int index = random.nextInt(options.size());
char next = ((Character)options.get(index)).charValue();
seed = seed + next;
seed = seed.substring(1);
return next;
}
}
}
Revise this Paste