| 38 | |
| 39 | # Write Some Data |
| 40 | def write(self, index, data): |
| 41 | size = len(data) |
| 42 | assert type(index) is int and 0 <= index < self.__end |
| 43 | assert type(data) is str and index < index + size <= self.__end |
| 44 | first_index = index |
| 45 | last_index = index + size - 1 |
| 46 | first_block = first_index / self.__size + 1 |
| 47 | last_block = last_index / self.__size + 2 |
| 48 | blocks = last_block - first_block |
| 49 | if blocks == 1: |
| 50 | if size == self.__size: |
| 51 | self.__write(first_block, data) |
| 52 | else: |
| 53 | stream = self.__read(first_block) |
| 54 | first_index = index % self.__size |
| 55 | last_index = first_index + size |
| 56 | stream = stream[:first_index] + data + stream[last_index:] |
| 57 | self.__write(first_block, stream) |
| 58 | else: |
| 59 | if index % self.__size: |
| 60 | stream = self.__read(first_block) |
| 61 | first_index = index % self.__size |
| 62 | last_index = self.__size - first_index |
| 63 | stream = stream[:first_index] + data[:last_index] |
| 64 | data = data[last_index:] |
| 65 | self.__write(first_block, stream) |
| 66 | else: |
| 67 | last_index = self.__size |
| 68 | stream = data[:last_index] |
| 69 | data = data[last_index:] |
| 70 | self.__write(first_block, stream) |
| 71 | first_block += 1 |
| 72 | last_block -= 1 |
| 73 | for block in range(first_block, last_block): |
| 74 | self.__write(block, data[:self.__size]) |
| 75 | data = data[self.__size:] |
| 76 | size = len(data) |
| 77 | if size == self.__size: |
| 78 | self.__write(last_block, data) |
| 79 | else: |
| 80 | stream = self.__read(last_block) |
| 81 | stream = data + stream[size:] |
| 82 | self.__write(last_block, stream) |
| 83 | |
| 84 | # Erase Some Data |
| 85 | def erase(self, index, size): |