aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/ConsoleSaveFileOutputStream.cpp
blob: c4812a734718eab205e6e090b72d466b2b2422a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "stdafx.h"
#include "File.h"
#include "ConsoleSaveFileOutputStream.h"

#include "ConsoleSaveFile.h"

//Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is
//created to represent this file connection.
//First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument.
//
//If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened
//for any other reason then a FileNotFoundException is thrown.
//
//Parameters:
//file - the file to be opened for writing.
ConsoleSaveFileOutputStream::ConsoleSaveFileOutputStream(ConsoleSaveFile *saveFile, const ConsoleSavePath &file)
{
	m_saveFile = saveFile;

	m_file = m_saveFile->createFile(file);

	m_saveFile->setFilePointer( m_file, 0, nullptr, FILE_BEGIN );
}

ConsoleSaveFileOutputStream::ConsoleSaveFileOutputStream(ConsoleSaveFile *saveFile, FileEntry *file)
{
	m_saveFile = saveFile;

	m_file = file;

	m_saveFile->setFilePointer( m_file, 0, nullptr, FILE_BEGIN );
}

//Writes the specified byte to this file output stream. Implements the write method of OutputStream.
//Parameters:
//b - the byte to be written.
void ConsoleSaveFileOutputStream::write(unsigned int b)
{	
	DWORD numberOfBytesWritten;

	byte value = static_cast<byte>(b);

	BOOL result = m_saveFile->writeFile(
		m_file,
		&value, // data buffer
		1, // number of bytes to write
		&numberOfBytesWritten // number of bytes written
		);

	if( result == 0 )
	{
		// TODO 4J Stu - Some kind of error handling
	}
	else if( numberOfBytesWritten == 0 )
	{
		// File pointer is past the end of the file
	}
}

//Writes b.length bytes from the specified byte array to this file output stream.
//Parameters:
//b - the data.
void ConsoleSaveFileOutputStream::write(byteArray b)
{
	DWORD numberOfBytesWritten;

	BOOL result = m_saveFile->writeFile(
		m_file,
		&b.data, // data buffer
		b.length, // number of bytes to write
		&numberOfBytesWritten // number of bytes written
		);

	if( result == 0 )
	{
		// TODO 4J Stu - Some kind of error handling
	}
	else if( numberOfBytesWritten == 0 || numberOfBytesWritten != b.length  )
	{
		// File pointer is past the end of the file
	}
}

//Writes len bytes from the specified byte array starting at offset off to this file output stream.
//Parameters:
//b - the data.
//off - the start offset in the data.
//len - the number of bytes to write.
void ConsoleSaveFileOutputStream::write(byteArray b, unsigned int offset, unsigned int length)
{
	// 4J Stu - We don't want to write any more than the array buffer holds
	assert( length <= ( b.length - offset ) );

	DWORD numberOfBytesWritten;

	BOOL result = m_saveFile->writeFile(
		m_file,
		&b[offset], // data buffer
		length, // number of bytes to write
		&numberOfBytesWritten // number of bytes written
		);

	if( result == 0 )
	{
		// TODO 4J Stu - Some kind of error handling
	}
	else if( numberOfBytesWritten == 0 || numberOfBytesWritten != length  )
	{
		// File pointer is past the end of the file
	}
}
//
//Closes this file output stream and releases any system resources associated with this stream.
//This file output stream may no longer be used for writing bytes.
//If this stream has an associated channel then the channel is closed as well.
void ConsoleSaveFileOutputStream::close()
{
	if( m_saveFile != nullptr )
	{
		BOOL result = m_saveFile->closeHandle( m_file );

		if( result == 0 )
		{
			// TODO 4J Stu - Some kind of error handling
		}

		// Stop the dtor from trying to close it again
		m_saveFile = nullptr;
	}
}