com.dicksoft.ocr.util.HttpUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.dicksoft.ocr.util.HttpUtil.java

Source

/**
 * Copyright (C) 2007 Richard Taylor
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * You should have received a copy of the GNU General Public License
 * (for example /usr/src/linux/COPYING); if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package com.dicksoft.ocr.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author <a href="mailto:ryo.away@gmail.com">Richard Taylor</a>
 */
public final class HttpUtil {
    private static final Log LOG = LogFactory.getLog(HttpUtil.class);

    private HttpUtil() {
    }

    /**
     * Downloads the text from the specified URL.
     * 
     * @param url
     *            the URL of the text to read
     * @return the text
     * @throws IOException
     *             if the URL is malformed, or problem reading the stream
     */
    public static String fetchText(String url) throws IOException {
        if (LOG.isDebugEnabled())
            LOG.debug("Http fetch: " + url);
        StringBuffer result = new StringBuffer();
        URL urlReal = null;
        urlReal = new URL(url);
        BufferedInputStream in = null;
        in = new BufferedInputStream(urlReal.openStream());

        int data = 0;
        while (true) {
            data = in.read();
            if (data == -1)
                break;
            else
                result.append((char) data);
        }
        return result.toString();
    }
}