From d26da26c9ab654ff4b7e97f11e96b07f7eadb1da Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Fri, 7 Feb 2025 01:58:30 +0100 Subject: [PATCH] added TMemoryStream to hx --- .../src/org/apache/thrift/TConfiguration.hx | 6 +- .../apache/thrift/transport/TMemoryStream.hx | 89 +++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 lib/haxe/src/org/apache/thrift/transport/TMemoryStream.hx diff --git a/lib/haxe/src/org/apache/thrift/TConfiguration.hx b/lib/haxe/src/org/apache/thrift/TConfiguration.hx index 47973f2c2c0..3bc5a864f8c 100644 --- a/lib/haxe/src/org/apache/thrift/TConfiguration.hx +++ b/lib/haxe/src/org/apache/thrift/TConfiguration.hx @@ -25,9 +25,9 @@ class TConfiguration public static inline var DEFAULT_MAX_FRAME_SIZE = 16384000; // this value is used consistently across all Thrift libraries public static inline var DEFAULT_RECURSION_DEPTH = 64; - public var MaxMessageSize(default,null) : Int = DEFAULT_MAX_MESSAGE_SIZE; - public var MaxFrameSize(default,null) : Int = DEFAULT_MAX_FRAME_SIZE; - public var RecursionLimit(default,null) : Int = DEFAULT_RECURSION_DEPTH; + public var MaxMessageSize(default,default) : Int = DEFAULT_MAX_MESSAGE_SIZE; + public var MaxFrameSize(default,default) : Int = DEFAULT_MAX_FRAME_SIZE; + public var RecursionLimit(default,default) : Int = DEFAULT_RECURSION_DEPTH; // TODO(JensG): add connection and i/o timeouts diff --git a/lib/haxe/src/org/apache/thrift/transport/TMemoryStream.hx b/lib/haxe/src/org/apache/thrift/transport/TMemoryStream.hx new file mode 100644 index 00000000000..e7ef36a9bbe --- /dev/null +++ b/lib/haxe/src/org/apache/thrift/transport/TMemoryStream.hx @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.Output; + + +class TMemoryStream implements TStream { + + private var Data : Bytes; + public var Position(default,default) : Int; + + public function new( data : Bytes = null) { + var target = new BytesBuffer(); + if ( data != null) { + for ( i in 0...data.length) { + target.addByte( data.get(i)); + ++Position; + } + } + Data = target.getBytes(); + } + + private function IsEof() : Bool { + return (0 > Position) || (Position >= Data.length); + } + + public function Close() : Void { + var target = new BytesBuffer(); + Data = target.getBytes(); + Position = 0; + } + + public function Peek() : Bool { + return (! IsEof()); + } + + // read count bytes into buf starting at offset + public function Read( buf : Bytes, offset : Int, count : Int) : Int { + var numRead = 0; + + for ( i in 0...count) { + if ( IsEof()) + break; + + buf.set( offset + i, Data.get( Position++)); + ++numRead; + } + + return numRead; + } + + // write count bytes from buf starting at offset + public function Write( buf : Bytes, offset : Int, count : Int) : Void { + var numBytes = buf.length - offset; + if ( numBytes > count) { + numBytes = count; + } + + for ( i in 0...numBytes) { + Data.set( Position + i, buf.get( offset + i)); + } + } + + public function Flush() : Void { + // nothing to do + } + +} +