You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kato-commits@incubator.apache.org by sp...@apache.org on 2009/05/13 10:20:55 UTC
svn commit: r774279 [2/2] - in
/incubator/kato/trunk/org.apache.kato.tools.plugins.hexeditor: ./
.settings/ META-INF/ bin/ icons/ src/ src/org/ src/org/apache/
src/org/apache/kato/ src/org/apache/kato/tools/
src/org/apache/kato/tools/plugins/ src/org/a...
Added: incubator/kato/trunk/org.apache.kato.tools.plugins.hexeditor/src/org/apache/kato/tools/plugins/hexeditor/views/RawByteModel.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato.tools.plugins.hexeditor/src/org/apache/kato/tools/plugins/hexeditor/views/RawByteModel.java?rev=774279&view=auto
==============================================================================
--- incubator/kato/trunk/org.apache.kato.tools.plugins.hexeditor/src/org/apache/kato/tools/plugins/hexeditor/views/RawByteModel.java (added)
+++ incubator/kato/trunk/org.apache.kato.tools.plugins.hexeditor/src/org/apache/kato/tools/plugins/hexeditor/views/RawByteModel.java Wed May 13 10:20:54 2009
@@ -0,0 +1,136 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.kato.tools.plugins.hexeditor.views;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+import javax.imageio.stream.FileImageInputStream;
+
+import org.apache.kato.tools.plugins.hexeditor.eyecatchers.Eyecatcher;
+import org.apache.kato.tools.plugins.hexeditor.eyecatchers.EyecatcherScanner;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.Viewer;
+
+//this is the model for the view i.e. the raw dump file
+public class RawByteModel implements IStructuredContentProvider {
+ private FileImageInputStream file = null;
+ private long ptrFile = 0; //current seek pointer
+ private final int pageSize = 40 * 16; //number of items to display on a page
+ private byte[] buffer = new byte[pageSize]; //byte buffer
+ private int bufferSize = 0;
+ private EyecatcherScanner scanner = new EyecatcherScanner();
+
+ public RawByteModel(String path) {
+ try {
+ file = new FileImageInputStream(new RandomAccessFile(path, "r"));
+ readIntoBuffer(0);
+ } catch (FileNotFoundException e) {
+ throw new IllegalArgumentException("The file " + path + " does not exist or could not be found");
+ }
+ }
+
+ public long getOffset() {
+ return ptrFile; //the offset into the file
+ }
+
+ //accepts either an ASCII or hex identified string
+ public long findData(String searchFor) {
+ String hex = null;
+ if (searchFor.startsWith("0x")) {
+ hex = searchFor.substring(2); //strip the 0x prefix
+ } else {
+ StringBuffer buffer = new StringBuffer(searchFor.length() * 2); //correctly intialise the buffer size
+ for(int i = 0; i < searchFor.length(); i++) {
+ buffer.append(Integer.toHexString(0xFF & searchFor.charAt(i)));
+ }
+ hex = buffer.toString();
+ }
+ scanner.reset(); //reset the scanner
+ long mark = ptrFile; //note where we are in case we don't find the data we're looking for
+ readIntoBuffer(0); //start searching from the beginning of the data
+ int bufferDiscardCount = 0; //count how many times we discard the current buffer contents searching for a match
+ while(!isEOF()) {
+ if (scanner.scan(buffer, hex)) { //found the search terms
+ long index = (bufferDiscardCount * bufferSize); // + scanner.getEyecatcher().getLocation(); //position in file
+ readIntoBuffer(index); //ensure that the buffer contains a complete display page to avoid overwriting the eyecatcher
+ return index;
+ }
+ bufferDiscardCount++;
+ readIntoBuffer(bufferDiscardCount * bufferSize); //carry on reading from the current point
+ }
+ readIntoBuffer(mark); //restore where we were
+ return -1; //no match has been found
+ }
+
+ public byte getData(long index) {
+ if((index < ptrFile) || ((ptrFile + bufferSize) <= index)) { //currently have required index in buffer
+ readIntoBuffer(index);
+ scanner.reset(); //if we are reading a new buffer, then invalidate the scanner
+ }
+ return buffer[(int)(index - ptrFile)];
+ }
+
+ public boolean isInRange(long index) {
+ return (!isEOF()) && (index < file.length());
+ }
+
+ public boolean isEOF() {
+ return (bufferSize == -1);
+ }
+
+ private void readIntoBuffer(long index) {
+ if (isEOF() || (index >= file.length())) {
+ throw new IllegalStateException("Cannot read beyond the end of a file");
+ }
+ try {
+ ptrFile = index;
+ file.seek(index);
+ bufferSize = file.read(buffer);
+ } catch (IOException e) {
+ throw new RuntimeException("Could not read data", e);
+ }
+
+ }
+
+ public Object[] getElements(Object arg0) {
+ return null;
+ }
+
+ public void dispose() {
+ try {
+ file.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public boolean hasEyeCatcher(Eyecatcher searchfor) {
+ return false;
+ }
+
+ public boolean hasEyeCatcher() {
+ return scanner.hasEyecatcher();
+ }
+
+ public Eyecatcher getEyecatcher() {
+ return scanner.getEyecatcher();
+ }
+}
Propchange: incubator/kato/trunk/org.apache.kato.tools.plugins.hexeditor/src/org/apache/kato/tools/plugins/hexeditor/views/RawByteModel.java
------------------------------------------------------------------------------
svn:mime-type = text/plain