Sha256: dccc8b462069a6bbc3ac665ba627e7d3093943e0744447b831b0e0115b68b43a

Contents?: true

Size: 1.8 KB

Versions: 28

Compression:

Stored size: 1.8 KB

Contents

-module(thrift_file_transport).
-author(todd@amiestreet.com).

-behaviour(thrift_transport).

-export([new_reader/1,
         new/1,
         new/2,
         write/2, read/2, flush/1, close/1]).

-record(t_file_transport, {device,
                           should_close = true,
                           mode = write}).

%%%% CONSTRUCTION   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

new_reader(Filename) ->
    case file:open(Filename, [read, binary, {read_ahead, 1024*1024}]) of
        {ok, IODevice} ->
            new(IODevice, [{should_close, true}, {mode, read}]);
        Error -> Error
    end.

new(Device) ->
    new(Device, []).

%% Device :: io_device()
%%
%% Device should be opened in raw and binary mode.
new(Device, Opts) when is_list(Opts) ->
    State = parse_opts(Opts, #t_file_transport{device = Device}),
    thrift_transport:new(?MODULE, State).


%% Parse options
parse_opts([{should_close, Bool} | Rest], State) when is_boolean(Bool) ->
    parse_opts(Rest, State#t_file_transport{should_close = Bool});
parse_opts([{mode, Mode} | Rest], State)
  when Mode =:= write;
       Mode =:= read ->
    parse_opts(Rest, State#t_file_transport{mode = Mode});
parse_opts([], State) ->
     State.


%%%% TRANSPORT IMPL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

write(#t_file_transport{device = Device, mode = write}, Data) ->
    file:write(Device, Data);
write(_T, _D) ->
    {error, read_mode}.


read(#t_file_transport{device = Device, mode = read}, Len)
  when is_integer(Len), Len >= 0 ->
    file:read(Device, Len);
read(_T, _D) ->
    {error, read_mode}.

flush(#t_file_transport{device = Device, mode = write}) ->
    file:sync(Device).

close(#t_file_transport{device = Device, should_close = SC}) ->
    case SC of
        true ->
            file:close(Device);
        false ->
            ok
    end.

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
auser-poolparty-1.3.0 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.1 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.10 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.11 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.12 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.13 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.14 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.15 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.16 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.17 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.2 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.3 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.4 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.5 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.6 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.7 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
auser-poolparty-1.3.8 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
fairchild-poolparty-1.3.17 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
fairchild-poolparty-1.3.5 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl
poolparty-1.3.15 examples/thrift/erlang/deps/thrift/src/thrift_file_transport.erl