⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linechunksource.java

📁 java编写的OCR软件
💻 JAVA
字号:
package de.spieleck.app.jacson.source;

import java.io.*;
import java.util.zip.GZIPInputStream;

import de.spieleck.app.jacson.JacsonException;
import de.spieleck.app.jacson.JacsonReport;

/**
 * Simplest possible Source:
 * Reads a file and generates a chunk for every single line read.
 * The only "major" feature: gzipped files are automagically unpacked
 * while reading. This has proved to be quite useful for weblos, since
 * those can be large and compress well..
 * @author fsn
 */
public class LineChunkSource
    extends ChunkSourceBase
{
    protected String fileName = null;
    protected BufferedReader in;
    protected int chunks;

    public LineChunkSource()
    {
        chunks = 0;
    }

    public LineChunkSource(String fileName, boolean ungzip)
        throws JacsonException
    {
        this();
        this.fileName = fileName;
        try
        {
            setStream(new FileInputStream(fileName), ungzip);
        }
        catch ( FileNotFoundException e )
        {
            e.printStackTrace();
            throw new JacsonException("Cannot open file!", e);
        }
    }

    public LineChunkSource(InputStream is, boolean ungzip)
        throws JacsonException
    {
        this();
        setStream(is, ungzip);
    }

    public void setStream(InputStream is)
        throws JacsonException
    {
        setStream(is, true);
    }

    public void setStream(InputStream is, boolean ungzip)
        throws JacsonException
    {
        try
        {
            if (ungzip)
                is = unwrapGZIP(is);
            setReader(new InputStreamReader(is));
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            throw new JacsonException("Error opening stream", e);
        }
    }

    public void setReader(Reader ir)
    {
        // Avoid double buffering
        if ( ir instanceof BufferedReader )
            in = (BufferedReader) ir;
        else
            in = new BufferedReader(ir,32768);
    }

    public String nextChunk()
        throws JacsonException
    {
        try
        {
            chunks++;
            return in.readLine();
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            return null;
        }
    }

    public String message()
    {
        JacsonReport jr = getRegReport();
        jr.begin("lines");
        if ( fileName != null )
            jr.report("name", fileName);
        return fileName;
    }

    public void summary()
    {
        JacsonReport jr = getRegReport();
        jr.report("chunks", Integer.toString(chunks));
        jr.end();
    }

    public static InputStream unwrapGZIP(InputStream is)
        throws IOException
    {
        // This ugly little hack can cause double buffering!
        if ( !is.markSupported() )
            is = new BufferedInputStream(is, 16384);
        is.mark(5); // allow reset of GZip magic number
        try
        {
            return new GZIPInputStream(is);
        }
        catch ( IOException gzip_failed )
        {
             is.reset();
        }
        return is;
    }
}
//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -