2020import java .util .List ;
2121import java .util .Map ;
2222import java .util .Objects ;
23+ import java .util .function .Supplier ;
2324import java .util .stream .Collectors ;
2425
2526public class Response {
@@ -83,7 +84,7 @@ public String toString() {
8384 }
8485
8586 @ SuppressWarnings ("unchecked" )
86- public static Response readFromSocket (ConnectionSocket socket ) {
87+ public static Supplier < Response > readFromSocket (ConnectionSocket socket ) {
8788 final ByteBuffer header = socket .read (12 );
8889 final long token = header .getLong ();
8990 final int responseLength = header .getInt ();
@@ -100,20 +101,22 @@ public static Response readFromSocket(ConnectionSocket socket) {
100101 );
101102 }
102103
103- Map <String , Object > json = Internals .readJson (buffer );
104- return new Response (
105- token ,
106- ResponseType .fromValue (((Long ) json .get ("t" )).intValue ()),
107- (List <Object >) json .getOrDefault ("r" , Collections .emptyList ()),
108- ((List <Long >) json .getOrDefault ("n" , Collections .emptyList ()))
109- .stream ()
110- .map (Long ::intValue )
111- .map (ResponseNote ::maybeFromValue )
112- .filter (Objects ::nonNull )
113- .collect (Collectors .toList ()),
114- Profile .fromList ((List <Object >) json .get ("p" )),
115- Backtrace .fromList ((List <Object >) json .getOrDefault ("b" , null )),
116- json .containsKey ("e" ) ? ErrorType .maybeFromValue (((Long ) json .get ("e" )).intValue ()) : null
117- );
104+ return () -> {
105+ Map <String , Object > json = Internals .readJson (buffer );
106+ return new Response (
107+ token ,
108+ ResponseType .fromValue (((Long ) json .get ("t" )).intValue ()),
109+ (List <Object >) json .getOrDefault ("r" , Collections .emptyList ()),
110+ ((List <Long >) json .getOrDefault ("n" , Collections .emptyList ()))
111+ .stream ()
112+ .map (Long ::intValue )
113+ .map (ResponseNote ::maybeFromValue )
114+ .filter (Objects ::nonNull )
115+ .collect (Collectors .toList ()),
116+ Profile .fromList ((List <Object >) json .get ("p" )),
117+ Backtrace .fromList ((List <Object >) json .getOrDefault ("b" , null )),
118+ json .containsKey ("e" ) ? ErrorType .maybeFromValue (((Long ) json .get ("e" )).intValue ()) : null
119+ );
120+ };
118121 }
119122}
0 commit comments