Я все еще не уверен, просматриваете ли вы URL или хотите проверить данные.
Для последнего вы можете сделать что-то вроде этого ...
/*
* Some typical Image-File Signatures (starting @ byte 0)
*/
private static final byte[] JPG_1 = new byte[] {-1, -40, -1, -37};
private static final byte[] JPG_2 = new byte[] {-1, -40, -1, -18};
private static final byte[] PNG = new byte[] {-119, 80, 78, 71, 13, 10, 26, 10};
public static boolean isMime(final InputStream ist) {
/*
* The number of bytes of Mime-encoded Data you want to read.
* (4 * 19 = 76, which is MIMELINEMAX from Base64$Encoder)
*/
final int mimeBlockLength = 4; // <- MUST be 4!
final int mimeBlockCount = 19; // <- your choice
final int mimeBufferLength = mimeBlockCount * mimeBlockLength;
final byte[] bar = new byte[mimeBufferLength];
try (final BufferedInputStream bis = new BufferedInputStream(ist, mimeBufferLength))
{
/*
* We expect at least one complete Mime-encoded buffer...
*/
if (bis.read(bar) != mimeBufferLength) {
return false;
}
/*
* Use a Java 9 feature to compare Signatures...
*/
if (Arrays.equals(bar, 0, JPG_1.length, JPG_1, 0, JPG_1.length)
|| Arrays.equals(bar, 0, JPG_2.length, JPG_2, 0, JPG_2.length)
|| Arrays.equals(bar, 0, PNG .length, PNG , 0, PNG .length)) {
return true;
} else {
return false;
}
} catch (final IOException e) {
return false;
}
}