KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > MemoryFile


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 public class MemoryFile {
8     private String JavaDoc name;
9     private int length;
10     private byte[] data;
11     private int pos;
12     private byte[] magic;
13     
14     MemoryFile(String JavaDoc name) {
15         this.name = name;
16         data = new byte[16];
17     }
18     
19     public String JavaDoc getName() {
20         return name;
21     }
22     
23     public void setName(String JavaDoc name) {
24         this.name = name;
25     }
26     
27     public long length() {
28         return length;
29     }
30     
31     public void setLength(long l) {
32         length = (int)l;
33     }
34     
35     public void seek(long pos) {
36         this.pos = (int)pos;
37     }
38     
39     public void write(byte[] b, int off, int len) {
40         if(pos+len > length) {
41             length = pos+len;
42         }
43         if(pos+len > data.length) {
44             byte[] n = new byte[length*2];
45             System.arraycopy(data, 0, n, 0, data.length);
46             data = n;
47         }
48         System.arraycopy(b, off, data, pos, len);
49         pos += len;
50     }
51
52     public void readFully(byte[] b, int off, int len) {
53         if(pos+len > length) {
54             length = pos+len;
55         }
56         if(pos+len > data.length) {
57             byte[] n = new byte[length*2];
58             System.arraycopy(data, 0, n, 0, data.length);
59             data = n;
60         }
61         System.arraycopy(data, pos, b, off, len);
62         pos += len;
63     }
64     
65     public long getFilePointer() {
66         return pos;
67     }
68
69     public void setMagic(byte[] magic) {
70         this.magic = magic;
71     }
72     
73     public byte[] getMagic() {
74         return magic;
75     }
76 }
77
Popular Tags