| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkStream.h" | 10 #include "SkStream.h" |
| (...skipping 864 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 875 // file access. | 875 // file access. |
| 876 SkFILEStream* stream = new SkFILEStream(path); | 876 SkFILEStream* stream = new SkFILEStream(path); |
| 877 if (!stream->isValid()) { | 877 if (!stream->isValid()) { |
| 878 delete stream; | 878 delete stream; |
| 879 stream = nullptr; | 879 stream = nullptr; |
| 880 } | 880 } |
| 881 return stream; | 881 return stream; |
| 882 } | 882 } |
| 883 | 883 |
| 884 // Declared in SkStreamPriv.h: | 884 // Declared in SkStreamPriv.h: |
| 885 size_t SkCopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { | |
| 886 SkASSERT(storage != nullptr); | |
| 887 SkASSERT(stream != nullptr); | |
| 888 | |
| 889 if (stream->hasLength()) { | |
| 890 const size_t length = stream->getLength(); | |
| 891 void* dst = storage->reset(length); | |
| 892 if (stream->read(dst, length) != length) { | |
| 893 return 0; | |
| 894 } | |
| 895 return length; | |
| 896 } | |
| 897 | |
| 898 SkDynamicMemoryWStream tempStream; | |
| 899 // Arbitrary buffer size. | |
| 900 #if defined(GOOGLE3) | |
| 901 // Stack frame size is limited in GOOGLE3. | |
| 902 const size_t bufferSize = 8 * 1024; // 8KB | |
| 903 #else | |
| 904 const size_t bufferSize = 256 * 1024; // 256KB | |
| 905 #endif | |
| 906 char buffer[bufferSize]; | |
| 907 SkDEBUGCODE(size_t debugLength = 0;) | |
| 908 do { | |
| 909 size_t bytesRead = stream->read(buffer, bufferSize); | |
| 910 tempStream.write(buffer, bytesRead); | |
| 911 SkDEBUGCODE(debugLength += bytesRead); | |
| 912 SkASSERT(tempStream.bytesWritten() == debugLength); | |
| 913 } while (!stream->isAtEnd()); | |
| 914 const size_t length = tempStream.bytesWritten(); | |
| 915 void* dst = storage->reset(length); | |
| 916 tempStream.copyTo(dst); | |
| 917 return length; | |
| 918 } | |
| 919 | |
| 920 // Declared in SkStreamPriv.h: | |
| 921 SkData* SkCopyStreamToData(SkStream* stream) { | 885 SkData* SkCopyStreamToData(SkStream* stream) { |
| 922 SkASSERT(stream != nullptr); | 886 SkASSERT(stream != nullptr); |
| 923 | 887 |
| 924 if (stream->hasLength()) { | 888 if (stream->hasLength()) { |
| 925 return SkData::NewFromStream(stream, stream->getLength()); | 889 return SkData::NewFromStream(stream, stream->getLength()); |
| 926 } | 890 } |
| 927 | 891 |
| 928 SkDynamicMemoryWStream tempStream; | 892 SkDynamicMemoryWStream tempStream; |
| 929 const size_t bufferSize = 4096; | 893 const size_t bufferSize = 4096; |
| 930 char buffer[bufferSize]; | 894 char buffer[bufferSize]; |
| 931 do { | 895 do { |
| 932 size_t bytesRead = stream->read(buffer, bufferSize); | 896 size_t bytesRead = stream->read(buffer, bufferSize); |
| 933 tempStream.write(buffer, bytesRead); | 897 tempStream.write(buffer, bytesRead); |
| 934 } while (!stream->isAtEnd()); | 898 } while (!stream->isAtEnd()); |
| 935 return tempStream.copyToData(); | 899 return tempStream.copyToData(); |
| 936 } | 900 } |
| 937 | 901 |
| 938 SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) { | |
| 939 if (!stream) { | |
| 940 return nullptr; | |
| 941 } | |
| 942 SkAutoTDelete<SkStreamRewindable> dupStream(stream->duplicate()); | |
| 943 if (dupStream) { | |
| 944 return dupStream.detach(); | |
| 945 } | |
| 946 stream->rewind(); | |
| 947 if (stream->hasLength()) { | |
| 948 size_t length = stream->getLength(); | |
| 949 if (stream->hasPosition()) { // If stream has length, but can't rewind. | |
| 950 length -= stream->getPosition(); | |
| 951 } | |
| 952 SkAutoTUnref<SkData> data(SkData::NewFromStream(stream, length)); | |
| 953 return new SkMemoryStream(data.get()); | |
| 954 } | |
| 955 SkDynamicMemoryWStream tempStream; | |
| 956 const size_t bufferSize = 4096; | |
| 957 char buffer[bufferSize]; | |
| 958 do { | |
| 959 size_t bytesRead = stream->read(buffer, bufferSize); | |
| 960 tempStream.write(buffer, bytesRead); | |
| 961 } while (!stream->isAtEnd()); | |
| 962 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, | |
| 963 // cheaper than copying to SkData | |
| 964 } | |
| 965 | |
| 966 bool SkStreamCopy(SkWStream* out, SkStream* input) { | 902 bool SkStreamCopy(SkWStream* out, SkStream* input) { |
| 967 const char* base = static_cast<const char*>(input->getMemoryBase()); | 903 const char* base = static_cast<const char*>(input->getMemoryBase()); |
| 968 if (base && input->hasPosition() && input->hasLength()) { | 904 if (base && input->hasPosition() && input->hasLength()) { |
| 969 // Shortcut that avoids the while loop. | 905 // Shortcut that avoids the while loop. |
| 970 size_t position = input->getPosition(); | 906 size_t position = input->getPosition(); |
| 971 size_t length = input->getLength(); | 907 size_t length = input->getLength(); |
| 972 SkASSERT(length >= position); | 908 SkASSERT(length >= position); |
| 973 return out->write(&base[position], length - position); | 909 return out->write(&base[position], length - position); |
| 974 } | 910 } |
| 975 char scratch[4096]; | 911 char scratch[4096]; |
| 976 size_t count; | 912 size_t count; |
| 977 while (true) { | 913 while (true) { |
| 978 count = input->read(scratch, sizeof(scratch)); | 914 count = input->read(scratch, sizeof(scratch)); |
| 979 if (0 == count) { | 915 if (0 == count) { |
| 980 return true; | 916 return true; |
| 981 } | 917 } |
| 982 if (!out->write(scratch, count)) { | 918 if (!out->write(scratch, count)) { |
| 983 return false; | 919 return false; |
| 984 } | 920 } |
| 985 } | 921 } |
| 986 } | 922 } |
| OLD | NEW |