anchor.diff:  
   -module(thrift_framed_transport).
   
  --behaviour(gen_server).
   -behaviour(thrift_transport).
   
   %% API
   -export([new/1]).
  -
  -%% gen_server callbacks
  --export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  -         terminate/2, code_change/3]).
   
   %% thrift_transport callbacks
   -export([write/2, read/2, flush/1, close/1]).
  
positive.code:     <<Data:Give/binary, RBuf2/binary>> = iolist_to_binary(RBuf1),
  
      Response = {ok, Data},
      State1 = State#framed_transport{wrapped = Wrapped1, read_buffer=RBuf2},
  
      {reply, Response, State1};
  
  handle_call(flush, _From, State) ->
      {Response, State1} = do_flush(State),
      {reply, Response, State1}.
  
  %%--------------------------------------------------------------------
  %% Function: handle_cast(Msg, State) -> {noreply, State} |
  %%                                      {noreply, State, Timeout} |
  %%                                      {stop, Reason, State}
  %% Description: Handling cast messages
  %%--------------------------------------------------------------------
  handle_cast(close, State) ->
      %% Wrapped is closed by terminate/2
      %%  error_logger:info_msg("thrift_framed_transport ~p: closing", [self()]),
      {stop, normal, State};
  handle_cast(Msg, State=#framed_transport{}) ->
      {noreply, State}.
  
  %%--------------------------------------------------------------------
  %% Function: handle_info(Info, State) -> {noreply, State} |
  %%                                       {noreply, State, Timeout} |
  %%                                       {stop, Reason, State}
  %% Description: Handling all non call/cast messages
  %%--------------------------------------------------------------------
  handle_info(_Info, State) ->
      {noreply, State}.
  
  %%--------------------------------------------------------------------
  %% Function: terminate(Reason, State) -> void()
  %% Description: This function is called by a gen_server when it is about to
  %% terminate. It should be the opposite of Module:init/1 and do any necessary
  %% cleaning up. When it returns, the gen_server terminates with Reason.
  %% The return value is ignored.
  %%--------------------------------------------------------------------
  terminate(_Reason, State = #framed_transport{wrapped=Wrapped}) ->
      thrift_transport:close(Wrapped),
      ok.
  
  %%--------------------------------------------------------------------
  %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
  %% Description: Convert process state when code is changed
  %%--------------------------------------------------------------------
  code_change(_OldVsn, State, _Extra) ->
      {ok, State}.
  
  %%--------------------------------------------------------------------
  %%% Internal functions
  %%--------------------------------------------------------------------
  do_flush(State = #framed_transport{write_buffer = Buffer,
                                     wrapped = Wrapped0}) ->
      FrameLen = iolist_size(Buffer),
      Data     = [<<FrameLen:32/integer-signed-big>>, Buffer],
  
      {Wrapped1, Response} = thrift_transport:write(Wrapped0, Data),
  
      {Wrapped2, _} = thrift_transport:flush(Wrapped1),
  
      State1 = State#framed_transport{wrapped = Wrapped2, write_buffer = []},
      {Response, State1}.
  
  min(A,B) when A<B -> A;
  min(_,B)          -> B.
  
negative.code:                            read_buffer, % iolist()
                             write_buffer % iolist()
                            }).
  -type state() :: #framed_transport{}.
  -include("thrift_transport_impl.hrl").
  
  new(WrappedTransport) ->
      State = #framed_transport{wrapped = WrappedTransport,
                                read_buffer = [],
                                write_buffer = []},
      thrift_transport:new(?MODULE, State).
  
  %% Writes data into the buffer
  write(State = #framed_transport{write_buffer = WBuf}, Data) ->
      {State#framed_transport{write_buffer = [WBuf, Data]}, ok}.
  
  %% Flushes the buffer through to the wrapped transport
  flush(State0 = #framed_transport{write_buffer = Buffer,
                                     wrapped = Wrapped0}) ->
      FrameLen = iolist_size(Buffer),
      Data     = [<<FrameLen:32/integer-signed-big>>, Buffer],
  
      {Wrapped1, Response} = thrift_transport:write(Wrapped0, Data),
  
      {Wrapped2, _} = thrift_transport:flush(Wrapped1),
  
      State1 = State0#framed_transport{wrapped = Wrapped2, write_buffer = []},
      {State1, Response}.
  
  %% Closes the transport and the wrapped transport
  close(State = #framed_transport{wrapped = Wrapped0}) ->
      {Wrapped1, Result} = thrift_transport:close(Wrapped0),
      NewState = State#framed_transport{wrapped = Wrapped1},
      {NewState, Result}.
  
  %% Reads data through from the wrapped transoprt
  read(State0 = #framed_transport{wrapped = Wrapped0, read_buffer = RBuf},
       Len) when is_integer(Len) ->
      {Wrapped1, {RBuf1, RBuf1Size}} =
          %% if the read buffer is empty, read another frame
          %% otherwise, just read from what's left in the buffer
  