Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Main {
    public static ByteArrayInputStream toByteStream(InputStream istream) {
        ByteArrayInputStream byteistream = new ByteArrayInputStream(new byte[0]);
        try {
            ByteArrayOutputStream byteostream = new ByteArrayOutputStream(8192);
            byte[] buffer = new byte[8192];
            int lenght;
            while ((lenght = istream.read(buffer)) != -1) {
                byteostream.write(buffer, 0, lenght);
            }
            byteistream = new ByteArrayInputStream(byteostream.toByteArray());
            byteostream.close();
        } catch (Exception e) {
        } finally {
            try {
                istream.close();
            } catch (Exception e) {
            }
        }
        return byteistream;
    }

    public static byte[] toByteArray(InputStream istream) {
        ByteArrayOutputStream byteostream = null;
        try {
            byteostream = new ByteArrayOutputStream(8192);
            byte[] buffer = new byte[8192];
            int lenght;
            while ((lenght = istream.read(buffer)) != -1) {
                byteostream.write(buffer, 0, lenght);
            }
            return byteostream.toByteArray();
        } catch (Exception e) {
            return null;
        } finally {
            try {
                istream.close();
                byteostream.close();
            } catch (Exception e) {
            }
        }
    }
}