Skip to content
fumoboy007 edited this page Apr 19, 2020 · 6 revisions

CharacterEncodingDetector

Detects the character encoding of a sequence of bytes.

public final class CharacterEncodingDetector

Initializers

init()

public init()

Methods

detectCharacterEncoding(ofFileAt:on:completionQueue:completionHandler:)

Detects the character encoding of a file.

@discardableResult public static func detectCharacterEncoding(ofFileAt fileURL: URL, on queue: DispatchQueue, completionQueue: DispatchQueue, completionHandler: @escaping (_ characterEncoding: String?) -> Void) throws -> () -> Void

Parameters

  • fileURL: The URL of the target file. The file does not need to be seekable.
  • queue: The dispatch queue on which to perform the analysis.
  • completionQueue: The dispatch queue on which to call the completion handler.
  • completionHandler: The closure to call after the potentially-asynchronous operation is finished.
  • characterEncoding: The iconv-compatible identifier of the detected character encoding, or nil if detection failed.

Throws

An error if the file could not be opened.

Returns

A closure to cancel the operation. The completion handler will still be called even if the operation is canceled.

detectCharacterEncoding(ofDataFromFileDescriptor:on:completionQueue:completionHandler:)

Detects the character encoding of a file.

@discardableResult public static func detectCharacterEncoding(ofDataFromFileDescriptor fileDescriptor: Int32, on queue: DispatchQueue, completionQueue: DispatchQueue, completionHandler: @escaping (_ characterEncoding: String?) -> Void) -> () -> Void

Attention: This method may modify the file offset of the file descriptor.

Parameters

  • fileDescriptor: The file descriptor of the target file. The file descriptor does not need to be seekable.
  • queue: The dispatch queue on which to perform the analysis.
  • completionQueue: The dispatch queue on which to call the completion handler.
  • completionHandler: The closure to call after the potentially-asynchronous operation is finished.
  • characterEncoding: The iconv-compatible identifier of the detected character encoding, or nil if detection failed.

Returns

A closure to cancel the operation. The completion handler will still be called even if the operation is canceled.

reset()

Resets the detector so that it can analyze a new sequence of bytes.

public func reset()

analyzeNextChunk(_:)

Analyzes the next chunk of bytes in the sequence.

public func analyzeNextChunk<T: DataProtocol>(_ data: T) -> Bool

If the operation fails, you may call finish() to get a best-effort detection result.

Precondition: After finish() is called, reset() must be called before a new analysis can performed.

Parameters

  • data: The next chunk of bytes in the sequence.

Returns

Whether the operation was successful. The operation may fail, for example, if there is no more available memory.

finish()

Finishes the analysis and returns the detection result.

public func finish() -> String?

In order to get the most accurate result, call this method only after feeding all the bytes in the sequence to the detector.

Precondition: After this method is called, reset() must be called before a new analysis can performed.

Returns

The iconv-compatible identifier of the detected character encoding, or nil if detection failed.