diff options
Diffstat (limited to 'src/main/java/org/openslx/virtualization/disk')
5 files changed, 21 insertions, 13 deletions
diff --git a/src/main/java/org/openslx/virtualization/disk/DiskImage.java b/src/main/java/org/openslx/virtualization/disk/DiskImage.java index 5271e6e..dfb242a 100644 --- a/src/main/java/org/openslx/virtualization/disk/DiskImage.java +++ b/src/main/java/org/openslx/virtualization/disk/DiskImage.java @@ -136,7 +136,7 @@ public abstract class DiskImage implements Closeable throw e; } Util.safeClose( fileHandle ); - final String errorMsg = new String( "File '" + diskImagePath.getAbsolutePath() + "' is not a valid disk image!" ); + final String errorMsg = "File '" + diskImagePath.getAbsolutePath() + "' is not a valid disk image!"; throw new DiskImageException( errorMsg ); } diff --git a/src/main/java/org/openslx/virtualization/disk/DiskImageQcow2.java b/src/main/java/org/openslx/virtualization/disk/DiskImageQcow2.java index a007e1c..a6b3b73 100644 --- a/src/main/java/org/openslx/virtualization/disk/DiskImageQcow2.java +++ b/src/main/java/org/openslx/virtualization/disk/DiskImageQcow2.java @@ -210,7 +210,7 @@ public class DiskImageQcow2 extends DiskImage // check QCOW2 file format version if ( qcowVersion < 2 || qcowVersion > 3 ) { // QCOW2 disk image does not contain a valid QCOW2 version - final String errorMsg = new String( "Invalid QCOW2 version in header found!" ); + final String errorMsg = "Invalid QCOW2 version in header found!"; throw new DiskImageException( errorMsg ); } diff --git a/src/main/java/org/openslx/virtualization/disk/DiskImageUtils.java b/src/main/java/org/openslx/virtualization/disk/DiskImageUtils.java index dc67548..6bcef7f 100644 --- a/src/main/java/org/openslx/virtualization/disk/DiskImageUtils.java +++ b/src/main/java/org/openslx/virtualization/disk/DiskImageUtils.java @@ -119,11 +119,11 @@ public class DiskImageUtils * @param diskImage file to a disk storing the image content. * @param offset offset in bytes for reading <code>numBytes</code> bytes. * @param numBytes number of bytes to read at <code>offset</code>. - * @return read bytes from the disk image file as {@link String}. + * @return read bytes from the disk image file as {@link byte[]}. * - * @throws DiskImageException unable to read two bytes from the disk image file. + * @throws DiskImageException unable to read bytes from the disk image file. */ - public static String readBytesAsString( RandomAccessFile diskImage, long offset, int numBytes ) + public static byte[] readBytesAsArray( RandomAccessFile diskImage, long offset, int numBytes ) throws DiskImageException { final long imageSize = DiskImageUtils.getImageSize( diskImage ); @@ -139,6 +139,6 @@ public class DiskImageUtils } } - return new String( values ); + return values; } } diff --git a/src/main/java/org/openslx/virtualization/disk/DiskImageVdi.java b/src/main/java/org/openslx/virtualization/disk/DiskImageVdi.java index c564112..1e5b20b 100644 --- a/src/main/java/org/openslx/virtualization/disk/DiskImageVdi.java +++ b/src/main/java/org/openslx/virtualization/disk/DiskImageVdi.java @@ -1,6 +1,8 @@ package org.openslx.virtualization.disk; import java.io.RandomAccessFile; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; import org.openslx.virtualization.Version; @@ -16,6 +18,10 @@ public class DiskImageVdi extends DiskImage * Big endian representation of the little endian VDI magic bytes (signature). */ private static final int VDI_MAGIC = 0x7f10dabe; + /** + * Just 16 null-bytes + */ + private static final byte[] ZERO_UUID = new byte[16]; /** * Creates a new VDI disk image from an existing VDI image file. @@ -73,10 +79,9 @@ public class DiskImageVdi extends DiskImage final RandomAccessFile diskFile = this.getDiskImage(); // if parent UUID is set, the VDI file is a snapshot - final String parentUuid = DiskImageUtils.readBytesAsString( diskFile, 440, 16 ); - final String zeroUuid = new String( new byte[ 16 ] ); + byte[] parentUuid = DiskImageUtils.readBytesAsArray( diskFile, 440, 16 ); - return !zeroUuid.equals( parentUuid ); + return !Arrays.equals( ZERO_UUID, parentUuid ); } @Override @@ -94,7 +99,9 @@ public class DiskImageVdi extends DiskImage public String getDescription() throws DiskImageException { final RandomAccessFile diskFile = this.getDiskImage(); - return DiskImageUtils.readBytesAsString( diskFile, 84, 256 ); + byte[] data = DiskImageUtils.readBytesAsArray( diskFile, 84, 256 ); + // This will replace invalid chars. Maybe use CharsetDecoder and fall back to latin1 on error + return new String( data, StandardCharsets.UTF_8 ); } @Override diff --git a/src/main/java/org/openslx/virtualization/disk/DiskImageVmdk.java b/src/main/java/org/openslx/virtualization/disk/DiskImageVmdk.java index 720ec37..77986ef 100644 --- a/src/main/java/org/openslx/virtualization/disk/DiskImageVmdk.java +++ b/src/main/java/org/openslx/virtualization/disk/DiskImageVmdk.java @@ -1,6 +1,7 @@ package org.openslx.virtualization.disk; import java.io.RandomAccessFile; +import java.nio.charset.StandardCharsets; import org.openslx.util.Util; import org.openslx.virtualization.configuration.VirtualizationConfigurationVmwareFileFormat; @@ -117,15 +118,15 @@ public class DiskImageVmdk extends DiskImage // get content of descriptor file embedded into the VMDK disk image final long vmdkDescriptorOffset = vmdkDescriptorSectorOffset * DiskImageVmdk.VMDK_SECTOR_SIZE; final long vmdkDescriptorSizeMax = vmdkDescriptorSectorSize * DiskImageVmdk.VMDK_SECTOR_SIZE; - final String descriptorStr = DiskImageUtils.readBytesAsString( diskFile, vmdkDescriptorOffset, - Long.valueOf( vmdkDescriptorSizeMax ).intValue() ); + final String descriptorStr = new String ( DiskImageUtils.readBytesAsArray( diskFile, vmdkDescriptorOffset, + Long.valueOf( vmdkDescriptorSizeMax ).intValue() ), StandardCharsets.US_ASCII ); // get final length of the content within the sectors to be able to trim all 'zero' characters final int vmdkDescriptorSize = descriptorStr.indexOf( 0 ); // if final length of the content is invalid, throw an exception if ( vmdkDescriptorSize > vmdkDescriptorSizeMax || vmdkDescriptorSize < 0 ) { - final String errorMsg = new String( "Embedded descriptor size in VMDK disk image is invalid!" ); + final String errorMsg = "Embedded descriptor size in VMDK disk image is invalid!"; throw new DiskImageException( errorMsg ); } |