OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library convert.hex.encoder; | 5 library convert.hex.encoder; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
9 | 9 |
10 import 'package:charcode/ascii.dart'; | 10 import 'package:charcode/ascii.dart'; |
11 | 11 |
12 /// The canonical instance of [HexEncoder]. | 12 /// The canonical instance of [HexEncoder]. |
13 const hexEncoder = const HexEncoder._(); | 13 const hexEncoder = const HexEncoder._(); |
14 | 14 |
15 /// A converter that encodes byte arrays into hexadecimal strings. | 15 /// A converter that encodes byte arrays into hexadecimal strings. |
16 /// | 16 /// |
17 /// This will throw a [RangeError] if the byte array has any digits that don't | 17 /// This will throw a [RangeError] if the byte array has any digits that don't |
18 /// fit in the gamut of a byte. | 18 /// fit in the gamut of a byte. |
19 class HexEncoder extends Converter<List<int>, String> { | 19 class HexEncoder |
| 20 extends ChunkedConverter<List<int>, String, List<int>, String> { |
20 const HexEncoder._(); | 21 const HexEncoder._(); |
21 | 22 |
22 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); | 23 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); |
23 | 24 |
24 ByteConversionSink startChunkedConversion(Sink<String> sink) => | 25 ByteConversionSink startChunkedConversion(Sink<String> sink) => |
25 new _HexEncoderSink(sink); | 26 new _HexEncoderSink(sink); |
26 } | 27 } |
27 | 28 |
28 /// A conversion sink for chunked hexadecimal encoding. | 29 /// A conversion sink for chunked hexadecimal encoding. |
29 class _HexEncoderSink extends ByteConversionSinkBase { | 30 class _HexEncoderSink extends ByteConversionSinkBase { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", | 80 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", |
80 bytes, i); | 81 bytes, i); |
81 } | 82 } |
82 | 83 |
83 throw 'unreachable'; | 84 throw 'unreachable'; |
84 } | 85 } |
85 | 86 |
86 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit | 87 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit |
87 /// [digit]. | 88 /// [digit]. |
88 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10; | 89 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10; |
OLD | NEW |