好友
阅读权限25
听众
最后登录1970-1-1
|
楼主|
无敌小儿
发表于 2015-1-9 15:59
package org.jsoup.helper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
public class DataUtil
{
private static final int bufferSize = 131072;
private static final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)");
static final String defaultCharset = "UTF-8";
static String getCharsetFromContentType(String paramString)
{
String str;
if (paramString == null)
str = null;
do
{
do
{
return str;
Matcher localMatcher = charsetPattern.matcher(paramString);
if (!localMatcher.find())
break;
str = localMatcher.group(1).trim();
}
while (Charset.isSupported(str));
str = str.toUpperCase(Locale.ENGLISH);
}
while (Charset.isSupported(str));
return null;
}
// ERROR //
public static Document load(java.io.File paramFile, String paramString1, String paramString2)
throws IOException
{
// Byte code:
// 0: aconst_null
// 1: astore_3
// 2: new 72 java/io/FileInputStream
// 5: dup
// 6: aload_0
// 7: invokespecial 75 java/io/FileInputStream:<init> (Ljava/io/File;)V
// 10: astore 4
// 12: aload 4
// 14: invokestatic 79 org/jsoup/helper/DataUtil:readToByteBuffer (Ljava/io/InputStream;)Ljava/nio/ByteBuffer;
// 17: aload_1
// 18: aload_2
// 19: invokestatic 85 org/jsoup/parser/Parser:htmlParser ()Lorg/jsoup/parser/Parser;
// 22: invokestatic 89 org/jsoup/helper/DataUtil:parseByteData (Ljava/nio/ByteBuffer;Ljava/lang/String;Ljava/lang/String;Lorg/jsoup/parser/Parser;)Lorg/jsoup/nodes/Document;
// 25: astore 6
// 27: aload 4
// 29: ifnull +8 -> 37
// 32: aload 4
// 34: invokevirtual 92 java/io/FileInputStream:close ()V
// 37: aload 6
// 39: areturn
// 40: astore 5
// 42: aload_3
// 43: ifnull +7 -> 50
// 46: aload_3
// 47: invokevirtual 92 java/io/FileInputStream:close ()V
// 50: aload 5
// 52: athrow
// 53: astore 5
// 55: aload 4
// 57: astore_3
// 58: goto -16 -> 42
//
// Exception table:
// from to target type
// 2 12 40 finally
// 12 27 53 finally
}
public static Document load(InputStream paramInputStream, String paramString1, String paramString2)
throws IOException
{
return parseByteData(readToByteBuffer(paramInputStream), paramString1, paramString2, Parser.htmlParser());
}
public static Document load(InputStream paramInputStream, String paramString1, String paramString2, Parser paramParser)
throws IOException
{
return parseByteData(readToByteBuffer(paramInputStream), paramString1, paramString2, paramParser);
}
static Document parseByteData(ByteBuffer paramByteBuffer, String paramString1, String paramString2, Parser paramParser)
{
String str1;
Element localElement;
String str2;
if (paramString1 == null)
{
str1 = Charset.forName("UTF-8").decode(paramByteBuffer).toString();
localDocument = paramParser.parseInput(str1, paramString2);
localElement = localDocument.select("meta[http-equiv=content-type], meta[charset]").first();
if (localElement != null)
{
if (!localElement.hasAttr("http-equiv"))
break label168;
str2 = getCharsetFromContentType(localElement.attr("content"));
if ((str2 != null) && (str2.length() != 0) && (!str2.equals("UTF-8")))
{
paramString1 = str2;
paramByteBuffer.rewind();
str1 = Charset.forName(str2).decode(paramByteBuffer).toString();
}
}
}
for (Document localDocument = null; ; localDocument = null)
{
if (localDocument == null)
{
if ((str1.length() > 0) && (str1.charAt(0) == 65279))
str1 = str1.substring(1);
localDocument = paramParser.parseInput(str1, paramString2);
localDocument.outputSettings().charset(paramString1);
}
return localDocument;
label168: str2 = localElement.attr("charset");
break;
Validate.notEmpty(paramString1, "Must set charset arg to character set of file to parse. Set to null to attempt to detect from HTML");
str1 = Charset.forName(paramString1).decode(paramByteBuffer).toString();
}
}
static ByteBuffer readToByteBuffer(InputStream paramInputStream)
throws IOException
{
return readToByteBuffer(paramInputStream, 0);
}
static ByteBuffer readToByteBuffer(InputStream paramInputStream, int paramInt)
throws IOException
{
boolean bool;
int i;
label18: byte[] arrayOfByte;
ByteArrayOutputStream localByteArrayOutputStream;
int j;
if (paramInt >= 0)
{
bool = true;
Validate.isTrue(bool, "maxSize must be 0 (unlimited) or larger");
if (paramInt <= 0)
break label66;
i = 1;
arrayOfByte = new byte[131072];
localByteArrayOutputStream = new ByteArrayOutputStream(131072);
j = paramInt;
}
while (true)
{
int k = paramInputStream.read(arrayOfByte);
if (k == -1);
while (true)
{
return ByteBuffer.wrap(localByteArrayOutputStream.toByteArray());
bool = false;
break;
label66: i = 0;
break label18;
if (i == 0)
break label102;
if (k <= j)
break label95;
localByteArrayOutputStream.write(arrayOfByte, 0, j);
}
label95: j -= k;
label102: localByteArrayOutputStream.write(arrayOfByte, 0, k);
}
}
} |
|