rastest.java
/*
* Copyright (c) 2001-2005 Servertec. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* THIS NOTICE MUST NOT BE ALTERED NOR REMOVED.
*
* CopyrightVersion 1.0
*/
import stec.sfc.Win32.RandomAccessFile;
import stec.sfc.Win32.FileAccessMode;
import stec.sfc.Win32.FileAttribute;
import stec.sfc.Win32.CreateFileAttribute;
import stec.sfc.Win32.FileShareMode;
import stec.sfc.Win32.FileOperation;
import stec.sfc.Win32.FileSeekMethod;
import stec.sfc.Win32.FileSystem;
public class rastest
{
public static void main(String[] args) throws Exception
{
System.out.println("creating test.dat");
RandomAccessFile fh = new RandomAccessFile("test.dat", FileAccessMode.READ | FileAccessMode.WRITE, FileShareMode.READ | FileShareMode.WRITE, FileOperation.CREATE_ALWAYS, CreateFileAttribute.RANDOM_ACCESS);
try
{
System.out.println("writing 'A'");
fh.write('A');
System.out.println("flushing");
fh.flush();
System.out.println("seeking offset 0");
long l = fh.seek(0, FileSeekMethod.BEGIN);
if(l != 0)
{
System.out.println("seek failed: " + l);
return;
}
char chr = (char)fh.read();
if(chr != 'A')
{
System.out.println("read failed: " + chr);
return;
}
System.out.println("reading EOF");
int i = fh.read();
if(i == -1)
{
System.out.println("EOF found");
}
else
{
System.out.println("EOF not found: " + i);
return;
}
System.out.println("seeking offset 1000");
l = fh.seek(1000, FileSeekMethod.BEGIN);
if(l != 1000)
{
System.out.println("seek failed: " + l);
return;
}
System.out.println("locking");
if(fh.lock(0, 1))
{
System.out.println("lock passed");
}
else
{
System.out.println("lock failed");
return;
}
System.out.println("locking");
if(fh.lock(0, 1))
{
System.out.println("lock not found");
return;
}
else
{
System.out.println("lock found");
}
System.out.println("unlocking");
if(fh.unlock(0, 1))
{
System.out.println("unlock passed");
}
else
{
System.out.println("unlock failed");
return;
}
System.out.println("locking");
if(fh.lock(0, 1))
{
System.out.println("lock passed");
}
else
{
System.out.println("lock failed");
return;
}
System.out.println("unlocking");
if(fh.unlock(0, 1))
{
System.out.println("unlock passed");
}
else
{
System.out.println("unlock failed");
return;
}
System.out.println("seeking offset 0");
l = fh.seek(0, FileSeekMethod.BEGIN);
if(l != 0)
{
System.out.println("seek failed: " + l);
return;
}
byte[] bytes = new byte[50];
for(i = 0; i < bytes.length; i++)
{
bytes[i] = (byte)((byte)'0' + (byte)(i % 9));
}
System.out.println("writing 50 bytes");
i = fh.write(bytes);
if(i != 50)
{
System.out.println("write failed: " + i);
return;
}
System.out.println("seeking offset 0");
l = fh.seek(0, FileSeekMethod.BEGIN);
if(l != 0)
{
System.out.println("seek failed: " + l);
return;
}
System.out.println("reading 50 bytes");
i = fh.read(bytes);
if(i != 50)
{
System.out.println("read failed: " + i);
return;
}
System.out.println("comparing bytes read");
for(i = 0; i < bytes.length; i++)
{
chr = (char)((byte)'0' + (byte)(i % 9));
if(bytes[i] != chr)
{
System.out.println("[" + i + "] mismatch: " + (char)bytes[i] + " " + chr);
return;
}
}
System.out.println("bytes read compare");
System.out.println("seeking offset 1000");
l = fh.seek(1000, FileSeekMethod.BEGIN);
if(l != 1000)
{
System.out.println("seek failed: " + l);
return;
}
System.out.println("looking for EOF");
i = fh.read();
if(i == -1)
{
System.out.println("EOF found");
}
else
{
System.out.println("EOF not found: " + i);
return;
}
System.out.println("all tests passed");
}
finally
{
fh.close();
System.out.println("deleting test.dat");
FileSystem.delete("test.dat");
}
}
}