summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/virtualization/disk/DiskImageUtils.java
blob: 6bcef7fd1d58c189e30fa4fa7274db0923faecf0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.openslx.virtualization.disk;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Utilities to parse disk image format elements and control versions of disk images.
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class DiskImageUtils
{
	/**
	 * Returns the size of a specified disk image file.
	 * 
	 * @param diskImage file to a disk storing the image content.
	 * @return size of the disk image file in bytes.
	 * 
	 * @throws DiskImageException unable to obtain the size of the disk image file.
	 */
	public static long getImageSize( RandomAccessFile diskImage ) throws DiskImageException
	{
		final long imageSize;

		try {
			imageSize = diskImage.length();
		} catch ( IOException e ) {
			throw new DiskImageException( e.getLocalizedMessage() );
		}

		return imageSize;
	}

	/**
	 * Reads two bytes ({@link Short}) at a given <code>offset</code> from the specified disk image
	 * file.
	 * 
	 * @param diskImage file to a disk storing the image content.
	 * @param offset offset in bytes for reading the two bytes.
	 * @return value of the two bytes from the disk image file as {@link Short}.
	 * 
	 * @throws DiskImageException unable to read two bytes from the disk image file.
	 */
	public static short readShort( RandomAccessFile diskImage, long offset ) throws DiskImageException
	{
		final long imageSize = DiskImageUtils.getImageSize( diskImage );
		short value = 0;

		if ( imageSize >= ( offset + Short.BYTES ) ) {
			try {
				diskImage.seek( offset );
				value = diskImage.readShort();
			} catch ( IOException e ) {
				throw new DiskImageException( e.getLocalizedMessage() );
			}
		}

		return value;
	}

	/**
	 * Reads four bytes ({@link Integer}) at a given <code>offset</code> from the specified disk
	 * image file.
	 * 
	 * @param diskImage file to a disk storing the image content.
	 * @param offset offset in bytes for reading the four bytes.
	 * @return value of the four bytes from the disk image file as {@link Integer}.
	 * 
	 * @throws DiskImageException unable to read four bytes from the disk image file.
	 */
	public static int readInt( RandomAccessFile diskImage, long offset ) throws DiskImageException
	{
		final long imageSize = DiskImageUtils.getImageSize( diskImage );
		int value = 0;

		if ( imageSize >= ( offset + Integer.BYTES ) ) {
			try {
				diskImage.seek( offset );
				value = diskImage.readInt();
			} catch ( IOException e ) {
				throw new DiskImageException( e.getLocalizedMessage() );
			}
		}

		return value;
	}

	/**
	 * Reads eight bytes ({@link Long}) at a given <code>offset</code> from the specified disk image
	 * file.
	 * 
	 * @param diskImage file to a disk storing the image content.
	 * @param offset offset in bytes for reading the eight bytes.
	 * @return value of the eight bytes from the disk image file as {@link Long}.
	 * 
	 * @throws DiskImageException unable to read eight bytes from the disk image file.
	 */
	public static long readLong( RandomAccessFile diskImage, long offset ) throws DiskImageException
	{
		final long imageSize = DiskImageUtils.getImageSize( diskImage );
		long value = 0;

		if ( imageSize >= ( offset + Long.BYTES ) ) {
			try {
				diskImage.seek( offset );
				value = diskImage.readLong();
			} catch ( IOException e ) {
				throw new DiskImageException( e.getLocalizedMessage() );
			}
		}

		return value;
	}

	/**
	 * Reads a variable number of bytes (<code>numBytes</code>) at a given <code>offset</code> from the specified disk image file.
	 * 
	 * @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 byte[]}.
	 * 
	 * @throws DiskImageException unable to read bytes from the disk image file.
	 */
	public static byte[] readBytesAsArray( RandomAccessFile diskImage, long offset, int numBytes )
			throws DiskImageException
	{
		final long imageSize = DiskImageUtils.getImageSize( diskImage );
		byte values[] = {};

		if ( imageSize >= ( offset + numBytes ) ) {
			try {
				diskImage.seek( offset );
				values = new byte[ numBytes ];
				diskImage.readFully( values );
			} catch ( IOException e ) {
				throw new DiskImageException( e.getLocalizedMessage() );
			}
		}

		return values;
	}
}