-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSharedMemory.java
executable file
·243 lines (207 loc) · 5.79 KB
/
SharedMemory.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*******************************************************************************
* Copyright 2009, Clark N. Hobbie
*
* This file is part of the CLIPC library.
*
* The CLIPC library is free software; you can redistribute it and/or modify it
* under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* The CLIPC library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with the CLIP library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*******************************************************************************/
package com.lts.ipc.sharedmemory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.FileChannel.MapMode;
import com.lts.ipc.IPCException;
import com.lts.ipc.Messages;
import com.lts.ipc.Utils;
/**
* <P>
* A shared memory segment.
* </P>
* <H2>Description</H2>
* <P>
* This class wraps the {@link java.nio.MappedByteBuffer} class to make it a little easier
* to use.
* </P>
* <P>
* {@link #quickstart}
* </P>
* <P>
* Instances of this class must correspond to a file on the host system that is read/write
* to the running process. <A name="quickstart">
* <H2>Quickstart</H2> </A>
* <UL>
* <LI>Create or connect to an existing segment via {@link #SharedMemory(String, int)}.</LI>
* </LI>
* <LI>Read from the segment via {@link #read(byte[], int, int, int)}.</LI>
* <LI>Write to the segment via {@link #write(byte[], int, int, int)}.</LI>
* <LI>You do not need to explicitly close your connection.</LI>
* </UL>
*
* @see java.nio.MappedByteBuffer
* @author cnh
*/
public class SharedMemory
{
protected File segmentFile;
protected MappedByteBuffer byteBuffer;
protected int size;
protected boolean connected;
protected FileChannel channel;
private FileLock myLock;
public MappedByteBuffer getByteBuffer()
{
return byteBuffer;
}
public void setByteBuffer(MappedByteBuffer byteBuffer)
{
this.byteBuffer = byteBuffer;
}
public boolean isConnected()
{
return connected;
}
public File getSegmentFile()
{
return segmentFile;
}
public int getSize()
{
return size;
}
public SharedMemory()
{
}
public SharedMemory(File file, int size) throws IPCException
{
connect(file, size);
}
public SharedMemory(String fileName, int size) throws IPCException
{
connect(fileName, size);
}
/**
* Connect this segment to the underlying shared memory segment.
*
* @param name
* The name of the file for the segment.
* @param size
* The size of the segment.
*/
public void connect(File file, int size) throws IPCException
{
if (null != this.byteBuffer)
throw new IPCException(Messages.ERROR_ALREADY_CONNECTED);
Utils.checkFile(file);
RandomAccessFile raf = null;
try
{
raf = new RandomAccessFile(file, "rw");
}
catch (FileNotFoundException e)
{
String msg = Messages.ERROR_OPEN_FAILED;
throw new IPCException(msg, e);
}
channel = raf.getChannel();
try
{
this.byteBuffer = channel.map(MapMode.READ_WRITE, 0, size);
}
catch (IOException e)
{
String msg = Messages.ERROR_MAP_EXCEPTION;
throw new IPCException(msg, e);
}
this.segmentFile = file;
this.size = size;
}
public void connect(String fileName, int size) throws IPCException
{
File file = new File(fileName);
connect(file, size);
}
public void write(byte[] buf, int buffOffset, int bufLength, int segmentOffset)
throws IPCException
{
try
{
if (null == this.byteBuffer)
throw new IPCException(Messages.ERROR_NOT_CONNECTED);
this.byteBuffer.position(segmentOffset);
this.byteBuffer.put(buf, buffOffset, bufLength);
}
catch (java.nio.BufferOverflowException e)
{
System.out.println("segment size = " + this.size + ", segmentOffset = "
+ segmentOffset + ", length = " + bufLength);
throw e;
}
}
public void write(byte value, int offset)
{
this.byteBuffer.position(offset);
this.byteBuffer.put(value);
}
public void write(byte[] buf) throws IPCException
{
write(buf, 0, buf.length, 0);
}
public void write(int value, int offset)
{
byte b = (byte) value;
write(b, offset);
}
public void write(byte[] buf, int segmentOffset) throws IPCException
{
write(buf, 0, buf.length, segmentOffset);
}
public void read(byte[] buf, int boffset, int length, int soffset)
throws IPCException
{
if (null == this.byteBuffer)
throw new IPCException(Messages.ERROR_NOT_CONNECTED);
this.byteBuffer.position(soffset);
this.byteBuffer.get(buf, boffset, length);
}
public void read(byte[] buf, int offset) throws IPCException
{
read(buf, 0, buf.length, offset);
}
public void read(byte[] buf) throws IPCException
{
read(buf, 0, buf.length, 0);
}
public void read(int offset, byte[] buf) throws IPCException
{
read(buf, 0, buf.length, offset);
}
public byte readByte(int offset) throws IPCException
{
return this.byteBuffer.get(offset);
}
public void lock() throws IOException
{
myLock = channel.lock();
}
public void unlock() throws IOException
{
myLock.release();
}
}