Example usage for java.io ByteArrayInputStream close

List of usage examples for java.io ByteArrayInputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayInputStream has no effect.

Usage

From source file:org.apache.carbondata.core.util.ObjectSerializationUtil.java

/**
 * Converts Base64 string to object./*from  w w w  .ja v  a 2 s  . c  o  m*/
 *
 * @param objectString serialized object in string format
 * @return Object after convert string to object
 * @throws IOException
 */
public static Object convertStringToObject(String objectString) throws IOException {
    if (objectString == null) {
        return null;
    }

    byte[] bytes = CarbonUtil.decodeStringToBytes(objectString);

    ByteArrayInputStream bais = null;
    GZIPInputStream gis = null;
    ObjectInputStream ois = null;

    try {
        bais = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(bais);
        ois = new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(), gis);
        return ois.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException("Could not read object", e);
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
            if (gis != null) {
                gis.close();
            }
            if (bais != null) {
                bais.close();
            }
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}

From source file:Main.java

/**
 * Attempts to decode Base64 data and deserialize a Java Object within.
 * Returns <tt>null if there was an error.
 * /*from  ww  w.  j  a  v a  2s  . com*/
 * @param encodedObject
 *            The Base64 data to decode
 * @return The decoded and deserialized object
 * @since 1.4
 */
public static Object decodeToObject(String encodedObject) throws Exception {
    byte[] objBytes = decode(encodedObject);

    java.io.ByteArrayInputStream bais = null;
    java.io.ObjectInputStream ois = null;

    try {
        bais = new java.io.ByteArrayInputStream(objBytes);
        ois = new java.io.ObjectInputStream(bais);

        return ois.readObject();
    } // end try
    catch (java.io.IOException e) {
        e.printStackTrace();
        return null;
    } // end catch
    catch (java.lang.ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } // end catch
    finally {
        try {
            bais.close();
        } catch (Exception e) {
            // ignore it
        }
        try {
            ois.close();
        } catch (Exception e) {
            // ignore it
        }
    } // end finally
}

From source file:cn.sharesdk.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;/*  ww  w .  j  ava 2s  . com*/
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);
    } catch (IOException e) {
        e.printStackTrace();
        Ln.i("NetworkHelper", "Base64Gzip == >>", e);
    }
    return result;
}

From source file:com.jaspersoft.jasperserver.util.JasperSerializationUtil.java

/**
 * DeSerialize a byte array//from  w w w . j a  va  2s  .  com
 * @param input
 * @return
 */
public static Object deserialize(byte[] input) {

    Object obj = null;
    Exception exp = null;
    ByteArrayInputStream bais = new ByteArrayInputStream(input);
    JasperObjectInputStream jois = null;
    long startTime = System.currentTimeMillis();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Enter deserialize .. Start Time" + System.currentTimeMillis());
        }

        jois = new JasperSerializationUtil().new JasperObjectInputStream(bais);
        obj = jois.readObject();
    } catch (IOException e) {
        exp = e;
    } catch (ClassNotFoundException e) {
        exp = e;
    } finally {
        try {
            if (null != jois) {
                jois.close();
            }
            bais.close();
        } catch (IOException e1) {
        }
        if (logger.isDebugEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            logger.debug("Exit deserialize .. Total Time Spent: " + elapsedTime);
        }

        if (null != exp) {
            logger.error(exp.getMessage(), exp);
            throw new RuntimeException(exp);
        }
    }
    return obj;
}

From source file:org.mrgeo.data.raster.RasterWritable.java

public static RasterWritable toWritable(final Raster raster, final CompressionCodec codec,
        final Compressor compressor, final Writable payload) throws IOException {
    compressor.reset();// w w w  .j  ava2  s .co  m

    final byte[] pixels = rasterToBytes(raster);
    final ByteArrayInputStream bis = new ByteArrayInputStream(pixels);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final CompressionOutputStream cos = codec.createOutputStream(baos, compressor);

    writeHeader(raster, cos);
    IOUtils.copyBytes(bis, cos, pixels.length, false);

    if (payload != null) {
        writePayload(payload, cos);
    }
    bis.close();
    cos.close();
    return new RasterWritable(baos.toByteArray());
}

From source file:com.openteach.diamond.network.waverider.master.MasterState.java

public static MasterState fromByteBuffer(ByteBuffer buffer) {
    ByteArrayInputStream bin = null;
    ObjectInputStream oin = null;
    try {//from  w  w w .j a  v  a2  s  . co m
        bin = new ByteArrayInputStream(buffer.array(), Packet.getHeaderSize() + Command.getHeaderSize(),
                buffer.remaining());
        oin = new ObjectInputStream(bin);
        return (MasterState) oin.readObject();
    } catch (IOException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } finally {
        try {
            if (oin != null) {
                oin.close();
            }
            if (bin != null) {
                bin.close();
            }
        } catch (IOException e) {
            logger.error(e);
        }
    }
}

From source file:org.collectionspace.services.client.test.BaseServiceTest.java

/**
 * Gets the part object./*w ww . ja v  a2 s . com*/
 *
 * @param partStr the part str
 * @param clazz the clazz
 * @return the part object
 * @throws JAXBException the jAXB exception
 */
@Deprecated
static protected Object getPartObject(String partStr, Class<?> clazz) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    ByteArrayInputStream bais = null;
    Object obj = null;
    try {
        bais = new ByteArrayInputStream(partStr.getBytes());
        Unmarshaller um = jc.createUnmarshaller();
        obj = um.unmarshal(bais);
    } finally {
        if (bais != null) {
            try {
                bais.close();
            } catch (Exception e) {
                if (logger.isDebugEnabled()) {
                    e.printStackTrace();
                }
            }
        }
    }
    return obj;
}

From source file:io.bitsquare.common.util.Utilities.java

public static <T extends Serializable> T deserialize(byte[] data) {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    ObjectInput in = null;//from   w  w  w  .j  a va 2  s  .  c om
    Object result = null;
    try {
        in = new LookAheadObjectInputStream(bis, true);
        result = in.readObject();
        if (!(result instanceof Serializable))
            throw new RuntimeException("Object not of type Serializable");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return (T) result;
}

From source file:org.apache.myfaces.shared_impl.util.StateUtils.java

public static byte[] decompress(byte[] bytes) {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int length;//  w  w  w . j  a v a2  s  . co  m

    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }

        byte[] moreBytes = baos.toByteArray();
        baos.close();
        bais.close();
        gis.close();
        baos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

From source file:org.apache.accumulo.core.client.mapreduce.lib.util.InputConfigurator.java

/**
 * Returns all {@link InputTableConfig} objects associated with this job.
 * /*from w  w w  .j  a va 2  s . c  om*/
 * @param implementingClass
 *          the class whose name will be used as a prefix for the property configuration key
 * @param conf
 *          the Hadoop configuration object to configure
 * @return all of the table query configs for the job
 * @since 1.6.0
 */
public static Map<String, InputTableConfig> getInputTableConfigs(Class<?> implementingClass,
        Configuration conf) {
    Map<String, InputTableConfig> configs = new HashMap<String, InputTableConfig>();
    Map.Entry<String, InputTableConfig> defaultConfig = getDefaultInputTableConfig(implementingClass, conf);
    if (defaultConfig != null)
        configs.put(defaultConfig.getKey(), defaultConfig.getValue());
    String configString = conf.get(enumToConfKey(implementingClass, ScanOpts.TABLE_CONFIGS));
    MapWritable mapWritable = new MapWritable();
    if (configString != null) {
        try {
            byte[] bytes = Base64.decodeBase64(configString.getBytes());
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            mapWritable.readFields(new DataInputStream(bais));
            bais.close();
        } catch (IOException e) {
            throw new IllegalStateException(
                    "The table query configurations could not be deserialized from the given configuration");
        }
    }
    for (Map.Entry<Writable, Writable> entry : mapWritable.entrySet())
        configs.put(((Text) entry.getKey()).toString(), (InputTableConfig) entry.getValue());

    return configs;
}