the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "File.h"
3#include "ConsoleSaveFileOutputStream.h"
4
5#include "ConsoleSaveFile.h"
6
7//Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is
8//created to represent this file connection.
9//First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument.
10//
11//If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened
12//for any other reason then a FileNotFoundException is thrown.
13//
14//Parameters:
15//file - the file to be opened for writing.
16ConsoleSaveFileOutputStream::ConsoleSaveFileOutputStream(ConsoleSaveFile *saveFile, const ConsoleSavePath &file)
17{
18 m_saveFile = saveFile;
19
20 m_file = m_saveFile->createFile(file);
21
22 m_saveFile->setFilePointer( m_file, 0, NULL, FILE_BEGIN );
23}
24
25ConsoleSaveFileOutputStream::ConsoleSaveFileOutputStream(ConsoleSaveFile *saveFile, FileEntry *file)
26{
27 m_saveFile = saveFile;
28
29 m_file = file;
30
31 m_saveFile->setFilePointer( m_file, 0, NULL, FILE_BEGIN );
32}
33
34//Writes the specified byte to this file output stream. Implements the write method of OutputStream.
35//Parameters:
36//b - the byte to be written.
37void ConsoleSaveFileOutputStream::write(unsigned int b)
38{
39 DWORD numberOfBytesWritten;
40
41 byte value = (byte) b;
42
43 BOOL result = m_saveFile->writeFile(
44 m_file,
45 &value, // data buffer
46 1, // number of bytes to write
47 &numberOfBytesWritten // number of bytes written
48 );
49
50 if( result == 0 )
51 {
52 // TODO 4J Stu - Some kind of error handling
53 }
54 else if( numberOfBytesWritten == 0 )
55 {
56 // File pointer is past the end of the file
57 }
58}
59
60//Writes b.length bytes from the specified byte array to this file output stream.
61//Parameters:
62//b - the data.
63void ConsoleSaveFileOutputStream::write(byteArray b)
64{
65 DWORD numberOfBytesWritten;
66
67 BOOL result = m_saveFile->writeFile(
68 m_file,
69 &b.data, // data buffer
70 b.length, // number of bytes to write
71 &numberOfBytesWritten // number of bytes written
72 );
73
74 if( result == 0 )
75 {
76 // TODO 4J Stu - Some kind of error handling
77 }
78 else if( numberOfBytesWritten == 0 || numberOfBytesWritten != b.length )
79 {
80 // File pointer is past the end of the file
81 }
82}
83
84//Writes len bytes from the specified byte array starting at offset off to this file output stream.
85//Parameters:
86//b - the data.
87//off - the start offset in the data.
88//len - the number of bytes to write.
89void ConsoleSaveFileOutputStream::write(byteArray b, unsigned int offset, unsigned int length)
90{
91 // 4J Stu - We don't want to write any more than the array buffer holds
92 assert( length <= ( b.length - offset ) );
93
94 DWORD numberOfBytesWritten;
95
96 BOOL result = m_saveFile->writeFile(
97 m_file,
98 &b[offset], // data buffer
99 length, // number of bytes to write
100 &numberOfBytesWritten // number of bytes written
101 );
102
103 if( result == 0 )
104 {
105 // TODO 4J Stu - Some kind of error handling
106 }
107 else if( numberOfBytesWritten == 0 || numberOfBytesWritten != length )
108 {
109 // File pointer is past the end of the file
110 }
111}
112//
113//Closes this file output stream and releases any system resources associated with this stream.
114//This file output stream may no longer be used for writing bytes.
115//If this stream has an associated channel then the channel is closed as well.
116void ConsoleSaveFileOutputStream::close()
117{
118 if( m_saveFile != NULL )
119 {
120 BOOL result = m_saveFile->closeHandle( m_file );
121
122 if( result == 0 )
123 {
124 // TODO 4J Stu - Some kind of error handling
125 }
126
127 // Stop the dtor from trying to close it again
128 m_saveFile = NULL;
129 }
130}