RandomAccessFile durch bytearrayinputstream ersetzen

skywalker77

Mitglied
Hallo !

Ich - als Java-Anfänger - habe eine klasse die eine Datei mittels RandomAccessFile lädt und schreibt.

In dieser Klasse sind seek() read() write() tell() usw vorhanden.
Die Aufgabe besteht nun darin, anstatt eine Datei zu nehmen diese Datei aus dem Speicher zu laden und dort dann auch wieder zurück zu legen.

Also bin ich auf ByteArrayInputStream gekommen.

Da hierbei manche Aufrufe fehlen, habe ich behelfsweise alles soweit hin gebogen - bis auf write.

Ich komme einfach nicht drauf wie ich da vorgehen soll.

Leider bin ich mir nicht mal sicher ob das etwas ist was von Java kommt oder ob das etwas ist was aus der Library verlangt wird die ich nutzen will.

Anbei eine original Datei mittels RandomAccessFile welche ich umbauen muss.
Freue mich über konstruktives zur Write-Methode.

Danke !


Java:
package com.radaee.util;

import android.util.Log;

import com.radaee.pdf.Document;
import com.radaee.pdf.Document.PDFStream;
import java.io.RandomAccessFile;

/**
 * File stream, an implement class for PDFStream, which used in Document.OpenStream
 * @author radaee
 */
public class PDFFileStream implements PDFStream
{
	private RandomAccessFile m_impl;
	public boolean open( String path )
	{
		try
		{
			m_impl = new RandomAccessFile( path, "rw" );
			return true;
		}
		catch( Exception e )
		{
			return false;
		}
	}
	public void close()
	{
		try
		{
			m_impl.close();
		}
		catch( Exception e )
		{
		}
		m_impl = null;
	}
	public boolean writeable()
	{
		return true;
	}
	public int get_size()
	{
		try
		{
			int len = (int)m_impl.length();
			if( len < 0 ) return 0;
			else return len;
		}
		catch( Exception e )
		{
			Log.d("get_size", e.getMessage());
			return 0;
		}
	}

	public int read(byte[] data)
	{
		try
		{
			int len = m_impl.read(data);
			if( len < 0 )
				return 0;
			else
				return len;
		}
		catch( Exception e )
		{
			Log.d("read", e.getMessage());
			return 0;
		}
	}

	public int write(byte[] data)
	{
		
		
		try
		{
			
			String str = null;
			str = new String(data, "UTF-8");
			Log.d("DEBUGGER", "Write Data to "+ tell() + "/data: ["+str+"]");
			
			m_impl.write(data);
			return data.length;
		}
		catch( Exception e )
		{
			Log.d("DEBUGLOG", "E write: "+e.getMessage());
			return 0;
		}
		
		
	}

	public void seek(int pos)
	{
		try
		{
			m_impl.seek(pos);
		}
		catch( Exception e )
		{
			Log.d("seek", e.getMessage());
		}
	}

	public int tell()
	{
		try
		{
			int pos = (int)m_impl.getFilePointer();
			if( pos < 0 ) return 0;
			else return pos;
		}
		catch( Exception e )
		{
			Log.d("tell", e.getMessage());
			return 0;
		}
	}
}
 
Hallo & vielen Dank schon mal !


Leider rauscht das ganze mit wehenden Fahnen ab.

Hier der angepasste Code erst mal:

Java:
public class cacheStream implements PDFStream
{
	private byte[] m_buffer;
	private int positionActive = 0;
	
	
	private ByteBuffer d_impl;
	
	
	private byte[] addData; //NN
	
	TimerTask mTimerTask; //NN
	Timer t = new Timer(); //NN
	
	private String filePath = null;
	
	
	public boolean open( String path )
	{
		Log.d("DEBUGLOG", "Open");
		try
		{			
			filePath = path;
			addData = null;
			
			 m_buffer = null;
		        try {
		        	File f1=new File(path);
		        	InputStream is = new FileInputStream(f1);
		            int size = is.available();
		            m_buffer = new byte[size];
		            is.read(m_buffer);
		            is.close();
		            d_impl = ByteBuffer.wrap(m_buffer);
	            
		        } catch (IOException e) {
		            e.printStackTrace();
		        }		
			
			return true;
		}
		catch( Exception e )
		{
			return false;
		}
	}
	
	public void close()
	{
		try
		{

		}
		catch( Exception e )
		{
			
		}

	}
	
	public boolean writeable()
	{
		return true;
	}
	
	
	public int get_size()
	{
		try
		{
			int len = d_impl.capacity();
			if( len < 0 ) return 0;
			else return len;
		}
		catch( Exception e )
		{
			Log.d("ADDget_size", e.getMessage());
			return 0;
		}
	}

	public int read(byte[] data)
	{
		
		try
		{
			d_impl.get(data);
			int len = data.length;
			if( len < 0 )
			{
				return 0;
			}
			else
			{
				return len;
			}

		}
		catch( Exception e )
		{
			return 0;
		}
		
	}

	public int write(byte[] data)
	{		
		d_impl.put(data);
		return data.length;
	}

	public void seek(int pos)
	{
		try
		{
			d_impl.position(pos);
		}
		catch( Exception e )
		{
			Log.d("ADDseek", e.getMessage());
		}
	}

	public int tell()
	{
		try
		{
			int pos = d_impl.position(); //positionActive;
			if( pos < 0 ) return 0;
			else return pos;
		}
		catch( Exception e )
		{
			Log.d("ADDtell", e.getMessage());
			return 0;
		}
	}
}

Vielleicht sieht jemand den Fehler. Ich sehe hier leider nicht all zu viel.

Die Fehlermeldung sagt leider nicht viel aus außer:
Java:
	09-29 03:33:42.977: I/dalvikvm-heap(12717): Grow heap (frag case) to 14.181MB for 3765097-byte allocation
	09-29 03:33:42.987: D/dalvikvm(12717): GC_FOR_ALLOC freed 1K, 19% free 13050K/15940K, paused 6ms, total 6ms
	09-29 03:33:43.047: A/libc(12717): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 12717 (droid.testapp)
 

Neue Themen


Zurück
Oben