org.bouncycastle.asn1.StreamUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk14 Show documentation
Show all versions of bcprov-jdk14 Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.4.
package org.bouncycastle.asn1;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
class StreamUtil
{
/**
* Find out possible longest length, capped by available memory.
*
* @param in input stream of interest
* @return length calculation or MAX_VALUE.
*/
static int findLimit(InputStream in)
{
if (in instanceof LimitedInputStream)
{
return ((LimitedInputStream)in).getLimit();
}
else if (in instanceof ASN1InputStream)
{
return ((ASN1InputStream)in).getLimit();
}
else if (in instanceof ByteArrayInputStream)
{
return ((ByteArrayInputStream)in).available();
}
else if (in instanceof FileInputStream)
{
try
{
FileChannel channel = ((FileInputStream)in).getChannel();
long size = (channel != null) ? channel.size() : Integer.MAX_VALUE;
if (size < Integer.MAX_VALUE)
{
return (int)size;
}
}
catch (IOException e)
{
// ignore - they'll find out soon enough!
}
}
long maxMemory = Runtime.getRuntime().maxMemory();
if (maxMemory > Integer.MAX_VALUE)
{
return Integer.MAX_VALUE;
}
return (int)maxMemory;
}
}