Db4oIsolatedStorageFile

This topic applies to .NET version only 

Db4oIsolatedStorageFile.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02using System; 03using System.IO; 04using System.IO.IsolatedStorage; 05using Db4objects.Db4o; 06using Db4objects.Db4o.Ext; 07 08namespace Db4objects.Db4odoc.IsolatedStorage 09{ 10 11 public class Db4oIsolatedStorageFile 12 { 13 private IsolatedStorageFileStream _stream; 14 15 [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] 16 static extern int FlushFileBuffers(IntPtr fileHandle); 17 18 public Db4oIsolatedStorageFile(String file, String fileMode) 19 { 20 try 21 { 22 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly(); 23 _stream = new IsolatedStorageFileStream(file, FileMode.OpenOrCreate, 24 fileMode.Equals("rw") ? FileAccess.ReadWrite : FileAccess.Read, isf); 25 } 26 catch (IOException x) 27 { 28 throw new DatabaseFileLockedException(file, x); 29 } 30 } 31 // end Db4oIsolatedStorageFile 32 33 public FileStream Stream 34 { 35 get { return _stream; } 36 } 37 // end Stream 38 39 public void Close() 40 { 41 _stream.Close(); 42 } 43 // end Close 44 45 public long Length() 46 { 47 return _stream.Length; 48 } 49 // end Length 50 51 public int Read(byte[] bytes, int offset, int length) 52 { 53 return _stream.Read(bytes, offset, length); 54 } 55 // end Read 56 57 public void Read(byte[] bytes) 58 { 59 _stream.Read(bytes, 0, bytes.Length); 60 } 61 // end Read 62 63 public void Seek(long pos) 64 { 65 _stream.Seek(pos, SeekOrigin.Begin); 66 } 67 // end Seek 68 69 public void Sync() 70 { 71 _stream.Flush(); 72 73 FlushFileBuffers(_stream.SafeFileHandle.DangerousGetHandle()); 74 75 } 76 // end Sync 77 78 public Db4oIsolatedStorageFile GetFD() 79 { 80 return this; 81 } 82 // end GetFD 83 84 public void Write(byte[] bytes) 85 { 86 this.Write(bytes, 0, bytes.Length); 87 } 88 // end Write 89 90 public void Write(byte[] bytes, int offset, int length) 91 { 92 _stream.Write(bytes, offset, length); 93 } 94 // end Write 95 } 96}
Db4oIsolatedStorageFile.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02 03Imports System 04Imports System.IO 05Imports System.IO.IsolatedStorage 06Imports Db4objects.Db4o 07Imports Db4objects.Db4o.Ext 08 09Namespace Db4objects.Db4odoc.IsolatedStorage 10 11 Public Class Db4oIsolatedStorageFile 12 Private _stream As IsolatedStorageFileStream 13 14 <System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _ 15 Private Shared Function FlushFileBuffers(ByVal fileHandle As IntPtr) As Integer 16 End Function 17 18 Public Sub New(ByVal file As String, ByVal fMode As String) 19 Try 20 Dim isf As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly() 21 _stream = New IsolatedStorageFileStream(file, FileMode.OpenOrCreate, IIf(fMode.Equals("rw"), FileAccess.ReadWrite, FileAccess.Read), isf) 22 Catch x As IOException 23 Throw New DatabaseFileLockedException(file, x) 24 End Try 25 End Sub 26 ' end Db4oIsolatedStorageFile 27 28 Public ReadOnly Property Stream() As FileStream 29 Get 30 Return _stream 31 End Get 32 End Property 33 ' end Stream 34 35 Public Sub Close() 36 _stream.Close() 37 End Sub 38 ' end Close 39 40 Public Function Length() As Long 41 Return _stream.Length 42 End Function 43 ' end Length 44 45 Public Function Read(ByVal bytes As Byte(), ByVal offset As Integer, ByVal length As Integer) As Integer 46 Return _stream.Read(bytes, offset, length) 47 End Function 48 ' end Read 49 50 Public Sub Read(ByVal bytes As Byte()) 51 _stream.Read(bytes, 0, bytes.Length) 52 End Sub 53 ' end Read 54 55 Public Sub Seek(ByVal pos As Long) 56 _stream.Seek(pos, SeekOrigin.Begin) 57 End Sub 58 ' end Seek 59 60 Public Sub Sync() 61 _stream.Flush() 62 63 FlushFileBuffers(_stream.SafeFileHandle.DangerousGetHandle()) 64 65 End Sub 66 ' end Sync 67 68 Public Function GetFD() As Db4oIsolatedStorageFile 69 Return Me 70 End Function 71 ' end GetFD 72 73 Public Sub Write(ByVal bytes As Byte()) 74 Me.Write(bytes, 0, bytes.Length) 75 End Sub 76 ' end Write 77 78 Public Sub Write(ByVal bytes As Byte(), ByVal offset As Integer, ByVal length As Integer) 79 _stream.Write(bytes, offset, length) 80 End Sub 81 ' end Write 82 End Class 83End Namespace

The full example code can be downloaded by clicking on the top right button of the code block.