78
78
import com .oracle .graal .python .builtins .PythonBuiltins ;
79
79
import com .oracle .graal .python .builtins .modules .PosixModuleBuiltinsFactory .CastToPathNodeGen ;
80
80
import com .oracle .graal .python .builtins .modules .PosixModuleBuiltinsFactory .ConvertPathlikeObjectNodeGen ;
81
- import com .oracle .graal .python .builtins .modules .PosixModuleBuiltinsFactory .ReadFromChannelNodeGen ;
82
81
import com .oracle .graal .python .builtins .modules .PosixModuleBuiltinsFactory .StatNodeFactory ;
83
82
import com .oracle .graal .python .builtins .objects .PNone ;
84
83
import com .oracle .graal .python .builtins .objects .bytes .BytesNodes .ToBytesNode ;
110
109
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
111
110
import com .oracle .graal .python .nodes .util .CastToIndexNode ;
112
111
import com .oracle .graal .python .nodes .util .CastToIntegerFromIntNode ;
112
+ import com .oracle .graal .python .nodes .util .ChannelNodes .ReadFromChannelNode ;
113
113
import com .oracle .graal .python .runtime .PosixResources ;
114
114
import com .oracle .graal .python .runtime .PythonContext ;
115
115
import com .oracle .graal .python .runtime .PythonCore ;
@@ -1007,90 +1007,10 @@ public static WriteNode create() {
1007
1007
}
1008
1008
}
1009
1009
1010
- abstract static class ReadFromChannelNode extends PNodeWithContext {
1011
- private final BranchProfile gotException = BranchProfile .create ();
1012
-
1013
- abstract ByteSequenceStorage execute (Channel channel , int size );
1014
-
1015
- @ Specialization
1016
- ByteSequenceStorage readSeekable (SeekableByteChannel channel , int size ) {
1017
- long availableSize ;
1018
- try {
1019
- availableSize = availableSize (channel );
1020
- } catch (IOException e ) {
1021
- gotException .enter ();
1022
- throw raise (OSError , e );
1023
- }
1024
- if (availableSize > ReadNode .MAX_READ ) {
1025
- availableSize = ReadNode .MAX_READ ;
1026
- }
1027
- int sz = (int ) Math .min (availableSize , size );
1028
- return readReadable (channel , sz );
1029
- }
1030
-
1031
- @ TruffleBoundary (transferToInterpreterOnException = false )
1032
- private static long availableSize (SeekableByteChannel channel ) throws IOException {
1033
- return channel .size () - channel .position ();
1034
- }
1035
-
1036
- @ Specialization
1037
- ByteSequenceStorage readReadable (ReadableByteChannel channel , int size ) {
1038
- int sz = Math .min (size , ReadNode .MAX_READ );
1039
- ByteBuffer dst = allocateBuffer (sz );
1040
- int readSize = readIntoBuffer (channel , dst );
1041
- byte [] array ;
1042
- if (readSize <= 0 ) {
1043
- array = new byte [0 ];
1044
- readSize = 0 ;
1045
- } else {
1046
- array = getByteBufferArray (dst );
1047
- }
1048
- ByteSequenceStorage byteSequenceStorage = new ByteSequenceStorage (array );
1049
- byteSequenceStorage .setNewLength (readSize );
1050
- return byteSequenceStorage ;
1051
- }
1052
-
1053
- @ Specialization
1054
- ByteSequenceStorage readGeneric (Channel channel , int size ) {
1055
- if (channel instanceof SeekableByteChannel ) {
1056
- return readSeekable ((SeekableByteChannel ) channel , size );
1057
- } else if (channel instanceof ReadableByteChannel ) {
1058
- return readReadable ((ReadableByteChannel ) channel , size );
1059
- } else {
1060
- throw raise (OSError , "file not opened for reading" );
1061
- }
1062
- }
1063
-
1064
- @ TruffleBoundary (allowInlining = true )
1065
- private static byte [] getByteBufferArray (ByteBuffer dst ) {
1066
- return dst .array ();
1067
- }
1068
-
1069
- @ TruffleBoundary (allowInlining = true )
1070
- private int readIntoBuffer (ReadableByteChannel readableChannel , ByteBuffer dst ) {
1071
- try {
1072
- return readableChannel .read (dst );
1073
- } catch (IOException e ) {
1074
- gotException .enter ();
1075
- throw raise (OSError , e );
1076
- }
1077
- }
1078
-
1079
- @ TruffleBoundary (allowInlining = true )
1080
- private static ByteBuffer allocateBuffer (int sz ) {
1081
- return ByteBuffer .allocate (sz );
1082
- }
1083
-
1084
- public static ReadFromChannelNode create () {
1085
- return ReadFromChannelNodeGen .create ();
1086
- }
1087
- }
1088
-
1089
1010
@ Builtin (name = "read" , fixedNumOfPositionalArgs = 2 )
1090
1011
@ GenerateNodeFactory
1091
1012
@ TypeSystemReference (PythonArithmeticTypes .class )
1092
1013
public abstract static class ReadNode extends PythonFileNode {
1093
- private static final int MAX_READ = Integer .MAX_VALUE / 2 ;
1094
1014
1095
1015
@ CompilationFinal private BranchProfile tooLargeProfile = BranchProfile .create ();
1096
1016
@@ -1100,8 +1020,8 @@ Object readOpaque(@SuppressWarnings("unused") VirtualFrame frame, int fd, long r
1100
1020
@ Cached ("create()" ) ReadFromChannelNode readNode ) {
1101
1021
if (OpaqueBytes .isInOpaqueFilesystem (getResources ().getFilePath (fd ), getContext ())) {
1102
1022
Channel channel = getResources ().getFileChannel (fd , channelClassProfile );
1103
- ByteSequenceStorage bytes = readNode .execute (channel , MAX_READ );
1104
- return new OpaqueBytes (Arrays .copyOf (bytes .getInternalByteArray (), bytes .length ()));
1023
+ ByteSequenceStorage bytes = readNode .execute (channel , ReadFromChannelNode . MAX_READ );
1024
+ return new OpaqueBytes (Arrays .copyOf (bytes .getInternalByteArray (), bytes .length ()));
1105
1025
}
1106
1026
return read (frame , fd , requestedSize , channelClassProfile , readNode );
1107
1027
}
@@ -1115,7 +1035,7 @@ Object read(@SuppressWarnings("unused") VirtualFrame frame, int fd, long request
1115
1035
size = Math .toIntExact (requestedSize );
1116
1036
} catch (ArithmeticException e ) {
1117
1037
tooLargeProfile .enter ();
1118
- size = MAX_READ ;
1038
+ size = ReadFromChannelNode . MAX_READ ;
1119
1039
}
1120
1040
Channel channel = getResources ().getFileChannel (fd , channelClassProfile );
1121
1041
ByteSequenceStorage array = readNode .execute (channel , size );
0 commit comments