3737@ Local (value = Processor .class )
3838public class QCOW2Processor extends AdapterBase implements Processor {
3939 private static final Logger s_logger = Logger .getLogger (QCOW2Processor .class );
40- StorageLayer _storage ;
40+ private static final int VIRTUALSIZE_HEADER_LOCATION = 24 ;
41+
42+ private StorageLayer _storage ;
4143
4244 @ Override
4345 public FormatInfo process (String templatePath , ImageFormat format , String templateName ) {
@@ -60,52 +62,30 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa
6062 File qcow2File = _storage .getFile (qcow2Path );
6163
6264 info .size = _storage .getSize (qcow2Path );
63- FileInputStream strm = null ;
64- byte [] b = new byte [8 ];
65+
6566 try {
66- strm = new FileInputStream (qcow2File );
67- strm .skip (24 );
68- strm .read (b );
69- } catch (Exception e ) {
70- s_logger .warn ("Unable to read qcow2 file " + qcow2Path , e );
67+ info .virtualSize = getVirtualSize (qcow2File );
68+ } catch (IOException e ) {
69+ s_logger .error ("Unable to get virtual size from " + qcow2File .getName ());
7170 return null ;
72- } finally {
73- if (strm != null ) {
74- try {
75- strm .close ();
76- } catch (IOException e ) {
77- }
78- }
7971 }
8072
81- long templateSize = NumbersUtil .bytesToLong (b );
82- info .virtualSize = templateSize ;
83-
8473 return info ;
8574 }
8675
8776 @ Override
88- public Long getVirtualSize (File file ) {
89- FileInputStream strm = null ;
77+ public long getVirtualSize (File file ) throws IOException {
9078 byte [] b = new byte [8 ];
91- try {
92- strm = new FileInputStream (file );
93- strm .skip (24 );
94- strm .read (b );
95- } catch (Exception e ) {
96- s_logger .warn ("Unable to read qcow2 file " + file , e );
97- return null ;
98- } finally {
99- if (strm != null ) {
100- try {
101- strm .close ();
102- } catch (IOException e ) {
103- }
79+ try (FileInputStream strm = new FileInputStream (file )) {
80+ if (strm .skip (VIRTUALSIZE_HEADER_LOCATION ) != VIRTUALSIZE_HEADER_LOCATION ) {
81+ throw new IOException ("Unable to skip to the virtual size header" );
82+ }
83+ if (strm .read (b ) != 8 ) {
84+ throw new IOException ("Unable to properly read the size" );
10485 }
10586 }
10687
107- long templateSize = NumbersUtil .bytesToLong (b );
108- return templateSize ;
88+ return NumbersUtil .bytesToLong (b );
10989 }
11090
11191 @ Override
0 commit comments