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.panbox.core.crypto.CryptCore.java

public static SecretKey decryptShareKey(ShareKeyDBEntry entry, PublicKey pubKey, PrivateKey privKey) {
    SecretKey result = null;/*from   w  ww  . j a va 2 s.c  o m*/
    if (entry != null) {
        byte[] encSK = entry.getEncryptedKey(pubKey);
        byte[] sk = new byte[KeyConstants.SYMMETRIC_BLOCK_SIZE];
        try {
            ASYMM_CIPHER.init(Cipher.DECRYPT_MODE, privKey);
            ByteArrayInputStream bis = new ByteArrayInputStream(encSK);
            CipherInputStream cis = new CipherInputStream(bis, ASYMM_CIPHER);
            cis.read(sk);
            cis.close();
            bis.close();
            result = new SecretKeySpec(sk, entry.getAlgorithm());
        } catch (InvalidKeyException e) {
            logger.warn("Exception caught in CryptCore.decryptShareKey", e);
        } catch (IOException e) {
            logger.warn("Exception caught in CryptCore.decryptShareKey", e);
        }
    }
    return result;
}

From source file:com.facebook.infrastructure.utils.FBUtilities.java

public static Object deserializeFromStream(byte[] bytes) {
    Object o = null;/*  w ww .  jav  a 2  s  . co  m*/
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    try {
        ObjectInputStream ois = new ObjectInputStream(bis);
        try {
            o = ois.readObject();
        } catch (ClassNotFoundException e) {
        }
        ois.close();
        bis.close();
    } catch (IOException e) {
        LogUtil.getLogger(FBUtilities.class.getName()).info(LogUtil.throwableToString(e));
    }
    return o;
}

From source file:org.collectionspace.services.client.CollectionSpaceClientUtils.java

/**
 * Gets the part object.//from   w  ww  .j av  a  2s  . co m
 *
 * @param partStr the part str
 * @param clazz the clazz
 * @return the part object
 * @throws JAXBException the jAXB exception
 */
static public 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() == true) {
                    e.printStackTrace();
                }
            }
        }
    }
    return obj;
}

From source file:Main.java

public static void writeFile(File file, byte[] content) throws IOException {
    if (!file.exists()) {
        try {//from w  ww. ja v a 2 s  . c  o  m
            file.createNewFile();
        } catch (IOException e) {
            throw new IOException("not crete file=" + file.getAbsolutePath());
        }
    }
    FileOutputStream fileOutputStream = null;
    ByteArrayInputStream bis = null;
    try {
        bis = new ByteArrayInputStream(content);
        fileOutputStream = new FileOutputStream(file, false);
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = bis.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, length);
        }
        fileOutputStream.flush();
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:fr.paris.lutece.plugins.document.service.search.DocumentIndexer.java

/**
 * Get the content from the document//from   www . j av  a  2 s.  co m
 * @param document the document to index
 * @return the content
 */
private static String getContentToIndex(Document document) {
    StringBuilder sbContentToIndex = new StringBuilder();
    sbContentToIndex.append(document.getTitle());

    for (DocumentAttribute attribute : document.getAttributes()) {
        if (attribute.isSearchable()) {
            if (!attribute.isBinary()) {
                // Text attributes
                sbContentToIndex.append(" ");
                sbContentToIndex.append(attribute.getTextValue());
            } else {
                // Binary file attribute
                // Gets indexer depending on the ContentType (ie: "application/pdf" should use a PDF indexer)
                IFileIndexerFactory factoryIndexer = (IFileIndexerFactory) SpringContextService
                        .getBean(IFileIndexerFactory.BEAN_FILE_INDEXER_FACTORY);
                IFileIndexer indexer = factoryIndexer.getIndexer(attribute.getValueContentType());

                if (indexer != null) {
                    try {
                        ByteArrayInputStream bais = new ByteArrayInputStream(attribute.getBinaryValue());
                        sbContentToIndex.append(" ");
                        sbContentToIndex.append(indexer.getContentToIndex(bais));
                        bais.close();
                    } catch (IOException e) {
                        AppLogService.error(e.getMessage(), e);
                    }
                }
            }
        }
    }

    // Index Metadata
    sbContentToIndex.append(" ");
    sbContentToIndex.append(StringUtils.defaultString(document.getXmlMetadata()));

    return sbContentToIndex.toString();
}

From source file:com.silverpeas.jcrutil.BasicDaoFactory.java

public static void setContent(Node fileNode, byte[] content, String mimeType)
        throws RepositoryException, IOException {
    ByteArrayInputStream in = null;
    try {// w w  w  .j a va  2s  .  co  m
        in = new ByteArrayInputStream(content);
        setContent(fileNode, in, mimeType);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:Main.java

/**
 * This is only used when the launcher shortcut is created.
 * /* w  w w . ja v a2s  .c  om*/
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *            be cropped.
 * @param size The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 *         shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    Bitmap blurbitmap = null;

    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inJustDecodeBounds = true;
        blurbitmap = BitmapFactory.decodeStream(bs, null, options);
        options.inJustDecodeBounds = false;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bs != null) {
            try {
                bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * blurbitmap.getWidth());
    final int mHeight = Math.round(mScale * blurbitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;

}

From source file:Main.java

public static byte[] unZip(byte[] data) {
    byte[] b = null;
    try {//from  w w w.  j a v a  2s  .  co  m
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        ZipInputStream zip = new ZipInputStream(bis);
        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static Document GetDocumentFromBytes(byte[] data) {
    //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl",null);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = null;//  w  w w .j  a va2 s. com
    Document doc = null;
    java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(data);
    try {
        db = dbf.newDocumentBuilder();

        doc = db.parse(bais);
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            bais.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return doc;
}

From source file:org.ow2.proactive.utils.ObjectByteConverter.java

/**
 * Convert the given byte array into the corresponding object.
 * <p>/*ww w. ja  v a 2 s .  co  m*/
 * The given byteArray can be uncompressed if it has been compressed before.
 * 
 * @param input the byteArray to be convert as an object.
 * @param uncompress true if the given byteArray must be also uncompressed, false if no compression was made on it.
 * @return the object corresponding to the given byteArray.
 * @throws IOException if an I/O exception occurs when writing the returned object
 * @throws ClassNotFoundException if class represented by given byteArray is not found.
 */
public static Object byteArrayToObject(byte[] input, boolean uncompress)
        throws IOException, ClassNotFoundException {
    if (input == null) {
        return null;
    }
    if (uncompress) {
        // Uncompress the bytes
        Inflater decompressor = new Inflater();
        decompressor.setInput(input);

        ByteArrayOutputStream bos = null;
        try {
            // Create an expandable byte array to hold the compressed data.
            bos = new ByteArrayOutputStream();
            // Compress the data
            byte[] buf = new byte[512];
            while (!decompressor.finished()) {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }
            decompressor.end();
            // set the UNCOMPRESSED data
            input = bos.toByteArray();
        } catch (DataFormatException dfe) {
            //convert into io exception to fit previous behavior
            throw new IOException("Compressed data format is invalid : " + dfe.getMessage(), dfe);
        } finally {
            if (bos != null) {
                bos.close();
            }
        }
    }
    //here, input byteArray is uncompressed if needed
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
        bais = new ByteArrayInputStream(input);
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } finally {
        if (ois != null) {
            ois.close();
        }
        if (bais != null) {
            bais.close();
        }
    }
}