/** * This method is a shorthand for {@link #guessBinary(byte[], int, int) guessBinary(b, 0, b.length)}. * * @param b the data to analyze * @return true if BinaryDetector thinks that the specified data is binary */ public static boolean guessBinary(byte b[]) { return guessBinary(b, 0, b.length); }
/** * Tries and detect whether the given stream contains binary or text data.</br> * This method returns <code>true</code> if it thinks that the bytes correspond to binary data. * * <p>A maximum of {@link #RECOMMENDED_BYTE_SIZE} will be read from the <code>InputStream</code>. The * stream will not be closed and will not be repositionned after the bytes have been read. It is up to the calling * method to use the <code>InputStream#mark()</code> and <code>InputStream#reset()</code> methods (if supported) * or reopen the stream if needed. * </p> * * @param in the stream to analyze * @return true if BinaryDetector thinks that the specified data is binary * @throws IOException if an error occurred while reading the InputStream. */ public static boolean guessBinary(InputStream in) throws IOException { byte[] bytes = new byte[RECOMMENDED_BYTE_SIZE]; return guessBinary(bytes, 0, StreamUtils.readUpTo(in, bytes)); } }
private boolean doGenericChecks(AbstractFile file) throws WarnUserException { // Do not allow directories if(file.isDirectory()) return false; // Warn the user if the file is large that a certain size as the whole file is loaded into memory // (in a JTextArea) if(file.getSize()>1048576) throw new WarnUserException(Translator.get("file_viewer.large_file_warning")); // Warn the user if the file looks like a binary file InputStream in = null; try { in = file.getInputStream(); if(BinaryDetector.guessBinary(in)) return false; } catch(IOException e) { // Not much too do } finally { if(in!=null) { try { in.close(); } catch(IOException e2) {} } } return true; } }