gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / hexeditor / ByteBuffer.java @ 2937
History | View | Annotate | Download (4.6 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2008 Robert Futrell
|
3 |
* All rights reserved.
|
4 |
*
|
5 |
* Redistribution and use in source and binary forms, with or without
|
6 |
* modification, are permitted provided that the following conditions are met:
|
7 |
* * Redistributions of source code must retain the above copyright
|
8 |
* notice, this list of conditions and the following disclaimer.
|
9 |
* * Redistributions in binary form must reproduce the above copyright
|
10 |
* notice, this list of conditions and the following disclaimer in the
|
11 |
* documentation and/or other materials provided with the distribution.
|
12 |
* * Neither the name "HexEditor" nor the names of its contributors may
|
13 |
* be used to endorse or promote products derived from this software
|
14 |
* without specific prior written permission.
|
15 |
*
|
16 |
* THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
17 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
18 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
19 |
* EVENT SHALL THE CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT,
|
20 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
21 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
22 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
23 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
25 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26 |
*/
|
27 |
package org.gvsig.tools.swing.impl.hexeditor; |
28 |
|
29 |
import java.io.BufferedInputStream; |
30 |
import java.io.ByteArrayOutputStream; |
31 |
import java.io.File; |
32 |
import java.io.FileInputStream; |
33 |
import java.io.IOException; |
34 |
import java.io.InputStream; |
35 |
|
36 |
public class ByteBuffer { |
37 |
|
38 |
/**
|
39 |
* The byte buffer that contains the document content.
|
40 |
*/
|
41 |
private byte[] buffer; |
42 |
|
43 |
|
44 |
public ByteBuffer(int size) { |
45 |
buffer = new byte[size]; |
46 |
} |
47 |
|
48 |
|
49 |
public ByteBuffer(String file) throws IOException { |
50 |
this(new File(file)); |
51 |
} |
52 |
|
53 |
|
54 |
public ByteBuffer(File file) throws IOException { |
55 |
|
56 |
int size = (int)file.length(); |
57 |
if (size<0) { // Probably never happens. |
58 |
throw new IOException("Negative file length: " + size); |
59 |
} |
60 |
buffer = new byte[size]; |
61 |
|
62 |
if (size>0) { |
63 |
BufferedInputStream in = new BufferedInputStream( |
64 |
new FileInputStream(file)); |
65 |
int pos = 0; |
66 |
int count = 0; |
67 |
try {
|
68 |
while (pos<buffer.length &&
|
69 |
(count=in.read(buffer, pos, buffer.length-pos))>-1) {
|
70 |
pos += count; |
71 |
} |
72 |
} finally {
|
73 |
in.close(); |
74 |
} |
75 |
} |
76 |
|
77 |
} |
78 |
|
79 |
|
80 |
/**
|
81 |
* Creates a buffer representing the contents read from an input stream.
|
82 |
*
|
83 |
* @param in The input stream to read from.
|
84 |
* @throws IOException If an IO error occurs.
|
85 |
*/
|
86 |
public ByteBuffer(InputStream in) throws IOException { |
87 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
88 |
buffer = new byte[4096]; // Use as a temporary buffer. |
89 |
int count = 0; |
90 |
while ((count=in.read(buffer, 0,buffer.length))>-1) { |
91 |
baos.write(buffer, 0,count);
|
92 |
} |
93 |
buffer = baos.toByteArray(); |
94 |
} |
95 |
|
96 |
|
97 |
public byte getByte(int offset) { |
98 |
return buffer[offset];
|
99 |
} |
100 |
|
101 |
public byte[] getBytes() { |
102 |
return this.buffer; |
103 |
} |
104 |
|
105 |
public int getSize() { |
106 |
return buffer.length;
|
107 |
} |
108 |
|
109 |
|
110 |
public void insertByte(int offset, byte b) { |
111 |
byte[] buf2 = new byte[buffer.length+1]; |
112 |
System.arraycopy(buffer,0, buf2,0, offset); |
113 |
buf2[offset] = b; |
114 |
System.arraycopy(buffer,offset, buf2,offset+1, buffer.length-offset); |
115 |
buffer = buf2; |
116 |
} |
117 |
|
118 |
|
119 |
public void insertBytes(int offs, byte[] b) { |
120 |
|
121 |
if (b==null || b.length==0) { |
122 |
return;
|
123 |
} |
124 |
|
125 |
byte[] buf2 = new byte[buffer.length+b.length]; |
126 |
System.arraycopy(buffer,0, buf2,0, offs); |
127 |
System.arraycopy(b,0, buf2,offs, b.length); |
128 |
System.arraycopy(buffer,offs, buf2,offs+b.length, buffer.length-offs);
|
129 |
buffer = buf2; |
130 |
|
131 |
} |
132 |
|
133 |
|
134 |
public int read(int offset, byte[] buf) { |
135 |
if (buf==null) { |
136 |
return -1; |
137 |
} |
138 |
int count = Math.min(buf.length, getSize()-offset); |
139 |
System.arraycopy(buffer,offset, buf,0, count); |
140 |
return count;
|
141 |
} |
142 |
|
143 |
|
144 |
public void remove(int offset, int len) { |
145 |
remove(offset, len, null);
|
146 |
} |
147 |
|
148 |
|
149 |
public void remove(int offset, int len, byte[] removed) { |
150 |
if (removed!=null) { |
151 |
System.arraycopy(buffer,offset, removed,0, len); |
152 |
} |
153 |
byte[] buf = new byte[buffer.length-len]; |
154 |
System.arraycopy(buffer,0, buf,0, offset); |
155 |
System.arraycopy(buffer,offset+len, buf,offset, buf.length-offset);
|
156 |
buffer = buf; |
157 |
} |
158 |
|
159 |
|
160 |
public void setByte(int offset, byte b) { |
161 |
buffer[offset] = b; |
162 |
} |
163 |
|
164 |
|
165 |
} |