Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- static String filename = "myfile.txt";
- static int PAGE_SIZE = 512;
- static RandomAccessFile file;
- static byte ONE_BYTE = 1;
- public static void main(String[] args) throws IOException {
- file = new RandomAccessFile(new File(filename), "rw");
- // Table: isFull: 1, primarykey: studentid, tablename: student,
- // numberoffields: 3, fieldnames: studentid, name, surname, fieldsizes:
- // 10, 15, 15
- List<String> fields = new ArrayList<String>();
- fields.add("studentid");
- fields.add("name");
- fields.add("surname");
- List<Integer> fieldSizes = new ArrayList<Integer>();
- fieldSizes.add(10);
- fieldSizes.add(15);
- fieldSizes.add(15);
- Scanner scanner = new Scanner(System.in);
- String tablename = readAnything(scanner, 30, "Table name ");
- try {
- createTable(tablename, fields, fieldSizes, "studentid");
- } catch (MyDBException e) {
- System.err.println(e.getMessage());
- }
- // Get Table Data
- ByteBuffer readBuffer = readPage(0);
- byte first = readBuffer.get();
- if (first == ONE_BYTE) {
- String primaryKey = readString(readBuffer, 25);
- System.out.println(String.format("Primary key is %s", primaryKey));
- String tableName = readString(readBuffer, 30);
- System.out.println(String.format("Table name is %s", tableName));
- int numberOfFields = readBuffer.getInt();
- System.out.println(String.format("Number of fields: %d", numberOfFields));
- for (int i = 0; i < numberOfFields; i++) {
- String fieldName = readString(readBuffer, 25);
- System.out.println(String.format("%d. field name: %s", i, fieldName));
- }
- for (int i = 0; i < numberOfFields; i++) {
- int fieldSize = readBuffer.getInt();
- System.out.println(String.format("%d. field size: %d", i, fieldSize));
- }
- }
- file.close();
- }
- static ByteBuffer readPage(int pageId) throws IOException {
- file.seek(PAGE_SIZE * pageId);
- byte[] arr = new byte[PAGE_SIZE];
- file.read(arr);
- ByteBuffer buffer = ByteBuffer.wrap(arr);
- return buffer;
- }
- static byte[] stringToByteArray(int length, String s) {
- ByteBuffer buffer = ByteBuffer.allocate(length);
- buffer.put(s.getBytes());
- return buffer.array();
- }
- static String byteArrayToString(byte[] bytes) {
- int length = 0;
- for (int i = 0; i < bytes.length; i++) {
- if (bytes[i] == 0) {
- break;
- }
- length++;
- }
- return new String(bytes, 0, length);
- }
- static String readString(ByteBuffer buffer, int length) {
- byte[] bytes = new byte[length];
- buffer.get(bytes);
- return byteArrayToString(bytes);
- }
- static void writePage(int pageId, ByteBuffer buffer) throws IOException {
- file.seek(PAGE_SIZE * pageId);
- file.write(buffer.array());
- }
- static void createTable(String tablename, List<String> fields, List<Integer> fieldSizes, String primaryKey) throws IOException, MyDBException {
- if (tablename.getBytes().length > 30) {
- throw new MyDBException("Table name cannot be larger than 30 bytes!");
- }
- ByteBuffer buffer = ByteBuffer.allocate(PAGE_SIZE);
- buffer.put(ONE_BYTE);
- buffer.put(stringToByteArray(25, primaryKey));
- buffer.put(stringToByteArray(30, tablename));
- buffer.putInt(fields.size());
- for (String fieldName : fields) {
- buffer.put(stringToByteArray(25, fieldName));
- }
- for (Integer fieldSize : fieldSizes) {
- buffer.putInt(fieldSize);
- }
- writePage(0, buffer);
- }
- static String readAnything(Scanner scanner, int length, String type) {
- while (true) {
- String str = scanner.next();
- if (str.getBytes().length > length) {
- System.err.println(type + " cannot be longer than " + length + ", please enter again: ");
- } else {
- return str;
- }
- }
- }
- public static class MyDBException extends Exception {
- private static final long serialVersionUID = -5228951309945306582L;
- public MyDBException(String message) {
- super(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement