/** * This method is a shorthand for {@link #detectEncoding(byte[], int, int) detectEncoding(b, 0, b.length)}. * * @param bytes the bytes for which to detect the encoding * @return the best guess at the character encoding, null if there is none (not enough data or confidence) */ public static String detectEncoding(byte bytes[]) { return detectEncoding(bytes, 0, bytes.length); }
/** * Lists all detectable encodings as returned by {@link #getDetectableEncodings()} to the standard output. * @param args command line arguments. */ public static void main(String args[]) { String encodings[] = getDetectableEncodings(); for (String encoding : encodings) System.out.println(encoding); } }
public synchronized void processDied(int returnValue) { String encoding; String oldEncoding; // Abort if there is no need to identify the encoding anymore. if(out == null) return; // Attempts to guess at the encoding. If no guess can be made, ignore. if((encoding = EncodingDetector.detectEncoding(out.toByteArray())) == null) return; // Checks whether the detected charset is supported. if(Charset.isSupported(encoding)) { oldEncoding = MuConfigurations.getPreferences().getVariable(MuPreference.SHELL_ENCODING); // If no encoding was previously set, or we have found a new encoding, change the current shell encoding. if((oldEncoding == null) || !encoding.equals(oldEncoding)) MuConfigurations.getPreferences().setVariable(MuPreference.SHELL_ENCODING, encoding); // Stop listening for new byte input if we have gathered a large enough sample set. if(out.size() >= EncodingDetector.MAX_RECOMMENDED_BYTE_SIZE) out = null; } }
/** * Try and detect the character encoding in which the bytes contained by the given <code>InputStream</code> are * encoded, and returns the best guess or <code>null</code> if there is none (not enough data or confidence). * Note that the returned character encoding may or may not be available on the Java runtime -- use * <code>java.nio.Charset#isSupported(String)</code> to determine if it is available. * * <p>A maximum of {@link #MAX_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 InputStream that supplies the bytes * @return the best guess at the character encoding, null if there is none (not enough data or confidence) * @throws IOException if an error occurred while reading the stream */ public static String detectEncoding(InputStream in) throws IOException { byte buf[] = BufferPool.getByteArray(MAX_RECOMMENDED_BYTE_SIZE); try { return detectEncoding(buf, 0, StreamUtils.readUpTo(in, buf)); } finally { BufferPool.releaseByteArray(buf); } }
in = file.getInputStream(); String encoding = EncodingDetector.detectEncoding(in);
comment = getString(commentBytes, defaultEncoding!=null?defaultEncoding:EncodingDetector.detectEncoding(commentBytes));
String guessedEncoding = EncodingDetector.detectEncoding(encodingAccumulator.toByteArray());