Android Open Source - mclib Stream Helper






From Project

Back to project page mclib.

License

The source code is released under:

Apache License, Version 2.0 FoundationProjectsPeopleGet InvolvedDownloadSupport ApacheHome ? Licenses Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITION...

If you think the Android project mclib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
   Copyright 2012 Mikhail Chabanov/*ww w  . j  a  va  2s.c om*/

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package mc.lib.stream;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import mc.lib.log.Log;
import mc.lib.network.NetworkHelper;
import android.content.res.AssetFileDescriptor;

public class StreamHelper
{
    private static final String LOGTAG = StreamHelper.class.getSimpleName();

    public static final int BUFFER_SIZE = 8192; // Dalvic buffer size
    public static final int READING_TIMEOUT = 1000;

    public static String readStream(InputStream src)
    {
        return readNetworkStream(src, NetworkHelper.DEFAULT_ENCODING);
    }

    public static String readStream(InputStream src, String encoding)
    {
        try
        {
            return AdvancedStreamHelper.readStreamWithBuffer(src, encoding, BUFFER_SIZE);
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Exception while reading stream", e);
        }
        return null;
    }

    public static byte[] readStreamToBytes(InputStream src)
    {
        try
        {
            byte[] res = new byte[src.available()];
            src.read(res);
            return res;
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Exception while reading stream", e);
        }
        return null;
    }

    public static void copyStream(InputStream in, OutputStream out)
    {
        try
        {
            AdvancedStreamHelper.copyStream(in, out, BUFFER_SIZE);
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Exception while copy stream", e);
        }
    }

    public static String readNetworkStream(InputStream src)
    {
        return readNetworkStream(src, NetworkHelper.DEFAULT_ENCODING);
    }

    public static String readNetworkStream(InputStream src, String encoding)
    {
        return readNetworkStream(src, encoding, READING_TIMEOUT);
    }

    public static String readNetworkStream(InputStream src, String encoding, int readingTimeout)
    {
        try
        {
            if(readingTimeout < READING_TIMEOUT)
            {
                readingTimeout = READING_TIMEOUT;
            }
            return AdvancedStreamHelper.readStreamWithBuffer(src, encoding, BUFFER_SIZE, readingTimeout);
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Exception while reading stream", e);
        }
        return null;
    }

    public static void copyNetworkStream(InputStream in, OutputStream out)
    {
        copyNetworkStream(in, out, READING_TIMEOUT);
    }

    public static void copyNetworkStream(InputStream in, OutputStream out, int readingTimeout)
    {
        try
        {
            if(readingTimeout < READING_TIMEOUT)
            {
                readingTimeout = READING_TIMEOUT;
            }
            AdvancedStreamHelper.copyStream(in, out, readingTimeout);
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Exception while stream copy", e);
        }
    }

    public static void close(AssetFileDescriptor fd)
    {
        try
        {
            if(fd != null)
                fd.close();
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Error on close AssetFileDescriptor " + fd, e);
        }

    }

    public static void close(Closeable c)
    {
        try
        {
            if(c != null)
                c.close();
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Error on close stream " + c, e);
        }

    }

    public static void close(Socket s)
    {
        try
        {
            if(s != null)
                s.close();
        }
        catch(IOException e)
        {
            Log.e(LOGTAG, "Error on close socket " + s, e);
        }
    }
}




Java Source Code List

mc.lib.archives.ArchivesHelper.java
mc.lib.assets.AssetsHelper.java
mc.lib.audio.AudioSource.java
mc.lib.audio.IAudioPlayer.java
mc.lib.audio.MediaAudioPlayer.java
mc.lib.files.FileHelper.java
mc.lib.files.FileReadingHelper.java
mc.lib.files.PermissionHelper.java
mc.lib.identity.IdentityHelper.java
mc.lib.image.BitmapTools.java
mc.lib.image.ImageHelper.java
mc.lib.image.InputStreamFullSkip.java
mc.lib.interfaces.OnCompleteListener.java
mc.lib.interfaces.OnProgressListener.java
mc.lib.interfaces.OnStartListener.java
mc.lib.io.KeyboardHelper.java
mc.lib.io.ToastHelper.java
mc.lib.location.LocationHelper.java
mc.lib.location.OnLocationChangeListener.java
mc.lib.log.Log.java
mc.lib.network.AsyncFileDownloader.java
mc.lib.network.AsyncNetworkHelper.java
mc.lib.network.EmailHelper.java
mc.lib.network.NetworkHelper.java
mc.lib.regexp.PopularRegexp.java
mc.lib.regexp.Validator.java
mc.lib.screen.ScreenHelper.java
mc.lib.stream.AdvancedStreamHelper.java
mc.lib.stream.StreamHelper.java
mc.lib.video.BasicVideoPlayer.java
mc.lib.video.IVideoPlayer.java
mc.lib.video.VideoViewPlayer.java