]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/js/src/io/file.ts
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / src / io / file.ts
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 import { FileHandle } from './interfaces';
19 import { ByteStream, AsyncByteStream } from './stream';
20 import { ArrayBufferViewInput, toUint8Array } from '../util/buffer';
21
22 /** @ignore */
23 export class RandomAccessFile extends ByteStream {
24 public size: number;
25 public position = 0;
26 protected buffer: Uint8Array | null;
27 constructor(buffer: ArrayBufferViewInput, byteLength?: number) {
28 super();
29 this.buffer = toUint8Array(buffer);
30 this.size = typeof byteLength === 'undefined' ? this.buffer.byteLength : byteLength;
31 }
32 public readInt32(position: number) {
33 const { buffer, byteOffset } = this.readAt(position, 4);
34 return new DataView(buffer, byteOffset).getInt32(0, true);
35 }
36 public seek(position: number) {
37 this.position = Math.min(position, this.size);
38 return position < this.size;
39 }
40 public read(nBytes?: number | null) {
41 const { buffer, size, position } = this;
42 if (buffer && position < size) {
43 if (typeof nBytes !== 'number') { nBytes = Infinity; }
44 this.position = Math.min(size,
45 position + Math.min(size - position, nBytes));
46 return buffer.subarray(position, this.position);
47 }
48 return null;
49 }
50 public readAt(position: number, nBytes: number) {
51 const buf = this.buffer;
52 const end = Math.min(this.size, position + nBytes);
53 return buf ? buf.subarray(position, end) : new Uint8Array(nBytes);
54 }
55 public close() { this.buffer && (this.buffer = null); }
56 public throw(value?: any) { this.close(); return { done: true, value }; }
57 public return(value?: any) { this.close(); return { done: true, value }; }
58 }
59
60 /** @ignore */
61 export class AsyncRandomAccessFile extends AsyncByteStream {
62 public size!: number;
63 public position = 0;
64 public _pending?: Promise<void>;
65 protected _handle: FileHandle | null;
66 constructor(file: FileHandle, byteLength?: number) {
67 super();
68 this._handle = file;
69 if (typeof byteLength === 'number') {
70 this.size = byteLength;
71 } else {
72 this._pending = (async () => {
73 this.size = (await file.stat()).size;
74 delete this._pending;
75 })();
76 }
77 }
78 public async readInt32(position: number) {
79 const { buffer, byteOffset } = await this.readAt(position, 4);
80 return new DataView(buffer, byteOffset).getInt32(0, true);
81 }
82 public async seek(position: number) {
83 this._pending && await this._pending;
84 this.position = Math.min(position, this.size);
85 return position < this.size;
86 }
87 public async read(nBytes?: number | null) {
88 this._pending && await this._pending;
89 const { _handle: file, size, position } = this;
90 if (file && position < size) {
91 if (typeof nBytes !== 'number') { nBytes = Infinity; }
92 let pos = position, offset = 0, bytesRead = 0;
93 const end = Math.min(size, pos + Math.min(size - pos, nBytes));
94 const buffer = new Uint8Array(Math.max(0, (this.position = end) - pos));
95 while ((pos += bytesRead) < end && (offset += bytesRead) < buffer.byteLength) {
96 ({ bytesRead } = await file.read(buffer, offset, buffer.byteLength - offset, pos));
97 }
98 return buffer;
99 }
100 return null;
101 }
102 public async readAt(position: number, nBytes: number) {
103 this._pending && await this._pending;
104 const { _handle: file, size } = this;
105 if (file && (position + nBytes) < size) {
106 const end = Math.min(size, position + nBytes);
107 const buffer = new Uint8Array(end - position);
108 return (await file.read(buffer, 0, nBytes, position)).buffer;
109 }
110 return new Uint8Array(nBytes);
111 }
112 public async close() { const f = this._handle; this._handle = null; f && await f.close(); }
113 public async throw(value?: any) { await this.close(); return { done: true, value }; }
114 public async return(value?: any) { await this.close(); return { done: true, value }; }
115 }