Example usage for java.io InputStream available

List of usage examples for java.io InputStream available

Introduction

In this page you can find the example usage for java.io InputStream available.

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

Usage

From source file:com.soomla.store.data.StorefrontInfo.java

/**
 * Read 'theme.json' and return it.//  w  ww. j  a  v a  2 s  .  c  om
 * @return theme.json's content as String.
 */
public String fetchThemeJsonFromFile() {
    String storefrontJSON = "";
    try {
        InputStream in = SoomlaApp.getAppContext().getAssets().open("soomla/theme.json");

        byte[] buffer = new byte[in.available()];
        in.read(buffer);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(buffer);
        out.close();
        in.close();

        storefrontJSON = out.toString();
    } catch (IOException e) {
        StoreUtils.LogError(TAG,
                "Can't read JSON storefront file. Please add theme.json to your 'assets' folder.");
    }

    return storefrontJSON;
}

From source file:de.elomagic.mag.AbstractTest.java

protected byte[] getOriginalMailAttachment() throws IOException {
    InputStream in = getClass().getResourceAsStream("/TestFile.pdf");

    return IOUtils.readFully(in, in.available());
}

From source file:net.krotscheck.util.ResourceUtilTest.java

/**
 * Assert that we can read a resource as a stream.
 *
 * @throws Exception Should not be thrown.
 *///from   w  ww.  j a  v  a  2  s  .  c o  m
@Test
public void testGetResourceAsStream() throws Exception {
    String name = "/valid-resource-file.txt";
    InputStream stream = ResourceUtil.getResourceAsStream(name);
    Assert.assertTrue(stream.available() > 0);

    String invalidName = "/invalid-resource-file.txt";
    InputStream invalidContent = ResourceUtil.getResourceAsStream(invalidName);

    Assert.assertTrue(invalidContent instanceof NullInputStream);
    Assert.assertTrue(invalidContent.available() == 0);

    // Make the file write only
    File resource = ResourceUtil.getFileForResource(name);
    Set<PosixFilePermission> oldPerms = Files.getPosixFilePermissions(resource.toPath());
    Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OWNER_WRITE);
    perms.add(PosixFilePermission.OWNER_EXECUTE);
    perms.add(PosixFilePermission.GROUP_WRITE);
    perms.add(PosixFilePermission.GROUP_EXECUTE);
    perms.add(PosixFilePermission.OTHERS_WRITE);
    perms.add(PosixFilePermission.OTHERS_EXECUTE);

    // Write only...
    Files.setPosixFilePermissions(resource.toPath(), perms);
    String writeOnlyName = "/valid-resource-file.txt";
    InputStream writeOnlyContent = ResourceUtil.getResourceAsStream(writeOnlyName);
    Assert.assertTrue(writeOnlyContent instanceof NullInputStream);
    Assert.assertTrue(writeOnlyContent.available() == 0);

    Files.setPosixFilePermissions(resource.toPath(), oldPerms);
}

From source file:algorithm.F5Steganography.java

private void restore(String payload, String carrier) throws IOException {
    try {/*from   www .  java2  s  .  com*/
        Process process = Runtime.getRuntime().exec(
                new String[] { "java", "-jar", LIBRARY_DIRECTORY + "f5.jar", "x", "-e", payload, carrier });
        process.waitFor();
        InputStream inputStream = process.getInputStream();
        byte b[] = new byte[inputStream.available()];
        inputStream.read(b, 0, b.length);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:algorithm.F5Steganography.java

private void encapsulate(String carrier, String payload, String output) throws IOException {
    try {// ww  w .  j av  a 2 s  . c  om
        String[] args = new String[] { "java", "-jar", LIBRARY_DIRECTORY + "f5.jar", "e", "-e", payload,
                carrier, output };
        Process process = Runtime.getRuntime().exec(args);
        process.waitFor();
        InputStream inputStream = process.getInputStream();
        byte b[] = new byte[inputStream.available()];
        inputStream.read(b, 0, b.length);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.pentaho.repository.importexport.StreamToJobNodeConverter.java

/**
 * /*from  www .ja  v a  2s . c  o m*/
 * @param inputStream
 * @param charset
 * @param mimeType
 * @return
 */
public IRepositoryFileData convert(final InputStream inputStream, final String charset, final String mimeType) {
    try {
        long size = inputStream.available();
        JobMeta jobMeta = new JobMeta();
        Repository repository = connectToRepository();
        Document doc = PDIImportUtil.loadXMLFrom(inputStream);
        if (doc != null) {
            jobMeta.loadXML(doc.getDocumentElement(), repository, null);
            JobDelegate delegate = new JobDelegate(repository, this.unifiedRepository);
            delegate.saveSharedObjects(jobMeta, null);
            return new NodeRepositoryFileData(delegate.elementToDataNode(jobMeta), size);
        } else {
            return null;
        }
    } catch (Exception e) {
        return null;
    }
}

From source file:com.plusub.lib.example.activity.tab4.Tab4Fragment.java

public String getFromAssets(String fileName) {
    String result = "";
    try {/*from   w  w  w . j ava  2 s  . co m*/
        InputStream in = getActivity().getResources().getAssets().open(fileName);
        //?  
        int lenght = in.available();
        //byte  
        byte[] buffer = new byte[lenght];
        //?byte  
        in.read(buffer);
        result = EncodingUtils.getString(buffer, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:org.opensaml.security.httpclient.impl.SecurityEnhancedTLSSocketFactoryTest.java

private X509Certificate getCertificate(String fileName) {
    try {//from  w w  w .  ja  v  a2  s.  c o  m
        InputStream ins = getInputStream(fileName);
        byte[] encoded = new byte[ins.available()];
        ins.read(encoded);
        return X509Support.decodeCertificates(encoded).iterator().next();
    } catch (Exception e) {
        Assert.fail("Could not create certificate from file: " + fileName + ": " + e.getMessage());
    }
    return null;
}

From source file:br.com.bluesoft.pronto.controller.SprintController.java

@SuppressWarnings("unchecked")
private byte[] getImageBytes(final HttpServletRequest request) throws FileUploadException, IOException {
    final FileItemFactory factory = new DiskFileItemFactory();
    final ServletFileUpload upload = new ServletFileUpload(factory);
    byte bytes[] = null;
    final List<FileItem> items = upload.parseRequest(request);
    for (final FileItem fileItem : items) {
        final InputStream inputStream = fileItem.getInputStream();
        final int numberBytes = inputStream.available();
        bytes = new byte[numberBytes];
        inputStream.read(bytes);/* w w  w.  j av a  2 s .  co  m*/
    }
    return bytes;
}

From source file:com.yanzhenjie.andserver.handler.AssetsRequestHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    InputStream stream = mAssetsWrapper.getInputStream(mFilePath);
    if (stream == null) {
        requestInvalid(response);/*from ww  w  .  j a va 2 s .  c o m*/
    } else {
        response.setStatusCode(200);
        response.setEntity(new InputStreamEntity(stream, stream.available()));
    }
}