documentation and cleanup

This commit is contained in:
Vincent Breitmoser
2016-02-22 23:38:02 +01:00
parent 4df63ccdeb
commit a0c90f0ad5
3 changed files with 24 additions and 4 deletions

View File

@@ -12,7 +12,14 @@ import android.content.ClipDescription;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
/** This class can be used to guess whether a stream of data is encoded in a given
* charset or not.
*
* An object of this class must be initialized with a byte[] buffer, which should
* be filled with data, then processed with {@link #readBytesFromBuffer}. This can
* be done any number of times. Once all data has been read, a final status can be
* read using the getter methods.
*/
public class CharsetVerifier {
private final ByteBuffer bufWrap;
@@ -58,7 +65,7 @@ public class CharsetVerifier {
charsetDecoder.reset();
}
public void write(int pos, int len) {
public void readBytesFromBuffer(int pos, int len) {
if (isFinished) {
throw new IllegalStateException("cannot write again after reading charset status!");
}
@@ -111,12 +118,25 @@ public class CharsetVerifier {
return charset;
}
/** Returns true if the data which was read is definitely binary.
*
* This can happen when either the supplied mimeType indicated a non-ambiguous
* binary data type, or if we guessed a charset but got errors while decoding.
*/
public boolean isDefinitelyBinary() {
finishIfNecessary();
return !isTextMimeType && (!isPossibleTextMimeType || (isGuessed && isFaulty));
}
/** Returns true iff the data which was read is probably (or
* definitely) text.
*
* The corner case where isDefinitelyBinary returns false but isProbablyText
* returns true is where the charset was provided by the data (so is not
* guessed) but is still faulty.
*/
public boolean isProbablyText() {
finishIfNecessary();
return isTextMimeType || isPossibleTextMimeType && (!isGuessed || !isFaulty);
}
}

View File

@@ -335,7 +335,7 @@ public class InputDataOperation extends BaseOperation<InputDataParcel> {
do {
totalLength += len;
out.write(buf, 0, len);
charsetVerifier.write(0, len);
charsetVerifier.readBytesFromBuffer(0, len);
} while ((len = is.read(buf)) > 0);
log.add(LogType.MSG_DATA_MIME_LENGTH, 3, Long.toString(totalLength));

View File

@@ -449,7 +449,7 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
// update signature buffer if signature is also present
signatureChecker.updateSignatureData(buffer, 0, length);
charsetVerifier.write(0, length);
charsetVerifier.readBytesFromBuffer(0, length);
// note down first couple of bytes for "magic bytes" file type detection
if (alreadyWritten == 0) {