17
17
package org .springframework .boot .loader .jar ;
18
18
19
19
import java .io .IOException ;
20
- import java .util .Optional ;
21
20
22
21
import org .springframework .boot .loader .data .RandomAccessData ;
23
22
@@ -45,7 +44,7 @@ class CentralDirectoryEndRecord {
45
44
46
45
private static final int READ_BLOCK_SIZE = 256 ;
47
46
48
- private final Optional < Zip64End > zip64End ;
47
+ private final Zip64End zip64End ;
49
48
50
49
private byte [] block ;
51
50
@@ -76,8 +75,7 @@ class CentralDirectoryEndRecord {
76
75
this .offset = this .block .length - this .size ;
77
76
}
78
77
int startOfCentralDirectoryEndRecord = (int ) (data .getSize () - this .size );
79
- this .zip64End = Optional .ofNullable (
80
- isZip64 () ? new Zip64End (data , startOfCentralDirectoryEndRecord ) : null );
78
+ this .zip64End = isZip64 () ? new Zip64End (data , startOfCentralDirectoryEndRecord ) : null ;
81
79
}
82
80
83
81
private byte [] createBlockFromEndOfData (RandomAccessData data , int size ) throws IOException {
@@ -94,6 +92,10 @@ private boolean isValid() {
94
92
return this .size == MINIMUM_SIZE + commentLength ;
95
93
}
96
94
95
+ private boolean isZip64 () {
96
+ return (int ) Bytes .littleEndianValue (this .block , this .offset + 10 , 2 ) == ZIP64_MAGICCOUNT ;
97
+ }
98
+
97
99
/**
98
100
* Returns the location in the data that the archive actually starts. For most files
99
101
* the archive data will start at 0, however, it is possible to have prefixed bytes
@@ -104,10 +106,9 @@ private boolean isValid() {
104
106
long getStartOfArchive (RandomAccessData data ) {
105
107
long length = Bytes .littleEndianValue (this .block , this .offset + 12 , 4 );
106
108
long specifiedOffset = Bytes .littleEndianValue (this .block , this .offset + 16 , 4 );
107
- long zip64EndSize = this .zip64End .map ((x ) -> x .getSize ()).orElse (0L );
108
- int zip64LocSize = this .zip64End .map ((x ) -> Zip64Locator .ZIP64_LOCSIZE ).orElse (0 );
109
- long actualOffset = data .getSize () - this .size - length - zip64EndSize
110
- - zip64LocSize ;
109
+ long zip64EndSize = (this .zip64End != null ) ? this .zip64End .getSize () : 0L ;
110
+ int zip64LocSize = (this .zip64End != null ) ? Zip64Locator .ZIP64_LOCSIZE : 0 ;
111
+ long actualOffset = data .getSize () - this .size - length - zip64EndSize - zip64LocSize ;
111
112
return actualOffset - specifiedOffset ;
112
113
}
113
114
@@ -118,34 +119,30 @@ long getStartOfArchive(RandomAccessData data) {
118
119
* @return the central directory data
119
120
*/
120
121
RandomAccessData getCentralDirectory (RandomAccessData data ) {
121
- if (isZip64 ()) {
122
- return this .zip64End .get ().getCentratDirectory (data );
123
- }
124
- else {
125
- long offset = Bytes .littleEndianValue (this .block , this .offset + 16 , 4 );
126
- long length = Bytes .littleEndianValue (this .block , this .offset + 12 , 4 );
127
- return data .getSubsection (offset , length );
122
+ if (this .zip64End != null ) {
123
+ return this .zip64End .getCentralDirectory (data );
128
124
}
125
+ long offset = Bytes .littleEndianValue (this .block , this .offset + 16 , 4 );
126
+ long length = Bytes .littleEndianValue (this .block , this .offset + 12 , 4 );
127
+ return data .getSubsection (offset , length );
129
128
}
130
129
131
130
/**
132
131
* Return the number of ZIP entries in the file.
133
132
* @return the number of records in the zip
134
133
*/
135
134
int getNumberOfRecords () {
136
- if (isZip64 ()) {
137
- return this .zip64End .get ().getNumberOfRecords ();
138
- }
139
- else {
140
- long numberOfRecords = Bytes .littleEndianValue (this .block , this .offset + 10 ,
141
- 2 );
142
- return (int ) numberOfRecords ;
135
+ if (this .zip64End != null ) {
136
+ return this .zip64End .getNumberOfRecords ();
143
137
}
138
+ long numberOfRecords = Bytes .littleEndianValue (this .block , this .offset + 10 , 2 );
139
+ return (int ) numberOfRecords ;
144
140
}
145
141
146
- boolean isZip64 () {
147
- return (int ) Bytes .littleEndianValue (this .block , this .offset + 10 ,
148
- 2 ) == ZIP64_MAGICCOUNT ;
142
+ String getComment () {
143
+ int commentLength = (int ) Bytes .littleEndianValue (this .block , this .offset + COMMENT_LENGTH_OFFSET , 2 );
144
+ AsciiBytes comment = new AsciiBytes (this .block , this .offset + COMMENT_LENGTH_OFFSET + 2 , commentLength );
145
+ return comment .toString ();
149
146
}
150
147
151
148
/**
@@ -154,11 +151,13 @@ boolean isZip64() {
154
151
* @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
155
152
* 4.3.14 of Zip64 specification</a>
156
153
*/
157
- private static class Zip64End {
154
+ private static final class Zip64End {
158
155
159
- static final int ZIP64_ENDTOT = 32 ; // total number of entries
160
- static final int ZIP64_ENDSIZ = 40 ; // central directory size in bytes
161
- static final int ZIP64_ENDOFF = 48 ; // offset of first CEN header
156
+ private static final int ZIP64_ENDTOT = 32 ; // total number of entries
157
+
158
+ private static final int ZIP64_ENDSIZ = 40 ; // central directory size in bytes
159
+
160
+ private static final int ZIP64_ENDOFF = 48 ; // offset of first CEN header
162
161
163
162
private final Zip64Locator locator ;
164
163
@@ -168,12 +167,11 @@ private static class Zip64End {
168
167
169
168
private int numberOfRecords ;
170
169
171
- Zip64End (RandomAccessData data , int centratDirectoryEndOffset )
172
- throws IOException {
170
+ private Zip64End (RandomAccessData data , int centratDirectoryEndOffset ) throws IOException {
173
171
this (data , new Zip64Locator (data , centratDirectoryEndOffset ));
174
172
}
175
173
176
- Zip64End (RandomAccessData data , Zip64Locator locator ) throws IOException {
174
+ private Zip64End (RandomAccessData data , Zip64Locator locator ) throws IOException {
177
175
this .locator = locator ;
178
176
byte [] block = data .read (locator .getZip64EndOffset (), 56 );
179
177
this .centralDirectoryOffset = Bytes .littleEndianValue (block , ZIP64_ENDOFF , 8 );
@@ -185,7 +183,7 @@ private static class Zip64End {
185
183
* Return the size of this zip 64 end of central directory record.
186
184
* @return size of this zip 64 end of central directory record
187
185
*/
188
- public long getSize () {
186
+ private long getSize () {
189
187
return this .locator .getZip64EndSize ();
190
188
}
191
189
@@ -195,16 +193,15 @@ public long getSize() {
195
193
* @param data the source data
196
194
* @return the central directory data
197
195
*/
198
- public RandomAccessData getCentratDirectory (RandomAccessData data ) {
199
- return data .getSubsection (this .centralDirectoryOffset ,
200
- this .centralDirectoryLength );
196
+ private RandomAccessData getCentralDirectory (RandomAccessData data ) {
197
+ return data .getSubsection (this .centralDirectoryOffset , this .centralDirectoryLength );
201
198
}
202
199
203
200
/**
204
201
* Return the number of entries in the zip64 archive.
205
202
* @return the number of records in the zip
206
203
*/
207
- public int getNumberOfRecords () {
204
+ private int getNumberOfRecords () {
208
205
return this .numberOfRecords ;
209
206
}
210
207
@@ -216,7 +213,7 @@ public int getNumberOfRecords() {
216
213
* @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
217
214
* 4.3.15 of Zip64 specification</a>
218
215
*/
219
- private static class Zip64Locator {
216
+ private static final class Zip64Locator {
220
217
221
218
static final int ZIP64_LOCSIZE = 20 ; // locator size
222
219
static final int ZIP64_LOCOFF = 8 ; // offset of zip64 end
@@ -225,8 +222,7 @@ private static class Zip64Locator {
225
222
226
223
private final int offset ;
227
224
228
- Zip64Locator (RandomAccessData data , int centralDirectoryEndOffset )
229
- throws IOException {
225
+ private Zip64Locator (RandomAccessData data , int centralDirectoryEndOffset ) throws IOException {
230
226
this .offset = centralDirectoryEndOffset - ZIP64_LOCSIZE ;
231
227
byte [] block = data .read (this .offset , ZIP64_LOCSIZE );
232
228
this .zip64EndOffset = Bytes .littleEndianValue (block , ZIP64_LOCOFF , 8 );
@@ -236,24 +232,18 @@ private static class Zip64Locator {
236
232
* Return the size of the zip 64 end record located by this zip64 end locator.
237
233
* @return size of the zip 64 end record located by this zip64 end locator
238
234
*/
239
- public long getZip64EndSize () {
235
+ private long getZip64EndSize () {
240
236
return this .offset - this .zip64EndOffset ;
241
237
}
242
238
243
239
/**
244
240
* Return the offset to locate {@link Zip64End}.
245
241
* @return offset of the Zip64 end of central directory record
246
242
*/
247
- public long getZip64EndOffset () {
243
+ private long getZip64EndOffset () {
248
244
return this .zip64EndOffset ;
249
245
}
250
246
251
247
}
252
248
253
- String getComment () {
254
- int commentLength = (int ) Bytes .littleEndianValue (this .block , this .offset + COMMENT_LENGTH_OFFSET , 2 );
255
- AsciiBytes comment = new AsciiBytes (this .block , this .offset + COMMENT_LENGTH_OFFSET + 2 , commentLength );
256
- return comment .toString ();
257
- }
258
-
259
249
}
0 commit comments