76
76
import io .minio .messages .VersioningConfiguration ;
77
77
import io .minio .org .apache .commons .validator .routines .InetAddressValidator ;
78
78
import java .io .BufferedInputStream ;
79
+ import java .io .ByteArrayOutputStream ;
79
80
import java .io .FileInputStream ;
80
81
import java .io .IOException ;
81
82
import java .io .InputStream ;
@@ -626,9 +627,8 @@ protected Response execute(
626
627
traceBuilder .append ("\n " );
627
628
628
629
OkHttpClient httpClient = this .httpClient ;
629
- if (method == Method .PUT || method == Method .POST ) {
630
- // Issue #924: disable connection retry for PUT and POST methods. Its safe to do
631
- // retry for other methods.
630
+ if (!(body instanceof byte []) && (method == Method .PUT || method == Method .POST )) {
631
+ // Issue #924: disable connection retry for PUT and POST methods for other than byte array.
632
632
httpClient = this .httpClient .newBuilder ().retryOnConnectionFailure (false ).build ();
633
633
}
634
634
@@ -2676,7 +2676,7 @@ public void removeBucket(RemoveBucketArgs args)
2676
2676
}
2677
2677
2678
2678
private ObjectWriteResponse putObject (
2679
- ObjectWriteArgs args ,
2679
+ PutObjectBaseArgs args ,
2680
2680
Object data ,
2681
2681
long objectSize ,
2682
2682
long partSize ,
@@ -2694,6 +2694,7 @@ private ObjectWriteResponse putObject(
2694
2694
String uploadId = null ;
2695
2695
long uploadedSize = 0L ;
2696
2696
Part [] parts = null ;
2697
+ ByteArrayOutputStream buffer = null ;
2697
2698
2698
2699
try {
2699
2700
for (int partNumber = 1 ; partNumber <= partCount || partCount < 0 ; partNumber ++) {
@@ -2714,12 +2715,19 @@ private ObjectWriteResponse putObject(
2714
2715
}
2715
2716
}
2716
2717
2718
+ Object body = data ;
2719
+ if (args .preloadData ()) {
2720
+ if (buffer == null ) buffer = new ByteArrayOutputStream ();
2721
+ fillData (buffer , data , availableSize );
2722
+ body = buffer .toByteArray ();
2723
+ }
2724
+
2717
2725
if (partCount == 1 ) {
2718
2726
return putObject (
2719
2727
args .bucket (),
2720
2728
args .region (),
2721
2729
args .object (),
2722
- data ,
2730
+ body ,
2723
2731
(int ) availableSize ,
2724
2732
headers ,
2725
2733
args .extraQueryParams ());
@@ -2744,7 +2752,7 @@ private ObjectWriteResponse putObject(
2744
2752
args .bucket (),
2745
2753
args .region (),
2746
2754
args .object (),
2747
- data ,
2755
+ body ,
2748
2756
(int ) availableSize ,
2749
2757
uploadId ,
2750
2758
partNumber ,
@@ -3807,6 +3815,25 @@ private long getAvailableSize(Object data, long expectedReadSize)
3807
3815
return totalBytesRead ;
3808
3816
}
3809
3817
3818
+ private void fillData (ByteArrayOutputStream buffer , Object data , long size ) throws IOException {
3819
+ buffer .reset ();
3820
+ byte [] buf = new byte [16384 ]; // 16KiB buffer for optimization
3821
+ long totalBytesRead = 0 ;
3822
+ while (totalBytesRead < size ) {
3823
+ long bytesToRead = size - totalBytesRead ;
3824
+ if (bytesToRead > buf .length ) bytesToRead = buf .length ;
3825
+ int bytesRead = -1 ;
3826
+ if (data instanceof RandomAccessFile ) {
3827
+ bytesRead = ((RandomAccessFile ) data ).read (buf , 0 , (int ) bytesToRead );
3828
+ } else if (data instanceof BufferedInputStream ) {
3829
+ bytesRead = ((BufferedInputStream ) data ).read (buf , 0 , (int ) bytesToRead );
3830
+ }
3831
+ if (bytesRead < 0 ) throw new IOException ("EOF on reading data" );
3832
+ buffer .write (buf , 0 , bytesRead );
3833
+ totalBytesRead += bytesRead ;
3834
+ }
3835
+ }
3836
+
3810
3837
/**
3811
3838
* Sets HTTP connect, write and read timeouts. A value of 0 means no timeout, otherwise values
3812
3839
* must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
0 commit comments