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:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Decode a SAML2 anthentication request for the HTTP-redirect binding.
 * //from w ww .j  av a2 s .  com
 * @param authnRequest
 *            the authn request
 * @return the encoded request
 * @throws IOException
 */
public static String httpRedirectDecode(final String encodedRequest) throws IOException {
    String inflatedRequest = null;

    ByteArrayInputStream bytesIn = null;
    InflaterInputStream inflater = null;

    final byte[] decodedBytes = Base64.decode(encodedRequest);

    try {
        bytesIn = new ByteArrayInputStream(decodedBytes);
        inflater = new InflaterInputStream(bytesIn, new Inflater(true));
        final Writer writer = new StringWriter();
        final char[] buffer = new char[1024];

        final Reader reader = new BufferedReader(new InputStreamReader(inflater, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }

        inflatedRequest = writer.toString();
    } finally {
        if (bytesIn != null) {
            bytesIn.close();
        }
        if (inflater != null) {
            inflater.close();
        }
    }

    return inflatedRequest;
}

From source file:jeevlet.representation.BlobRepresentation.java

@Override
public void write(OutputStream arg0) throws IOException {
    if (response == null)
        return;/*from w ww . jav a  2 s  . co m*/

    String data = response.getText();

    // FIXME use apache commons codec instead of sun lib
    // byte blob[] = new BASE64Decoder().decodeBuffer(data);

    byte blob[] = Base64.decodeBase64(data.getBytes());

    ByteArrayInputStream input = new ByteArrayInputStream(blob);
    copy(input, arg0);
    arg0.flush();
    input.close();
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleOpenShiftWebAppTarBall() throws IOException, ArchiveException {
    ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array());
    CompressorInputStream cis = new GzipCompressorInputStream(bis);
    ArchiveInputStream ais = new TarArchiveInputStream(cis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048);
    CompressorOutputStream cos = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(cos);

    ArchiveEntry nextEntry;//from ww w. j  a v  a 2  s . c o  m
    while ((nextEntry = ais.getNextEntry()) != null) {
        aos.putArchiveEntry(nextEntry);
        IOUtils.copy(ais, aos);
        aos.closeArchiveEntry();
    }
    ais.close();
    cis.close();
    bis.close();

    TarArchiveEntry entry = new TarArchiveEntry(
            Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile());
    byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes();
    entry.setSize(xmlData.length);
    aos.putArchiveEntry(entry);
    IOUtils.write(xmlData, aos);
    aos.closeArchiveEntry();

    aos.finish();
    cos.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:com.turbospaces.monitor.services.GroupDiscoveryService.java

public synchronized JChannel connect(final String group, final byte[] bytes) throws Exception {
    JChannel jChannel = channels.get(group);
    if (jChannel == null || bytes != null) {
        ClassPathResource resource = new ClassPathResource("udp-largecluster.xml");
        InputStream inputStream = resource.getInputStream();
        jChannel = new JChannel(inputStream);
        inputStream.close();//from   w  w w  .  j ava  2  s  .c o  m
        if (bytes != null) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            jChannel = new JChannel(byteArrayInputStream);
            byteArrayInputStream.close();
        }
        jChannel.setDiscardOwnMessages(true);
        ProtocolStack protocolStack = jChannel.getProtocolStack();
        protocolStack.getTransport().setValue("enable_diagnostics", false);
        logger.info("joining network group {}", group);
        jChannel.connect(group);
        channels.put(group, jChannel);
    }

    return jChannel;
}

From source file:deployer.TestUtils.java

public static ByteBuffer createSampleOpenShiftWebAppTarBallWithEmptyFiles(String[] filepaths)
        throws IOException, ArchiveException {
    ByteArrayInputStream bis = new ByteArrayInputStream(createSampleAppTarBall(ArtifactType.WebApp).array());
    CompressorInputStream cis = new GzipCompressorInputStream(bis);
    ArchiveInputStream ais = new TarArchiveInputStream(cis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(bis.available() + 2048);
    CompressorOutputStream cos = new GzipCompressorOutputStream(bos);
    ArchiveOutputStream aos = new TarArchiveOutputStream(cos);

    ArchiveEntry nextEntry;//from www  .j av a2  s.  c o m
    while ((nextEntry = ais.getNextEntry()) != null) {
        aos.putArchiveEntry(nextEntry);
        IOUtils.copy(ais, aos);
        aos.closeArchiveEntry();
    }
    ais.close();
    cis.close();
    bis.close();

    TarArchiveEntry entry = new TarArchiveEntry(
            Paths.get(".openshift", CONFIG_DIRECTORY, "/standalone.xml").toFile());
    byte[] xmlData = SAMPLE_STANDALONE_DATA.getBytes();
    entry.setSize(xmlData.length);
    aos.putArchiveEntry(entry);
    IOUtils.write(xmlData, aos);

    for (int i = 0; i < filepaths.length; i++) {
        String filepath = filepaths[i];
        TarArchiveEntry emptyEntry = new TarArchiveEntry(Paths.get(filepath).toFile());
        byte[] emptyData = "".getBytes();
        emptyEntry.setSize(emptyData.length);
        aos.putArchiveEntry(emptyEntry);
        IOUtils.write(emptyData, aos);
        aos.closeArchiveEntry();
    }

    aos.finish();
    cos.close();
    bos.flush();
    return ByteBuffer.wrap(bos.toByteArray());
}

From source file:org.openfaces.component.table.TableDataModel.java

private static boolean checkSerializableEqualsAndHashcode(Object rowKey) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Object deserializedRowKey;//from   w w  w .  ja va  2  s  . c  o  m
    try {
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(rowKey);
        oos.close();
        byte[] serializedObject = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedObject);
        ObjectInputStream ois = new ObjectInputStream(bais);
        deserializedRowKey = ois.readObject();
        bais.close();
    } catch (IOException e) {
        throw new RuntimeException(
                "The rowData or rowKey object is marked as Serializable but can't be serialized: "
                        + rowKey.getClass().getName()
                        + " ; check that all object's fields are also Serializable",
                e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    boolean equalsValid = deserializedRowKey.equals(rowKey);
    boolean hashCodeValid = deserializedRowKey.hashCode() == rowKey.hashCode();
    boolean result = equalsValid && hashCodeValid;
    return result;
}

From source file:org.eclipse.skalli.testutil.StorageServiceTestBase.java

private void writeContent(final String TEST_CATEGORY, String id, String content) throws Exception {
    StorageService storageService = getStorageService();
    ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
    storageService.write(TEST_CATEGORY, TEST_ID, is);
    is.close();
    return;/*from  w w  w .  j  av  a2  s .c  om*/
}

From source file:com.vigglet.oei.technician.UploadProfilePhoto.java

@Override
protected void preProcessRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {//w  ww .  ja v  a 2s  . co  m
        User user = getUser(req);

        for (Part part : req.getParts()) {
            byte[] b = IOUtils.toByteArray(part.getInputStream());
            String fileName = extractFileName(part);

            File file = new File(Content.FILE_LOCATION + "/" + fileName);
            FileOutputStream fos = new FileOutputStream(file);
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            IOUtils.copy(bais, fos);

            fos.flush();
            fos.close();
            bais.close();

            Content content = new Content();
            content.setCompany(user.getCompany());
            content.setUser(user.getId());
            content.setName(file.getName());
            content.setFilesize((int) file.length());
            content.setLocation(file.getAbsolutePath());

            ContentUtil.getInstance().insertOrUpdate(content);
        }
    } catch (ServletException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
}

From source file:org.osaf.cosmo.atom.processor.mock.MockContentProcessor.java

private void setItemProperties(Reader content, NoteItem item) throws ValidationException, ProcessorException {
    Properties props = new Properties();

    try {/*www.  ja v a2  s  . c o m*/
        ByteArrayInputStream in = new ByteArrayInputStream(IOUtils.toByteArray(content));
        props.load(in);
        in.close();
    } catch (IOException e) {
        throw new ProcessorException("Could not read properties", e);
    }

    String uid = props.getProperty("uid");
    if (StringUtils.isBlank(uid))
        throw new ProcessorException("Uid not found in content");
    item.setUid(uid);

    String name = props.getProperty("name");
    if (name != null) {
        if (StringUtils.isBlank(name))
            name = null;
        item.setDisplayName(name);
    }

    EventStamp es = StampUtils.getEventStamp(item);
    if (es != null) {
        String startDate = props.getProperty("startDate");
        if (startDate != null)
            es.setStartDate(parseDateTime(startDate));
    }
}

From source file:lucee.runtime.engine.CFMLEngineImpl.java

private static void copyRecursiveAndRename(Resource src, Resource trg) throws IOException {
    if (!src.exists())
        return;/* w  w w .j  ava2  s . c om*/
    if (src.isDirectory()) {
        if (!trg.exists())
            trg.mkdirs();

        Resource[] files = src.listResources();
        for (int i = 0; i < files.length; i++) {
            copyRecursiveAndRename(files[i], trg.getRealResource(files[i].getName()));
        }
    } else if (src.isFile()) {
        if (trg.getName().endsWith(".rc") || trg.getName().startsWith(".")) {
            return;
        }

        if (trg.getName().equals("railo-web.xml.cfm")) {
            trg = trg.getParentResource().getRealResource("lucee-web.xml.cfm");
            // cfLuceeConfiguration
            InputStream is = src.getInputStream();
            OutputStream os = trg.getOutputStream();
            try {
                String str = Util.toString(is);
                str = str.replace("<cfRailoConfiguration",
                        "<!-- copy from Railo context --><cfLuceeConfiguration");
                str = str.replace("</cfRailoConfiguration", "</cfLuceeConfiguration");
                str = str.replace("<railo-configuration", "<lucee-configuration");
                str = str.replace("</railo-configuration", "</lucee-configuration");
                str = str.replace("{railo-config}", "{lucee-config}");
                str = str.replace("{railo-server}", "{lucee-server}");
                str = str.replace("{railo-web}", "{lucee-web}");
                str = str.replace("\"railo.commons.", "\"lucee.commons.");
                str = str.replace("\"railo.runtime.", "\"lucee.runtime.");
                str = str.replace("\"railo.cfx.", "\"lucee.cfx.");
                str = str.replace("/railo-context.ra", "/lucee-context.lar");
                str = str.replace("/railo-context", "/lucee");
                str = str.replace("railo-server-context", "lucee-server");
                str = str.replace("http://www.getrailo.org", "http://stable.lucee.org");
                str = str.replace("http://www.getrailo.com", "http://stable.lucee.org");

                ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());

                try {
                    Util.copy(bais, os);
                    bais.close();
                } finally {
                    Util.closeEL(is, os);
                }
            } finally {
                Util.closeEL(is, os);
            }
            return;
        }

        InputStream is = src.getInputStream();
        OutputStream os = trg.getOutputStream();
        try {
            Util.copy(is, os);
        } finally {
            Util.closeEL(is, os);
        }
    }
}