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