summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleFile.java
blob: 8564bcea5639ed526582a11ff255ecc8a59bf523 (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
package org.eclipse.swt.ole.win32;

/*
 * Licensed Materials - Property of IBM,
 * WebSphere Studio Workbench
 * (c) Copyright IBM Corp 2000
 */
import java.io.*;
import org.eclipse.swt.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.internal.ole.win32.*;

final class OleFile {
	IStorage rootStorage;
	File file;
	String streamName;

	static int READ = 0;
	static int WRITE = 1;
OleFile(File file, String streamName, int mode) {
	if (file == null || file.isDirectory())
		OLE.error(OLE.ERROR_INVALID_ARGUMENT);

	this.file = file;
	this.streamName = streamName;

	if (mode == READ)
		openForRead();
	if (mode == WRITE)
		openForWrite();
}
void dispose() {
	
	rootStorage.Release();
	rootStorage = null;
	file = null;
	streamName = null;
}
IStorage getRootStorage() {
	return rootStorage;
}
private void openForRead() {

	if (!file.exists())	return;

	char[] path = (file.getAbsolutePath()+"\0").toCharArray();
	if (COM.StgIsStorageFile(path) == COM.S_OK) {
		readStorageFile(path);
	} else {
		readTraditionalFile(path);
	}
	
}
private void openForWrite() {
	char[] filePath = (file.getAbsolutePath()+"\0").toCharArray();
	int[] address = new int[1];
	int mode = COM.STGM_TRANSACTED | COM.STGM_READWRITE | COM.STGM_SHARE_EXCLUSIVE | COM.STGM_CREATE;
	
	int result = COM.StgCreateDocfile(filePath, mode, 0, address);
	if (result != COM.S_OK)
		OLE.error(OLE.ERROR_CANNOT_CREATE_FILE, result);

	IStorage storage = new IStorage(address[0]);
		
	rootStorage = storage;
}
private void readStorageFile(char[] path) {
	
	int mode = COM.STGM_READ | COM.STGM_TRANSACTED | COM.STGM_SHARE_EXCLUSIVE;
	int[] address = new int[1];

	int result = COM.StgOpenStorage(path, 0, mode, 0, 0, address);
	if (result != COM.S_OK)
		OLE.error(OLE.ERROR_CANNOT_OPEN_FILE, result);
		
	rootStorage = new IStorage(address[0]);
	rootStorage.AddRef();
}
private void readTraditionalFile(char[] path) {

	if (streamName == null) OLE.error(OLE.ERROR_NULL_ARGUMENT);

	int mode = COM.STGM_DIRECT | COM.STGM_SHARE_EXCLUSIVE | COM.STGM_READWRITE | COM.STGM_CREATE;
	
	// Create a temporary storage object
	int[] address = new int[1];
	int result = COM.StgCreateDocfile(null, mode | COM.STGM_DELETEONRELEASE, 0, address);
	if (result != COM.S_OK)
		OLE.error(OLE.ERROR_CANNOT_OPEN_FILE, result);
	rootStorage = new IStorage(address[0]);
	rootStorage.AddRef();

	// Create a stream on the storage object with the name specified in streamName
	address = new int[1];
	result = rootStorage.CreateStream(streamName, mode, 0, 0, address);
	if (result != COM.S_OK)
		OLE.error(OLE.ERROR_CANNOT_OPEN_FILE, result);

	// Copy over data in file to named stream
	IStream stream = new IStream(address[0]);
	stream.AddRef();
	try {
	
		FileInputStream fileInput = new FileInputStream(file);
	
		int increment = 1024*4;
		byte[] buffer = new byte[increment];
		int count = 0;
		
		while((count = fileInput.read(buffer)) > 0){
			int pv = COM.CoTaskMemAlloc(count);
			OS.MoveMemory(pv, buffer, count);
			result = stream.Write(pv, count, null) ;
			COM.CoTaskMemFree(pv);
			if (result != COM.S_OK)
				OLE.error(OLE.ERROR_CANNOT_OPEN_FILE, result);
		}
		stream.Commit(COM.STGC_DEFAULT);

		fileInput.close();
	} catch (IOException err) {
		OLE.error(OLE.ERROR_CANNOT_OPEN_FILE);
	}
		
	stream.Release();

}
}