3535class BlobWriterChannelImpl implements BlobWriteChannel {
3636
3737 private static final long serialVersionUID = 8675286882724938737L ;
38- private static final int CHUNK_SIZE = 256 * 1024 ;
39- private static final int MIN_BUFFER_SIZE = 8 * CHUNK_SIZE ;
38+ private static final int MIN_CHUNK_SIZE = 256 * 1024 ;
39+ private static final int DEFAULT_CHUNK_SIZE = 8 * MIN_CHUNK_SIZE ;
4040
4141 private final StorageServiceOptions options ;
4242 private final Blob blob ;
@@ -45,6 +45,7 @@ class BlobWriterChannelImpl implements BlobWriteChannel {
4545 private byte [] buffer = new byte [0 ];
4646 private int limit ;
4747 private boolean isOpen = true ;
48+ private int chunkSize = DEFAULT_CHUNK_SIZE ;
4849
4950 private transient StorageRpc storageRpc ;
5051 private transient StorageObject storageObject ;
@@ -65,8 +66,8 @@ private void writeObject(ObjectOutputStream out) throws IOException {
6566 }
6667
6768 private void flush (boolean compact ) {
68- if (limit >= MIN_BUFFER_SIZE || compact && limit >= CHUNK_SIZE ) {
69- final int length = limit - limit % CHUNK_SIZE ;
69+ if (limit >= chunkSize || compact && limit >= MIN_CHUNK_SIZE ) {
70+ final int length = limit - limit % MIN_CHUNK_SIZE ;
7071 runWithRetries (callable (new Runnable () {
7172 @ Override
7273 public void run () {
@@ -75,7 +76,7 @@ public void run() {
7576 }), options .retryParams (), StorageServiceImpl .EXCEPTION_HANDLER );
7677 position += length ;
7778 limit -= length ;
78- byte [] temp = new byte [compact ? limit : MIN_BUFFER_SIZE ];
79+ byte [] temp = new byte [compact ? limit : chunkSize ];
7980 System .arraycopy (buffer , length , temp , 0 , limit );
8081 buffer = temp ;
8182 }
@@ -107,8 +108,7 @@ public int write(ByteBuffer byteBuffer) throws IOException {
107108 if (spaceInBuffer >= toWrite ) {
108109 byteBuffer .get (buffer , limit , toWrite );
109110 } else {
110- buffer = Arrays .copyOf (buffer ,
111- Math .max (MIN_BUFFER_SIZE , buffer .length + toWrite - spaceInBuffer ));
111+ buffer = Arrays .copyOf (buffer , Math .max (chunkSize , buffer .length + toWrite - spaceInBuffer ));
112112 byteBuffer .get (buffer , limit , toWrite );
113113 }
114114 limit += toWrite ;
@@ -135,4 +135,10 @@ public void run() {
135135 buffer = null ;
136136 }
137137 }
138+
139+ @ Override
140+ public void chunkSize (int chunkSize ) {
141+ chunkSize = (chunkSize / MIN_CHUNK_SIZE ) * MIN_CHUNK_SIZE ;
142+ this .chunkSize = Math .max (MIN_CHUNK_SIZE , chunkSize );
143+ }
138144}
0 commit comments