String FileReaderLoader::stringResult()
{
    ASSERT(m_readType != ReadAsArrayBuffer && m_readType != ReadAsBlob);

    // If the loading is not started or an error occurs, return an empty result.
    if (!m_rawData || m_errorCode)
        return m_stringResult;

    // If already converted from the raw data, return the result now.
    if (m_isRawDataConverted)
        return m_stringResult;

    switch (m_readType) {
    case ReadAsArrayBuffer:
        // No conversion is needed.
        break;
    case ReadAsBinaryString:
        m_stringResult = String(static_cast<const char*>(m_rawData->data()), m_bytesLoaded);
        break;
    case ReadAsText:
        convertToText();
        break;
    case ReadAsDataURL:
        // Partial data is not supported when reading as data URL.
        if (isCompleted())
            convertToDataURL();
        break;
    default:
        ASSERT_NOT_REACHED();
    }
    
    return m_stringResult;
}
void FileReaderSync::read(ScriptExecutionContext* scriptExecutionContext, Blob* blob, ReadType readType, ExceptionCode& ec)
{
    // The blob is read by routing through the request handling layer given the temporary public url.
    KURL urlForReading = BlobURL::createPublicURL(scriptExecutionContext->securityOrigin());
    ThreadableBlobRegistry::registerBlobURL(urlForReading, blob->url());

    ResourceRequest request(urlForReading);
    request.setHTTPMethod("GET");

    FileReaderSyncLoader loader((readType == ReadAsBinaryString) ? &m_result : 0);
    loader.start(scriptExecutionContext, request, ec);
    ThreadableBlobRegistry::unregisterBlobURL(urlForReading);
    if (ec)
        return;

    switch (readType) {
    case ReadAsBinaryString:
        // Nothing to do since we need no conversion.
        return;
    case ReadAsText:
        convertToText(loader.rawData().data(), loader.rawData().size(), m_result);
        return;
    case ReadAsDataURL:
        FileReader::convertToDataURL(loader.rawData(), blob->type(), m_result);
        return;
    }

    ASSERT_NOT_REACHED();
}
Beispiel #3
0
String FileReaderLoader::stringResult()
{
    ASSERT(m_readType != ReadAsArrayBuffer && m_readType != ReadAsBlob && m_readType != ReadByClient);

    // If the loading is not started or an error occurs, return an empty result.
    if (!m_rawData || m_errorCode)
        return m_stringResult;

    // If already converted from the raw data, return the result now.
    if (m_isRawDataConverted)
        return m_stringResult;

    switch (m_readType) {
    case ReadAsArrayBuffer:
        // No conversion is needed.
        break;
    case ReadAsBinaryString:
        m_stringResult = m_rawData->toString();
        m_isRawDataConverted = true;
        break;
    case ReadAsText:
        convertToText();
        break;
    case ReadAsDataURL:
        // Partial data is not supported when reading as data URL.
        if (m_finishedLoading)
            convertToDataURL();
        break;
    default:
        ASSERT_NOT_REACHED();
    }

    return m_stringResult;
}
const ScriptString& FileReader::result()
{
    // If reading as binary string, we can return the result immediately.
    if (m_readType == ReadFileAsBinaryString)
        return m_result;

    // If we already convert the raw data received so far, we can return the result now.
    if (m_isRawDataConverted)
        return m_result;
    m_isRawDataConverted = true;

    if (m_readType == ReadFileAsText)
        convertToText();
    // For data URL, we only do the coversion until we receive all the raw data.
    else if (m_readType == ReadFileAsDataURL && m_state == Completed)
        convertToDataURL(m_rawData, m_fileType, m_result);

    return m_result;
}