IO.java :  » Database-DBMS » Quadcap-Embeddable-Database » com » quadcap » io » Java Open Source

Java Open Source » Database DBMS » Quadcap Embeddable Database 
Quadcap Embeddable Database » com » quadcap » io » IO.java
package com.quadcap.io;

/* Copyright 1997 - 2003 Quadcap Software.  All rights reserved.
 *
 * This software is distributed under the Quadcap Free Software License.
 * This software may be used or modified for any purpose, personal or
 * commercial.  Open Source redistributions are permitted.  Commercial
 * redistribution of larger works derived from, or works which bundle
 * this software requires a "Commercial Redistribution License"; see
 * http://www.quadcap.com/purchase.
 *
 * Redistributions qualify as "Open Source" under  one of the following terms:
 *   
 *    Redistributions are made at no charge beyond the reasonable cost of
 *    materials and delivery.
 *
 *    Redistributions are accompanied by a copy of the Source Code or by an
 *    irrevocable offer to provide a copy of the Source Code for up to three
 *    years at the cost of materials and delivery.  Such redistributions
 *    must allow further use, modification, and redistribution of the Source
 *    Code under substantially the same terms as this license.
 *
 * Redistributions of source code must retain the copyright notices as they
 * appear in each source code file, these license terms, and the
 * disclaimer/limitation of liability set forth as paragraph 6 below.
 *
 * Redistributions in binary form must reproduce this Copyright Notice,
 * these license terms, and the disclaimer/limitation of liability set
 * forth as paragraph 6 below, in the documentation and/or other materials
 * provided with the distribution.
 *
 * The Software is provided on an "AS IS" basis.  No warranty is
 * provided that the Software is free of defects, or fit for a
 * particular purpose.  
 *
 * Limitation of Liability. Quadcap Software shall not be liable
 * for any damages suffered by the Licensee or any third party resulting
 * from use of the Software.
 */

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import com.quadcap.util.Util;

/**
 * 
 *
 * @author Stan Bailes
 */
public class IO {
    /**
     * Diff two files, return true if the file contents are the same
     */
    public static boolean cmpFile(File a, File b) throws IOException {
        FileInputStream fa = null;
        FileInputStream fb = null;
        try {
            fa = new FileInputStream(a);
            BufferedInputStream sa = new BufferedInputStream(fa);
            fb = new FileInputStream(b);
            BufferedInputStream sb = new BufferedInputStream(fb);
            int ca = sa.read();
            int cb = sb.read();
            while (ca >= 0 && cb >= 0 && ca == cb) {
                ca = sa.read();
                cb = sb.read();
            }
            return ca == cb;
        } finally {
            if (fa != null) try { fa.close(); } catch (IOException e) {}
            if (fb != null) try { fb.close(); } catch (IOException e) {}
        }
            
    }
    
    /**
     * Copy a file
     */
    public static final void copyFile(File from, File to) throws IOException {
        FileOutputStream fos = new FileOutputStream(to);
        try {
            FileInputStream fis = new FileInputStream(from);
            try {
                copyStream(fis, fos);
            } finally {
                fis.close();
            }
        } finally {
            fos.close();
        }
    }

    /**
     * Copy 'is' to 'os' until EOF
     */
    public static final void copyStream(InputStream is, OutputStream os)
        throws IOException
    {
        byte[] buf = new byte[4096];
        int cnt;
        while ((cnt = is.read(buf)) > 0) {
            os.write(buf, 0, cnt);
        }
    }

    /**
     * Rate limited stream copy.  Copy the input stream to the output
     * until EOF.  An initial series of "buffered" bytes is sent with
     * no rate limit, thereafter, bytes are streamed (in 4Kbyte buffers) at
     * the rate limit "bps" bytes per/sec.
     * No particular effort is made to buffer the incoming stream.
     */
    public static final void copyStream(InputStream is, OutputStream os,
                                        int bps, int buffered)
        throws IOException
    {
        byte[] buf = new byte[4096];
        int cnt;
        int amt = 0;
        long interval = (buf.length * 1000) / bps;
        long wait = 0;
        long start = System.currentTimeMillis();
        while ((cnt = is.read(buf)) > 0) {
            amt += cnt;
            if (wait > 0) Util.sleep(wait);
            os.write(buf, 0, cnt);
            long done = System.currentTimeMillis();
            if (amt > buffered) {
                long elap = done - start;
                wait = interval - elap;
            }
            start = done;
        }
    }

    /**
     * Copy 'r' to 'w' until EOF
     */
    public static final void copyStream(Reader r, Writer w) 
        throws IOException
    {
        char[] buf = new char[4096];
        int cnt;
        while ((cnt = r.read(buf)) > 0) {
            w.write(buf, 0, cnt);
        }
    }

    /**
     * Copy 'is' to 'os' until EOF, or <b>limit</b> bytes are copied.
     */
    public static final void copyStream(InputStream is, OutputStream os,
                                        int limit)
        throws IOException
    {
        byte[] buf = new byte[4096];
        int cnt;
        while (limit > buf.length) {
            if ((cnt = is.read(buf)) > 0) {
                os.write(buf, 0, cnt);
                limit -= cnt;
            } else {
                limit = 0;
            }
        }
        if ((cnt = is.read(buf, 0, limit)) > 0) {
            os.write(buf, 0, cnt);
        }
    }

    /**
     * Write a string to an outputstream using the brutal low-byte extraction
     * encoding technique.  Works great for ISO-8859-1.
     */
    public static void write(OutputStream os, String s) throws IOException {
        final int len = s.length();
        for (int i = 0; i < len; i++) os.write(s.charAt(i));
    }

    /**
     * Read an inputstream into a byte buffer
     */
    public static void readFully(InputStream is, byte[] buf)
        throws IOException
    {
        int pos = 0;
        int len = buf.length;
        do {
            int cnt = is.read(buf, pos, len);
            if (cnt <= 0) throw new IOException("unexpected EOF");
            len -= cnt;
            pos += cnt;
        } while (len > 0);
    }

    /**
     * Recursively delete an entire directory.  Be careful!
     */
    public static void deleteDirectory(File f) throws IOException {
        File[] files = f.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    deleteDirectory(f);
                } else {
                    file.delete();
                }
            }
        }
        f.delete();
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.