2525import java .net .MalformedURLException ;
2626import java .net .URL ;
2727import java .nio .ByteBuffer ;
28- import java .nio .charset .Charset ;
28+ import java .nio .charset .StandardCharsets ;
2929
3030import org .apache .commons .compress .compressors .CompressorException ;
31- import org .apache .commons .compress .compressors .CompressorInputStream ;
3231import org .apache .commons .compress .compressors .CompressorStreamFactory ;
3332import org .apache .log4j .Logger ;
3433
@@ -57,7 +56,10 @@ public static int getVirtualSizeHeaderLocation() {
5756 * @param inputStream The QCOW2 object in stream format.
5857 * @return The virtual size of the QCOW2 object.
5958 */
60- public static long getVirtualSize (InputStream inputStream ) throws IOException {
59+ public static long getVirtualSize (InputStream inputStream , boolean isCompressed ) throws IOException {
60+ if (isCompressed ) {
61+ return getVirtualSizeFromInputStream (inputStream );
62+ }
6163 byte [] bytes = new byte [VIRTUALSIZE_HEADER_LENGTH ];
6264
6365 if (inputStream .skip (VIRTUALSIZE_HEADER_LOCATION ) != VIRTUALSIZE_HEADER_LOCATION ) {
@@ -71,20 +73,13 @@ public static long getVirtualSize(InputStream inputStream) throws IOException {
7173 return NumbersUtil .bytesToLong (bytes );
7274 }
7375
74- public static long getVirtualSize (String urlStr ) {
75- InputStream inputStream = null ;
76-
76+ private static long getVirtualSizeFromInputStream (InputStream inputStream ) throws IOException {
7777 try {
78- URL url = new URL (urlStr );
79- BufferedInputStream bufferedInputStream = new BufferedInputStream (url .openStream ());
80- inputStream = bufferedInputStream ;
81-
8278 try {
83- CompressorInputStream compressorInputStream = new CompressorStreamFactory (). createCompressorInputStream ( bufferedInputStream );
84- inputStream = compressorInputStream ;
79+ BufferedInputStream bufferedInputStream = new BufferedInputStream ( inputStream );
80+ inputStream = new CompressorStreamFactory (). createCompressorInputStream ( bufferedInputStream ) ;
8581 } catch (CompressorException e ) {
8682 LOGGER .warn (e .getMessage ());
87- inputStream = bufferedInputStream ;
8883 }
8984
9085 byte [] inputBytes = inputStream .readNBytes (VIRTUALSIZE_HEADER_LOCATION + VIRTUALSIZE_HEADER_LENGTH );
@@ -93,7 +88,7 @@ public static long getVirtualSize(String urlStr) {
9388 inputMagicBytes .put (inputBytes , 0 , MAGIC_HEADER_LENGTH );
9489
9590 ByteBuffer qcow2MagicBytes = ByteBuffer .allocate (MAGIC_HEADER_LENGTH );
96- qcow2MagicBytes .put ("QFI" .getBytes (Charset . forName ( "UTF-8" ) ));
91+ qcow2MagicBytes .put ("QFI" .getBytes (StandardCharsets . UTF_8 ));
9792 qcow2MagicBytes .put ((byte )0xfb );
9893
9994 long virtualSize = 0L ;
@@ -105,12 +100,6 @@ public static long getVirtualSize(String urlStr) {
105100 }
106101
107102 return virtualSize ;
108- } catch (MalformedURLException e ) {
109- LOGGER .warn ("Failed to validate for qcow2, malformed URL: " + urlStr + ", error: " + e .getMessage ());
110- throw new IllegalArgumentException ("Invalid URL: " + urlStr );
111- } catch (IOException e ) {
112- LOGGER .warn ("Failed to validate for qcow2, error: " + e .getMessage ());
113- throw new IllegalArgumentException ("Failed to connect URL: " + urlStr );
114103 } finally {
115104 if (inputStream != null ) {
116105 try {
@@ -121,4 +110,17 @@ public static long getVirtualSize(String urlStr) {
121110 }
122111 }
123112 }
113+
114+ public static long getVirtualSize (String urlStr ) {
115+ try {
116+ URL url = new URL (urlStr );
117+ return getVirtualSizeFromInputStream (url .openStream ());
118+ } catch (MalformedURLException e ) {
119+ LOGGER .warn ("Failed to validate for qcow2, malformed URL: " + urlStr + ", error: " + e .getMessage ());
120+ throw new IllegalArgumentException ("Invalid URL: " + urlStr );
121+ } catch (IOException e ) {
122+ LOGGER .warn ("Failed to validate for qcow2, error: " + e .getMessage ());
123+ throw new IllegalArgumentException ("Failed to connect URL: " + urlStr );
124+ }
125+ }
124126}
0 commit comments