Java Examples for org.encog.bot.BotUtil

The following java examples will help you to understand the usage of org.encog.bot.BotUtil. These source code samples are taken from different open source projects.

Example 1
Project: VAFusion2-master  File: CloudRequest.java View source code
/**
	 * Perform a GET request.
	 * 
	 * @param async
	 *            True if this request should be asynchronous.
	 * @param url
	 *            The URL.
	 */
public void performURLGET(final boolean async, final String url) {
    try {
        if (async) {
            final AsynchronousCloudRequest request = new AsynchronousCloudRequest(new URL(url));
            final Thread t = new Thread(request);
            t.setDaemon(true);
            t.start();
        } else {
            final URL url2 = new URL(url);
            final URLConnection u = url2.openConnection();
            final String contents = BotUtil.loadPage(u.getInputStream());
            handleResponse(contents);
        }
    } catch (final IOException e) {
        throw new EncogCloudError(e);
    }
}
Example 2
Project: encog-java-core-master  File: EncogAnalyst.java View source code
/**
	 * Down load a file from the specified URL, uncompress if needed.
	 * @param url THe URL.
	 * @param file The file to down load into.
	 */
private void downloadPage(final URL url, final File file) {
    FileOutputStream fos = null;
    InputStream is = null;
    FileInputStream fis = null;
    GZIPInputStream gis = null;
    try {
        // download the URL
        long size = 0;
        final byte[] buffer = new byte[BotUtil.BUFFER_SIZE];
        final File tempFile = new File(file.getParentFile(), "temp.tmp");
        int length;
        int lastUpdate = 0;
        fos = new FileOutputStream(tempFile);
        is = url.openStream();
        do {
            length = is.read(buffer);
            if (length >= 0) {
                fos.write(buffer, 0, length);
                size += length;
            }
            if (lastUpdate > UPDATE_TIME) {
                report(0, (int) (size / Format.MEMORY_MEG), "Downloading... " + Format.formatMemory(size));
                lastUpdate = 0;
            }
            lastUpdate++;
        } while (length >= 0);
        fos.close();
        fos = null;
        if (url.toString().toLowerCase().endsWith(".gz")) {
            fis = new FileInputStream(tempFile);
            gis = new GZIPInputStream(fis);
            fos = new FileOutputStream(file);
            size = 0;
            lastUpdate = 0;
            do {
                length = gis.read(buffer);
                if (length >= 0) {
                    fos.write(buffer, 0, length);
                    size += length;
                }
                if (lastUpdate > UPDATE_TIME) {
                    report(0, (int) (size / Format.MEMORY_MEG), "Uncompressing... " + Format.formatMemory(size));
                    lastUpdate = 0;
                }
                lastUpdate++;
            } while (length >= 0);
            tempFile.delete();
        } else {
            // rename the temp file to the actual file
            file.delete();
            tempFile.renameTo(file);
        }
    } catch (final IOException e) {
        throw new AnalystError(e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
        if (gis != null) {
            try {
                gis.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
    }
}
Example 3
Project: encog-java-examples-master  File: MultiSunspot.java View source code
/**
	 * Download the sun spot data from NASA.
	 * @return The path downloaded to.
	 * @throws MalformedURLException
	 */
public static File downloadSunSpotData() throws MalformedURLException {
    File rawFile = new File(MYDIR, "sunspots.csv");
    // Step 1. Download sunspot data from NASA.
    if (rawFile.exists()) {
        System.out.println("Data already downloaded to: " + rawFile.getPath());
    } else {
        System.out.println("Downloading sunspot data to: " + rawFile.getPath());
        BotUtil.downloadPage(new URL("http://solarscience.msfc.nasa.gov/greenwch/spot_num.txt"), rawFile);
    }
    return rawFile;
}
Example 4
Project: encog-java-workbench-master  File: CreateTrainingData.java View source code
public static void downloadSunspots(String name) {
    try {
        EncogWorkBench.getInstance().getMainWindow().beginWait();
        File targetFile = new File(EncogWorkBench.getInstance().getProjectDirectory(), name);
        BotUtil.downloadPage(new URL("http://solarscience.msfc.nasa.gov/greenwch/spot_num.txt"), targetFile);
    } catch (IOException ex) {
        EncogWorkBench.displayError("Error Downloading Data", ex);
    } finally {
        EncogWorkBench.getInstance().getMainWindow().endWait();
    }
}
Example 5
Project: encog-java-master  File: EncogAnalyst.java View source code
/**
	 * Down load a file from the specified URL, uncompress if needed.
	 * @param url THe URL.
	 * @param file The file to down load into.
	 */
private void downloadPage(final URL url, final File file) {
    FileOutputStream fos = null;
    InputStream is = null;
    FileInputStream fis = null;
    GZIPInputStream gis = null;
    try {
        // download the URL
        long size = 0;
        final byte[] buffer = new byte[BotUtil.BUFFER_SIZE];
        final File tempFile = new File(file.getParentFile(), "temp.tmp");
        int length;
        int lastUpdate = 0;
        fos = new FileOutputStream(tempFile);
        is = url.openStream();
        do {
            length = is.read(buffer);
            if (length >= 0) {
                fos.write(buffer, 0, length);
                size += length;
            }
            if (lastUpdate > UPDATE_TIME) {
                report(0, (int) (size / Format.MEMORY_MEG), "Downloading... " + Format.formatMemory(size));
                lastUpdate = 0;
            }
            lastUpdate++;
        } while (length >= 0);
        fos.close();
        fos = null;
        if (url.toString().toLowerCase().endsWith(".gz")) {
            fis = new FileInputStream(tempFile);
            gis = new GZIPInputStream(fis);
            fos = new FileOutputStream(file);
            size = 0;
            lastUpdate = 0;
            do {
                length = gis.read(buffer);
                if (length >= 0) {
                    fos.write(buffer, 0, length);
                    size += length;
                }
                if (lastUpdate > UPDATE_TIME) {
                    report(0, (int) (size / Format.MEMORY_MEG), "Uncompressing... " + Format.formatMemory(size));
                    lastUpdate = 0;
                }
                lastUpdate++;
            } while (length >= 0);
            tempFile.delete();
        } else {
            // rename the temp file to the actual file
            file.delete();
            tempFile.renameTo(file);
        }
    } catch (final IOException e) {
        throw new AnalystError(e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
        if (gis != null) {
            try {
                gis.close();
            } catch (IOException e) {
                EncogLogging.log(e);
            }
        }
    }
}