Skip to content

Commit

Permalink
added TMemoryStream to hx
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens-G committed Feb 11, 2025
1 parent f98e721 commit d26da26
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/haxe/src/org/apache/thrift/TConfiguration.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
89 changes: 89 additions & 0 deletions lib/haxe/src/org/apache/thrift/transport/TMemoryStream.hx
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit d26da26

Please sign in to comment.