internal void WriteByteArrayTo(IJournalledResource data, long position, byte[] buf, int off, int len) { if (PARANOID_CHECKS) { lock (write_lock) { if (write_lock_count == 0) { Console.Out.WriteLine("Write without a Lock!"); Console.Out.WriteLine(new ApplicationException().StackTrace); } } } long page_number = position / page_size; int start_offset = (int)(position % page_size); int to_write = System.Math.Min(len, page_size - start_offset); BMPage page = FetchPage(data, page_number); lock (page) { try { page.Initialize(); page.Write(start_offset, buf, off, to_write); } finally { page.Dispose(); } } len -= to_write; while (len > 0) { off += to_write; position += to_write; ++page_number; to_write = System.Math.Min(len, page_size); page = FetchPage(data, page_number); lock (page) { try { page.Initialize(); page.Write(0, buf, off, to_write); } finally { page.Dispose(); } } len -= to_write; } }
internal void WriteByteTo(IJournalledResource data, long position, int b) { if (PARANOID_CHECKS) { lock (write_lock) { if (write_lock_count == 0) { Console.Out.WriteLine("Write without a Lock!"); Console.Out.WriteLine(new ApplicationException().StackTrace); } } } long page_number = position / page_size; BMPage page = FetchPage(data, page_number); lock (page) { try { page.Initialize(); page.Write((int)(position % page_size), (byte)b); } finally { page.Dispose(); } } }
internal int ReadByteArrayFrom(IJournalledResource data, long position, byte[] buf, int off, int len) { int orig_len = len; long page_number = position / page_size; int start_offset = (int)(position % page_size); int to_read = System.Math.Min(len, page_size - start_offset); BMPage page = FetchPage(data, page_number); lock (page) { try { page.Initialize(); page.Read(start_offset, buf, off, to_read); } finally { page.Dispose(); } } len -= to_read; while (len > 0) { off += to_read; position += to_read; ++page_number; to_read = System.Math.Min(len, page_size); page = FetchPage(data, page_number); lock (page) { try { page.Initialize(); page.Read(0, buf, off, to_read); } finally { page.Dispose(); } } len -= to_read; } return(orig_len); }
// ------ // Buffered access methods. These are all thread safe methods. When a page // is accessed the page is synchronized so no 2 or more operations can // Read/Write from the page at the same time. An operation can Read/Write to // different pages at the same time, however, and this requires thread safety // at a lower level (in the IJournalledResource implementation). // ------ internal int ReadByteFrom(IJournalledResource data, long position) { long page_number = position / page_size; int v; BMPage page = FetchPage(data, page_number); lock (page) { try { page.Initialize(); v = ((int)page.Read((int)(position % page_size))) & 0x0FF; } finally { page.Dispose(); } } return(v); }