@@ -907,6 +907,60 @@ static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
907
907
}
908
908
909
909
910
+ // Wrapper for writev(2).
911
+ //
912
+ // bytesWritten = writev(fd, chunks, position, callback)
913
+ // 0 fd integer. file descriptor
914
+ // 1 chunks array of buffers to write
915
+ // 2 position if integer, position to write at in the file.
916
+ // if null, write from the current position
917
+ static void WriteBuffers (const FunctionCallbackInfo<Value>& args) {
918
+ Environment* env = Environment::GetCurrent (args);
919
+
920
+ CHECK (args[0 ]->IsInt32 ());
921
+ CHECK (args[1 ]->IsArray ());
922
+
923
+ int fd = args[0 ]->Int32Value ();
924
+ Local<Array> chunks = args[1 ].As <Array>();
925
+ int64_t pos = GET_OFFSET (args[2 ]);
926
+ Local<Value> req = args[3 ];
927
+
928
+ uint32_t chunkCount = chunks->Length ();
929
+
930
+ uv_buf_t s_iovs[1024 ]; // use stack allocation when possible
931
+ uv_buf_t * iovs;
932
+
933
+ if (chunkCount > ARRAY_SIZE (s_iovs))
934
+ iovs = new uv_buf_t [chunkCount];
935
+ else
936
+ iovs = s_iovs;
937
+
938
+ for (uint32_t i = 0 ; i < chunkCount; i++) {
939
+ Local<Value> chunk = chunks->Get (i);
940
+
941
+ if (!Buffer::HasInstance (chunk)) {
942
+ if (iovs != s_iovs)
943
+ delete[] iovs;
944
+ return env->ThrowTypeError (" Array elements all need to be buffers" );
945
+ }
946
+
947
+ iovs[i] = uv_buf_init (Buffer::Data (chunk), Buffer::Length (chunk));
948
+ }
949
+
950
+ if (req->IsObject ()) {
951
+ ASYNC_CALL (write , req, fd, iovs, chunkCount, pos)
952
+ if (iovs != s_iovs)
953
+ delete[] iovs;
954
+ return ;
955
+ }
956
+
957
+ SYNC_CALL (write , nullptr , fd, iovs, chunkCount, pos)
958
+ if (iovs != s_iovs)
959
+ delete[] iovs;
960
+ args.GetReturnValue ().Set (SYNC_RESULT);
961
+ }
962
+
963
+
910
964
// Wrapper for write(2).
911
965
//
912
966
// bytesWritten = write(fd, string, position, enc, callback)
@@ -1248,6 +1302,7 @@ void InitFs(Local<Object> target,
1248
1302
env->SetMethod (target, " readlink" , ReadLink);
1249
1303
env->SetMethod (target, " unlink" , Unlink);
1250
1304
env->SetMethod (target, " writeBuffer" , WriteBuffer);
1305
+ env->SetMethod (target, " writeBuffers" , WriteBuffers);
1251
1306
env->SetMethod (target, " writeString" , WriteString);
1252
1307
1253
1308
env->SetMethod (target, " chmod" , Chmod);
0 commit comments