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 | 19 class HexEncoder extends Converter<List<int>, String> { |
20 extends ChunkedConverter<List<int>, String, List<int>, String> { | |
21 const HexEncoder._(); | 20 const HexEncoder._(); |
22 | 21 |
23 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); | 22 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); |
24 | 23 |
25 ByteConversionSink startChunkedConversion(Sink<String> sink) => | 24 ByteConversionSink startChunkedConversion(Sink<String> sink) => |
26 new _HexEncoderSink(sink); | 25 new _HexEncoderSink(sink); |
27 } | 26 } |
28 | 27 |
29 /// A conversion sink for chunked hexadecimal encoding. | 28 /// A conversion sink for chunked hexadecimal encoding. |
30 class _HexEncoderSink extends ByteConversionSinkBase { | 29 class _HexEncoderSink extends ByteConversionSinkBase { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", | 79 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", |
81 bytes, i); | 80 bytes, i); |
82 } | 81 } |
83 | 82 |
84 throw 'unreachable'; | 83 throw 'unreachable'; |
85 } | 84 } |
86 | 85 |
87 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit | 86 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit |
88 /// [digit]. | 87 /// [digit]. |
89 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10; | 88 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10; |
OLD | NEW |