https://github.com/mezoni/string_reader
Implementation of random access string readers for reading data from various data sources in various encodings through a single `StringReader` interface.
https://github.com/mezoni/string_reader
dart dart3 dartlang file-reader string-reader
Last synced: 20 days ago
JSON representation
Implementation of random access string readers for reading data from various data sources in various encodings through a single `StringReader` interface.
- Host: GitHub
- URL: https://github.com/mezoni/string_reader
- Owner: mezoni
- License: bsd-3-clause
- Created: 2023-09-27T17:00:30.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-27T17:00:38.000Z (over 2 years ago)
- Last Synced: 2025-01-13T05:43:38.570Z (over 1 year ago)
- Topics: dart, dart3, dartlang, file-reader, string-reader
- Language: Dart
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# string_readers
Implementation of random access string readers for reading data from various data sources in various encodings through a single `StringReader` interface.
Version: 0.1.0
## About this software
Implementation of random access string readers for reading data from various data sources in various encodings through a single `StringReader` interface.
Possible application of this is the organization of access operations to data located in large files, as well as the organization of access to data from various sources through a single `StringReader` interface.
The current version includes the following implementations:
- ByteReader
- FileReader
- StringReader
- Utf8Reader
These implementations allow random read access to string input data from the following sources:
- String data encoded in Utf-16 encoding
- Byte data encoded in Utf-8 encoding
- String data located in files
## Example
An example of accessing data from various sources through a single `StringReader` interface.
```dart
import 'dart:convert';
import 'dart:io';
import 'package:string_reader/file_reader.dart';
import 'package:string_reader/string_reader.dart';
import 'package:string_reader/utf8_reader.dart';
void main(List args) {
const source = 'Hello';
File? file;
RandomAccessFile? fp;
try {
const filename = 'example/temp.txt';
file = File(filename);
_writeToFile(file, source);
fp = file.openSync();
final result = _readUsingFileReader(fp);
print(result);
} catch (e) {
rethrow;
} finally {
if (fp != null) {
fp.closeSync();
}
if (file != null) {
if (file.existsSync()) {
file.deleteSync();
}
}
}
final stringReader = StringReader(source);
final result = _readUsingStringReader(stringReader);
print(result);
}
String _readUsingFileReader(RandomAccessFile fp) {
const bufferSize = 8;
final fileReader = FileReader(fp, bufferSize: bufferSize);
final utf8Reader = Utf8Reader(fileReader);
return _readUsingStringReader(utf8Reader);
}
String _readUsingStringReader(StringReader reader) {
final charCodes = [];
var position = 0;
while (position < reader.length) {
final char = reader.readChar(position);
position += reader.count;
charCodes.add(char);
}
return String.fromCharCodes(charCodes);
}
void _writeToFile(File file, String string) {
if (file.existsSync()) {
file.deleteSync();
}
final bytes = Utf8Encoder().convert('Hello');
file.writeAsBytesSync(bytes);
}
```