Advertisement
sedran

Example IO

Jul 15th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. import java.nio.ByteBuffer;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.     static String filename = "myfile.txt";
  11.     static int PAGE_SIZE = 512;
  12.     static RandomAccessFile file;
  13.     static byte ONE_BYTE = 1;
  14.  
  15.     public static void main(String[] args) throws IOException {
  16.         file = new RandomAccessFile(new File(filename), "rw");
  17.  
  18.         // Table: isFull: 1, primarykey: studentid, tablename: student,
  19.         // numberoffields: 3, fieldnames: studentid, name, surname, fieldsizes:
  20.         // 10, 15, 15
  21.         List<String> fields = new ArrayList<String>();
  22.         fields.add("studentid");
  23.         fields.add("name");
  24.         fields.add("surname");
  25.  
  26.         List<Integer> fieldSizes = new ArrayList<Integer>();
  27.         fieldSizes.add(10);
  28.         fieldSizes.add(15);
  29.         fieldSizes.add(15);
  30.  
  31.         Scanner scanner = new Scanner(System.in);
  32.         String tablename = readAnything(scanner, 30, "Table name ");
  33.  
  34.         try {
  35.             createTable(tablename, fields, fieldSizes, "studentid");
  36.         } catch (MyDBException e) {
  37.             System.err.println(e.getMessage());
  38.         }
  39.  
  40.         // Get Table Data
  41.         ByteBuffer readBuffer = readPage(0);
  42.         byte first = readBuffer.get();
  43.         if (first == ONE_BYTE) {
  44.             String primaryKey = readString(readBuffer, 25);
  45.             System.out.println(String.format("Primary key is %s", primaryKey));
  46.  
  47.             String tableName = readString(readBuffer, 30);
  48.             System.out.println(String.format("Table name is %s", tableName));
  49.  
  50.             int numberOfFields = readBuffer.getInt();
  51.             System.out.println(String.format("Number of fields: %d", numberOfFields));
  52.  
  53.             for (int i = 0; i < numberOfFields; i++) {
  54.                 String fieldName = readString(readBuffer, 25);
  55.                 System.out.println(String.format("%d. field name: %s", i, fieldName));
  56.             }
  57.  
  58.             for (int i = 0; i < numberOfFields; i++) {
  59.                 int fieldSize = readBuffer.getInt();
  60.                 System.out.println(String.format("%d. field size: %d", i, fieldSize));
  61.             }
  62.         }
  63.  
  64.         file.close();
  65.     }
  66.  
  67.     static ByteBuffer readPage(int pageId) throws IOException {
  68.         file.seek(PAGE_SIZE * pageId);
  69.  
  70.         byte[] arr = new byte[PAGE_SIZE];
  71.         file.read(arr);
  72.  
  73.         ByteBuffer buffer = ByteBuffer.wrap(arr);
  74.         return buffer;
  75.     }
  76.  
  77.     static byte[] stringToByteArray(int length, String s) {
  78.         ByteBuffer buffer = ByteBuffer.allocate(length);
  79.         buffer.put(s.getBytes());
  80.         return buffer.array();
  81.     }
  82.  
  83.     static String byteArrayToString(byte[] bytes) {
  84.         int length = 0;
  85.         for (int i = 0; i < bytes.length; i++) {
  86.             if (bytes[i] == 0) {
  87.                 break;
  88.             }
  89.             length++;
  90.         }
  91.         return new String(bytes, 0, length);
  92.     }
  93.  
  94.     static String readString(ByteBuffer buffer, int length) {
  95.         byte[] bytes = new byte[length];
  96.         buffer.get(bytes);
  97.         return byteArrayToString(bytes);
  98.     }
  99.  
  100.     static void writePage(int pageId, ByteBuffer buffer) throws IOException {
  101.         file.seek(PAGE_SIZE * pageId);
  102.         file.write(buffer.array());
  103.     }
  104.  
  105.     static void createTable(String tablename, List<String> fields, List<Integer> fieldSizes, String primaryKey) throws IOException, MyDBException {
  106.         if (tablename.getBytes().length > 30) {
  107.             throw new MyDBException("Table name cannot be larger than 30 bytes!");
  108.         }
  109.  
  110.         ByteBuffer buffer = ByteBuffer.allocate(PAGE_SIZE);
  111.         buffer.put(ONE_BYTE);
  112.         buffer.put(stringToByteArray(25, primaryKey));
  113.         buffer.put(stringToByteArray(30, tablename));
  114.         buffer.putInt(fields.size());
  115.         for (String fieldName : fields) {
  116.             buffer.put(stringToByteArray(25, fieldName));
  117.         }
  118.  
  119.         for (Integer fieldSize : fieldSizes) {
  120.             buffer.putInt(fieldSize);
  121.         }
  122.  
  123.         writePage(0, buffer);
  124.     }
  125.  
  126.     static String readAnything(Scanner scanner, int length, String type) {
  127.         while (true) {
  128.             String str = scanner.next();
  129.             if (str.getBytes().length > length) {
  130.                 System.err.println(type + " cannot be longer than " + length + ", please enter again: ");
  131.             } else {
  132.                 return str;
  133.             }
  134.         }
  135.     }
  136.    
  137.     public static class MyDBException extends Exception {
  138.         private static final long serialVersionUID = -5228951309945306582L;
  139.        
  140.         public MyDBException(String message) {
  141.             super(message);
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement