@@ -7,18 +7,9 @@ import { type BIDSFile, FileTree } from '../types/filetree.ts'
77import { requestReadPermission } from '../setup/requestPermissions.ts'
88import { FileIgnoreRules , readBidsIgnore } from './ignore.ts'
99import { logger } from '../utils/logger.ts'
10+ import { createUTF8Stream } from './streams.ts'
1011export { type BIDSFile , FileTree }
1112
12- /**
13- * Thrown when a text file is decoded as UTF-8 but contains UTF-16 characters
14- */
15- export class UnicodeDecodeError extends Error {
16- constructor ( message : string ) {
17- super ( message )
18- this . name = 'UnicodeDecode'
19- }
20- }
21-
2213/**
2314 * Deno implementation of BIDSFile
2415 */
@@ -67,27 +58,17 @@ export class BIDSFileDeno implements BIDSFile {
6758 * Read the entire file and decode as utf-8 text
6859 */
6960 async text ( ) : Promise < string > {
70- const streamReader = this . stream
71- . pipeThrough ( new TextDecoderStream ( 'utf-8' ) )
72- . getReader ( )
73- let data = ''
61+ const reader = this . stream . pipeThrough ( createUTF8Stream ( ) ) . getReader ( )
62+ const chunks : string [ ] = [ ]
7463 try {
75- // Read once to check for unicode issues
76- const { done, value } = await streamReader . read ( )
77- // Check for UTF-16 BOM
78- if ( value && value . startsWith ( '\uFFFD' ) ) {
79- throw new UnicodeDecodeError ( 'This file appears to be UTF-16' )
80- }
81- if ( done ) return data
82- data += value
83- // Continue reading the rest of the file if no unicode issues were found
8464 while ( true ) {
85- const { done, value } = await streamReader . read ( )
86- if ( done ) return data
87- data += value
65+ const { done, value } = await reader . read ( )
66+ if ( done ) break
67+ chunks . push ( value )
8868 }
69+ return chunks . join ( '' )
8970 } finally {
90- streamReader . releaseLock ( )
71+ reader . releaseLock ( )
9172 }
9273 }
9374
0 commit comments