All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.boot.loader.data.RandomAccessDataFile Maven / Gradle / Ivy

/*
 * Copyright 2012-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.loader.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

/**
 * {@link RandomAccessData} implementation backed by a {@link RandomAccessFile}.
 *
 * @author Phillip Webb
 * @author Andy Wilkinson
 */
public class RandomAccessDataFile implements RandomAccessData {

	private final File file;

	private final RandomAccessFile randomAccessFile;

	private final long offset;

	private final long length;

	/**
	 * Create a new {@link RandomAccessDataFile} backed by the specified file.
	 * @param file the underlying file
	 * @throws IllegalArgumentException if the file is null or does not exist
	 */
	public RandomAccessDataFile(File file) {
		if (file == null) {
			throw new IllegalArgumentException("File must not be null");
		}
		try {
			this.randomAccessFile = new RandomAccessFile(file, "r");
		}
		catch (FileNotFoundException ex) {
			throw new IllegalArgumentException(
					String.format("File %s must exist", file.getAbsolutePath()));
		}
		this.file = file;
		this.offset = 0L;
		this.length = file.length();
	}

	/**
	 * Private constructor used to create a {@link #getSubsection(long, long) subsection}.
	 * @param file the underlying file
	 * @param randomAccessFile the random access file from which data is read
	 * @param offset the offset of the section
	 * @param length the length of the section
	 */
	private RandomAccessDataFile(File file, RandomAccessFile randomAccessFile,
			long offset, long length) {
		this.file = file;
		this.offset = offset;
		this.length = length;
		this.randomAccessFile = randomAccessFile;
	}

	/**
	 * Returns the underlying File.
	 * @return the underlying file
	 */
	public File getFile() {
		return this.file;
	}

	@Override
	public InputStream getInputStream() throws IOException {
		return new DataInputStream(this.randomAccessFile);
	}

	@Override
	public RandomAccessData getSubsection(long offset, long length) {
		if (offset < 0 || length < 0 || offset + length > this.length) {
			throw new IndexOutOfBoundsException();
		}
		return new RandomAccessDataFile(this.file, this.randomAccessFile,
				this.offset + offset, length);
	}

	@Override
	public byte[] read() throws IOException {
		return read(0, this.length);
	}

	@Override
	public byte[] read(long offset, long length) throws IOException {
		byte[] bytes = new byte[(int) length];
		synchronized (this.randomAccessFile) {
			this.randomAccessFile.seek(this.offset + offset);
			this.randomAccessFile.read(bytes, 0, (int) length);
		}
		return bytes;
	}

	@Override
	public long getSize() {
		return this.length;
	}

	public void close() throws IOException {

	}

	/**
	 * {@link RandomAccessDataInputStream} implementation for the
	 * {@link RandomAccessDataFile}.
	 */
	private class DataInputStream extends InputStream {

		private RandomAccessFile file;

		private int position;

		DataInputStream(RandomAccessFile file) throws IOException {
			this.file = file;
		}

		@Override
		public int read() throws IOException {
			return doRead(null, 0, 1);
		}

		@Override
		public int read(byte[] b) throws IOException {
			return read(b, 0, b == null ? 0 : b.length);
		}

		@Override
		public int read(byte[] b, int off, int len) throws IOException {
			if (b == null) {
				throw new NullPointerException("Bytes must not be null");
			}
			return doRead(b, off, len);
		}

		/**
		 * Perform the actual read.
		 * @param b the bytes to read or {@code null} when reading a single byte
		 * @param off the offset of the byte array
		 * @param len the length of data to read
		 * @return the number of bytes read into {@code b} or the actual read byte if
		 * {@code b} is {@code null}. Returns -1 when the end of the stream is reached
		 * @throws IOException in case of I/O errors
		 */
		public int doRead(byte[] b, int off, int len) throws IOException {
			if (len == 0) {
				return 0;
			}
			int cappedLen = cap(len);
			if (cappedLen <= 0) {
				return -1;
			}
			synchronized (this.file) {
				this.file.seek(RandomAccessDataFile.this.offset + this.position);
				if (b == null) {
					int rtn = this.file.read();
					moveOn(rtn == -1 ? 0 : 1);
					return rtn;
				}
				else {
					return (int) moveOn(this.file.read(b, off, cappedLen));
				}
			}
		}

		@Override
		public long skip(long n) throws IOException {
			return (n <= 0 ? 0 : moveOn(cap(n)));
		}

		/**
		 * Cap the specified value such that it cannot exceed the number of bytes
		 * remaining.
		 * @param n the value to cap
		 * @return the capped value
		 */
		private int cap(long n) {
			return (int) Math.min(RandomAccessDataFile.this.length - this.position, n);
		}

		/**
		 * Move the stream position forwards the specified amount.
		 * @param amount the amount to move
		 * @return the amount moved
		 */
		private long moveOn(int amount) {
			this.position += amount;
			return amount;
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy