@@ -82,70 +82,98 @@ public static void assertCounter(
82
82
* Any attempt to read a different blob will throw a {@link FileNotFoundException}
83
83
*/
84
84
public static BlobContainer singleBlobContainer (final String blobName , final byte [] blobContent ) {
85
- return new BlobContainer () {
86
-
85
+ return new MostlyUnimplementedFakeBlobContainer () {
87
86
@ Override
88
87
public InputStream readBlob (String name , long position , long length ) throws IOException {
89
88
if (blobName .equals (name ) == false ) {
90
89
throw new FileNotFoundException ("Blob not found: " + name );
91
90
}
92
- return Streams .limitStream (new ByteArrayInputStream (blobContent , Math .toIntExact (position ), blobContent .length ), length );
91
+ return Streams .limitStream (new ByteArrayInputStream (blobContent , Math .toIntExact (position ),
92
+ blobContent .length - Math .toIntExact (position )), length );
93
93
}
94
+ };
95
+ }
94
96
95
- @ Override
96
- public long readBlobPreferredLength () {
97
- return Long .MAX_VALUE ;
98
- }
97
+ static BlobContainer singleSplitBlobContainer (final String blobName , final byte [] blobContent , final int partSize ) {
98
+ if (partSize >= blobContent .length ) {
99
+ return singleBlobContainer (blobName , blobContent );
100
+ } else {
101
+ final String prefix = blobName + ".part" ;
102
+ return new MostlyUnimplementedFakeBlobContainer () {
103
+ @ Override
104
+ public InputStream readBlob (String name , long position , long length ) throws IOException {
105
+ if (name .startsWith (prefix ) == false ) {
106
+ throw new FileNotFoundException ("Blob not found: " + name );
107
+ }
108
+ assert position + length <= partSize
109
+ : "cannot read [" + position + "-" + (position + length ) + "] from array part of length [" + partSize + "]" ;
110
+ final int partNumber = Integer .parseInt (name .substring (prefix .length ()));
111
+ final int positionInBlob = Math .toIntExact (position ) + partSize * partNumber ;
112
+ assert positionInBlob + length <= blobContent .length
113
+ : "cannot read [" + positionInBlob + "-" + (positionInBlob + length ) + "] from array of length ["
114
+ + blobContent .length + "]" ;
115
+ return Streams .limitStream (new ByteArrayInputStream (blobContent ,
116
+ positionInBlob , blobContent .length - positionInBlob ), length );
117
+ }
118
+ };
119
+ }
120
+ }
99
121
100
- @ Override
101
- public Map <String , BlobMetadata > listBlobs () {
102
- throw unsupportedException ();
103
- }
122
+ private static class MostlyUnimplementedFakeBlobContainer implements BlobContainer {
104
123
105
- @ Override
106
- public BlobPath path () {
107
- throw unsupportedException () ;
108
- }
124
+ @ Override
125
+ public long readBlobPreferredLength () {
126
+ return Long . MAX_VALUE ;
127
+ }
109
128
110
- @ Override
111
- public InputStream readBlob ( String blobName ) {
112
- throw unsupportedException ();
113
- }
129
+ @ Override
130
+ public Map < String , BlobMetadata > listBlobs ( ) {
131
+ throw unsupportedException ();
132
+ }
114
133
115
- @ Override
116
- public void writeBlob ( String blobName , InputStream inputStream , long blobSize , boolean failIfAlreadyExists ) {
117
- throw unsupportedException ();
118
- }
134
+ @ Override
135
+ public BlobPath path ( ) {
136
+ throw unsupportedException ();
137
+ }
119
138
120
- @ Override
121
- public void writeBlobAtomic (String blobName , InputStream inputStream , long blobSize , boolean failIfAlreadyExists ) {
122
- throw unsupportedException ();
123
- }
139
+ @ Override
140
+ public InputStream readBlob (String blobName ) {
141
+ throw unsupportedException ();
142
+ }
124
143
125
- @ Override
126
- public DeleteResult delete ( ) {
127
- throw unsupportedException ();
128
- }
144
+ @ Override
145
+ public void writeBlob ( String blobName , InputStream inputStream , long blobSize , boolean failIfAlreadyExists ) {
146
+ throw unsupportedException ();
147
+ }
129
148
130
- @ Override
131
- public void deleteBlobsIgnoringIfNotExists ( List < String > blobNames ) {
132
- throw unsupportedException ();
133
- }
149
+ @ Override
150
+ public void writeBlobAtomic ( String blobName , InputStream inputStream , long blobSize , boolean failIfAlreadyExists ) {
151
+ throw unsupportedException ();
152
+ }
134
153
135
- @ Override
136
- public Map < String , BlobContainer > children () {
137
- throw unsupportedException ();
138
- }
154
+ @ Override
155
+ public DeleteResult delete () {
156
+ throw unsupportedException ();
157
+ }
139
158
140
- @ Override
141
- public Map <String , BlobMetadata > listBlobsByPrefix ( String blobNamePrefix ) {
142
- throw unsupportedException ();
143
- }
159
+ @ Override
160
+ public void deleteBlobsIgnoringIfNotExists ( List <String > blobNames ) {
161
+ throw unsupportedException ();
162
+ }
144
163
145
- private UnsupportedOperationException unsupportedException () {
146
- assert false : "this operation is not supported and should have not be called" ;
147
- return new UnsupportedOperationException ("This operation is not supported" );
148
- }
149
- };
164
+ @ Override
165
+ public Map <String , BlobContainer > children () {
166
+ throw unsupportedException ();
167
+ }
168
+
169
+ @ Override
170
+ public Map <String , BlobMetadata > listBlobsByPrefix (String blobNamePrefix ) {
171
+ throw unsupportedException ();
172
+ }
173
+
174
+ private UnsupportedOperationException unsupportedException () {
175
+ assert false : "this operation is not supported and should have not be called" ;
176
+ return new UnsupportedOperationException ("This operation is not supported" );
177
+ }
150
178
}
151
179
}
0 commit comments