OSDN Git Service

98a332c134f10b2b43ef9ac3a8f3c269f91223e9
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / java / io / FileOutputStream.java
1 package java.io;\r
2 \r
3  \r
4 import lejos.nxt.Flash;\r
5 \r
6 \r
7 public class FileOutputStream extends OutputStream {\r
8 \r
9         /**\r
10          * Current page this stream is writing to\r
11          */\r
12         private int page_pointer;\r
13         \r
14         /**\r
15          * Current byte in *buffer* (buff below) it is writing to\r
16          */\r
17         private int data_pointer;\r
18         \r
19         /**\r
20          * A buffer of the same size as a page of flash memory.\r
21          */\r
22         private byte [] buff;\r
23         \r
24         private boolean append = false;\r
25         /**\r
26          * File attached to this stream\r
27          */\r
28         File file;\r
29         \r
30         /**\r
31          * create a new OutputStream to write to this file, starting  at the beginning of the file.\r
32          * @param f  the file this stream writes to\r
33          */             \r
34         public FileOutputStream(File f) \r
35         {\r
36                 this(f, false);\r
37         }\r
38 /**\r
39  * create a new OutputStream to write to this file\r
40  * @param f  the file this stream writes to\r
41  * @param append  if true this sream will start writing at the end of the file, otherwise at the beginning\r
42  */     \r
43         public FileOutputStream(File f, boolean append) {\r
44        this.append = append;\r
45         file = f;\r
46                 buff = new byte[File.BYTES_PER_PAGE];\r
47                 page_pointer = file.page_location;\r
48                 data_pointer = 0; // Start of first page\r
49 \r
50                 if(append)\r
51                 {\r
52                         page_pointer = file.page_location + file.file_length/File.BYTES_PER_PAGE ;\r
53                         data_pointer =  file.file_length%File.BYTES_PER_PAGE;\r
54                         Flash.readPage(buff, page_pointer);\r
55                 }\r
56                 else file.file_length = 0;// can this cause trouble?\r
57         }\r
58         \r
59 /**\r
60  * write 1 byte to the file; if necessary, file will be moved become the last file in memory\r
61  */     \r
62         public void write(int b) throws IOException {\r
63                 if(file.page_location < 0) throw new IOException(); // "File has not been created!"\r
64                 buff[data_pointer] = (byte)b;\r
65                 data_pointer++;\r
66                 file.file_length++; \r
67                 if(data_pointer >= File.BYTES_PER_PAGE) \r
68                 {\r
69                         if(file.getIndex()< ( File.totalFiles -1)) \r
70                                 {\r
71                                 file.moveToTop();\r
72                                 page_pointer = file.page_location + file.file_length/File.BYTES_PER_PAGE;                                       \r
73                                 }\r
74                         flush(); // Write to flash\r
75                         page_pointer++; // Move to next page\r
76                         data_pointer = 0;\r
77                 }\r
78         }\r
79         \r
80         public void flush() throws IOException {\r
81                 Flash.writePage(buff, page_pointer);\r
82     }\r
83 /**\r
84  * write the buffer to flash memory and update the file parameters in flash\r
85  * Resets pointers, so file can be writen again from beginning with the same output stream.\r
86  */     \r
87         public void close() throws IOException {\r
88                 // !! Alternate implementation: If this is a new file, perhaps only \r
89                 // write the file table information AFTER close() called so  \r
90                 // incomplete/partial files don't exist.\r
91                 flush();\r
92                 File.writeTable(File.listFiles()); // Updates file size for this file.\r
93         page_pointer = file.page_location;\r
94         data_pointer = 0; // Start of first page\r
95         }\r
96 }\r