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

org.evosuite.runtime.mock.java.io.EvoFileChannel Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
/**
 * Copyright (C) 2010-2017 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
package org.evosuite.runtime.mock.java.io;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.NonReadableChannelException;
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.atomic.AtomicInteger;

import org.evosuite.runtime.mock.java.lang.MockIllegalArgumentException;
import org.evosuite.runtime.vfs.VirtualFileSystem;


/**
 * This is not a mock of FileChannel, as FileChannel is an abstract class.
 * This class is never instantiated directly from the SUTs, but rather from mock classes (eg MockFileInputStream).
 * 
 * 
 * @author arcuri
 *
 */
public class EvoFileChannel extends FileChannel{  //FIXME mock FileChannel
	
	
	/**
	 * The read/write position in the channel
	 */
	private final AtomicInteger position;

	/**
	 * The absolute path of the file this channel is for
	 */
	private final String path;

	/**
	 * Can this channel be used for read operations?
	 */
	private final boolean isOpenForRead;

	/**
	 * Can this channel be used for write operations?
	 */
	private final boolean isOpenForWrite;

	/**
	 * Is this channel closed? Most functions throw an exception if the channel is closed.
	 * Once a channel is closed, it cannot be reopened
	 */
	private volatile boolean closed;


	private final Object readWriteMonitor = new Object();

	/**
	 * Main constructor
	 * 
	 * @param sharedPosition   the position in the channel, which should be shared with the stream this channel was generated from 
	 							(i.e., same instance reference) 
	 * @param path				full qualifying path the of the target file
	 * @param isOpenForRead
	 * @param isOpenForWrite
	 */
	protected EvoFileChannel(AtomicInteger sharedPosition, String path,
			boolean isOpenForRead, boolean isOpenForWrite) {
		super();
		this.position = sharedPosition;
		this.path = path;
		this.isOpenForRead = isOpenForRead;
		this.isOpenForWrite = isOpenForWrite;

		closed = false;
	}

	// -----  read --------

	@Override
	public int read(ByteBuffer dst) throws IOException {
		return read(new ByteBuffer[]{dst},0,1,position);
	}

	@Override
	public int read(ByteBuffer dst, long pos) throws IOException {

		if(pos < 0){
			throw new MockIllegalArgumentException("Negative position: "+pos);
		}

		AtomicInteger tmp = new AtomicInteger((int)pos);

		return read(new ByteBuffer[]{dst},0,1,tmp);
	}

	@Override
	public long read(ByteBuffer[] dsts, int offset, int length)
			throws IOException {
		return read(dsts,offset,length,position);
	}

	private int read(ByteBuffer[] dsts, int offset, int length, AtomicInteger posToUpdate)
			throws IOException {
		if(!isOpenForRead){
			throw new NonReadableChannelException();
		}

		throwExceptionIfClosed();

		int counter = 0;

		synchronized(readWriteMonitor){
			for(int j=offset; j srcs.length) ||  (length < 0) || (length > srcs.length-offset) ){
			throw new IndexOutOfBoundsException();
		}

		throwExceptionIfClosed();

		int counter = 0;

		byte[] buffer = new byte[1];

		synchronized(readWriteMonitor){
			for(int j=offset; j




© 2015 - 2024 Weber Informatics LLC | Privacy Policy