Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c73bf743a33f180a779836fdf8c871e4e06f29d9 (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
/*******************************************************************************
 * Copyright (c) 2000, 2009 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.utils;

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;

/**
 * @noextend This class is not intended to be subclassed by clients.
 */
public class ERandomAccessFile extends RandomAccessFile {
	private boolean isle;
	private long ptr_offset;
	int val[] = new int[4];
	private final String path;

	public ERandomAccessFile(String file, String mode) throws IOException {
		super(file, mode);
		path = file;
	}

	public ERandomAccessFile(File file, String mode) throws IOException {
		this(file.getPath(), mode);
	}

	public void setEndian(boolean le) {
		isle = le;
	}

	public final short readShortE() throws IOException {
		val[0] = read();
		val[1] = read();
		if ((val[0] | val[1]) < 0)
			throw new EOFException();
		if (isle) {
			return (short) ((val[1] << 8) + val[0]);
		}
		return (short) ((val[0] << 8) + val[1]);
	}

	public final long readIntE() throws IOException {
		val[0] = read();
		val[1] = read();
		val[2] = read();
		val[3] = read();
		if ((val[0] | val[1] | val[2] | val[3]) < 0)
			throw new EOFException();
		long value;
		if (isle) {
			value = ((val[3] << 24) + (val[2] << 16) + (val[1] << 8) + val[0]);
		} else {
			value = ((val[0] << 24) + (val[1] << 16) + (val[2] << 8) + val[3]);
		}
		return value & 0x00000000ffffffffL;

	}

	public final long readLongE() throws IOException {
		byte[] bytes = new byte[8];
		long result = 0;
		super.readFully(bytes);
		int shift = 0;
		if (isle)
			for (int i = 7; i >= 0; i--) {
				shift = i * 8;
				result += (((long) bytes[i]) << shift) & (0xffL << shift);
			}
		else
			for (int i = 0; i <= 7; i++) {
				shift = (7 - i) * 8;
				result += (((long) bytes[i]) << shift) & (0xffL << shift);
			}
		return result;
	}

	public final void readFullyE(byte[] bytes) throws IOException {
		super.readFully(bytes);
		byte tmp = 0;
		if (isle)
			for (int i = 0; i < (bytes.length / 2); i++) {
				tmp = bytes[i];
				bytes[i] = bytes[bytes.length - i - 1];
				bytes[bytes.length - i - 1] = tmp;
			}
	}

	public void setFileOffset(long offset) throws IOException {
		ptr_offset = offset;
		super.seek(offset);
	}

	/**
	 * Get the path of the file reader
	 * @since 6.12
	 */
	public String getPath() {
		return path;
	}

	@Override
	public long getFilePointer() throws IOException {
		long ptr = super.getFilePointer();
		ptr = ptr - ptr_offset;
		return ptr;
	}

	@Override
	public void seek(long pos) throws IOException {
		long real_pos = pos + ptr_offset;
		super.seek(real_pos);
	}

	/**
	 * Get the byte order of the file
	 * @return {@link ByteOrder#LITTLE_ENDIAN} or {@link ByteOrder#BIG_ENDIAN}
	 * @since 6.12
	 */
	public ByteOrder order() {
		return isle ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
	}
}

Back to the top