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 870 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
881 if (stream->hasLength()) { | 881 if (stream->hasLength()) { |
882 const size_t length = stream->getLength(); | 882 const size_t length = stream->getLength(); |
883 void* dst = storage->reset(length); | 883 void* dst = storage->reset(length); |
884 if (stream->read(dst, length) != length) { | 884 if (stream->read(dst, length) != length) { |
885 return 0; | 885 return 0; |
886 } | 886 } |
887 return length; | 887 return length; |
888 } | 888 } |
889 | 889 |
890 SkDynamicMemoryWStream tempStream; | 890 SkDynamicMemoryWStream tempStream; |
891 // Arbitrary buffer size. | 891 if (!SkStreamCopy(&tempStream, stream)) { |
892 #if defined(GOOGLE3) | 892 return 0; |
893 // Stack frame size is limited in GOOGLE3. | 893 } |
894 const size_t bufferSize = 8 * 1024; // 8KB | |
895 #else | |
896 const size_t bufferSize = 256 * 1024; // 256KB | |
897 #endif | |
898 char buffer[bufferSize]; | |
899 SkDEBUGCODE(size_t debugLength = 0;) | |
900 do { | |
901 size_t bytesRead = stream->read(buffer, bufferSize); | |
902 tempStream.write(buffer, bytesRead); | |
903 SkDEBUGCODE(debugLength += bytesRead); | |
904 SkASSERT(tempStream.bytesWritten() == debugLength); | |
905 } while (!stream->isAtEnd()); | |
906 const size_t length = tempStream.bytesWritten(); | 894 const size_t length = tempStream.bytesWritten(); |
907 void* dst = storage->reset(length); | 895 void* dst = storage->reset(length); |
908 tempStream.copyTo(dst); | 896 tempStream.copyTo(dst); |
909 return length; | 897 return length; |
910 } | 898 } |
911 | 899 |
912 // Declared in SkStreamPriv.h: | 900 // Declared in SkStreamPriv.h: |
913 SkData* SkCopyStreamToData(SkStream* stream) { | 901 SkData* SkCopyStreamToData(SkStream* stream) { |
914 SkASSERT(stream != nullptr); | 902 SkASSERT(stream != nullptr); |
915 | 903 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
969 while (true) { | 957 while (true) { |
970 count = input->read(scratch, sizeof(scratch)); | 958 count = input->read(scratch, sizeof(scratch)); |
971 if (0 == count) { | 959 if (0 == count) { |
972 return true; | 960 return true; |
973 } | 961 } |
974 if (!out->write(scratch, count)) { | 962 if (!out->write(scratch, count)) { |
975 return false; | 963 return false; |
976 } | 964 } |
977 } | 965 } |
978 } | 966 } |
967 | |
968 // TODO: optimize for the special case when the input is SkMemoryStream. | |
scroggo
2016/01/08 19:11:49
The optimization is actually elsewhere - you shoul
yujieqin
2016/01/11 14:03:08
Not sure what you mean here, I moved the TODO into
| |
969 bool SkStreamCopy(SkDynamicMemoryWStream* out, SkStream* input) { | |
970 // Arbitrary buffer size. | |
971 #if defined(GOOGLE3) | |
972 // Stack frame size is limited in GOOGLE3. | |
973 const size_t bufferSize = 8 * 1024; // 8KB | |
974 #else | |
975 const size_t bufferSize = 256 * 1024; // 256KB | |
976 #endif | |
977 char buffer[bufferSize]; | |
978 SkDEBUGCODE(size_t debugLength = 0;) | |
979 do { | |
980 size_t bytesRead = input->read(buffer, bufferSize); | |
981 if (!out->write(buffer, bytesRead)) { | |
982 return false; | |
983 } | |
984 SkDEBUGCODE(debugLength += bytesRead); | |
985 SkASSERT(out->bytesWritten() == debugLength); | |
986 } while (!input->isAtEnd()); | |
987 return true; | |
988 } | |
OLD | NEW |