Guest User

Untitled

a guest
Apr 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. /** A subclass of RandomAccessFile to enable basic buffering to a byte array
  2. * Copyright (C) 2009 minddumped.blogspot.com
  3.  
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8.  
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13.  
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see .
  16. */
  17.  
  18. import java.io.RandomAccessFile;
  19. import java.io.FileNotFoundException;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import clojure.lang.IObj;
  23. import clojure.lang.IPersistentMap;
  24.  
  25.  
  26. /**
  27. *
  28. * @author minddumped.blogspot.com original code
  29. * @author moritz.angermann@gmail.com added clojure integration
  30. */
  31. public class BufferedRandomAccessFile
  32. extends RandomAccessFile
  33. implements IObj {
  34.  
  35. private final static int _bufferlength = 65536;
  36.  
  37. public BufferedRandomAccessFile(IPersistentMap meta,
  38. File file,
  39. String mode,
  40. int bufflen)
  41. throws FileNotFoundException {
  42. super(file, mode); _meta = meta; init(file, mode, bufflen);
  43. }
  44. public BufferedRandomAccessFile(File file, String mode, int bufflen)
  45. throws FileNotFoundException {
  46. super(file, mode); _meta = null; init(file, mode, bufflen);
  47. }
  48. public BufferedRandomAccessFile(IPersistentMap meta, File file, String mode)
  49. throws FileNotFoundException {
  50. super(file, mode); _meta = meta; init(file, mode, _bufferlength);
  51. }
  52. public BufferedRandomAccessFile(File file, String mode)
  53. throws FileNotFoundException {
  54. super(file, mode); _meta = null; init(file, mode, _bufferlength);
  55. }
  56. public BufferedRandomAccessFile(IPersistentMap meta, File file)
  57. throws FileNotFoundException {
  58. super(file, "r"); _meta = meta; init(file,"r", _bufferlength);
  59. }
  60. public BufferedRandomAccessFile(File file)
  61. throws FileNotFoundException {
  62. super(file, "r"); _meta = null; init(file, "r", _bufferlength);
  63. }
  64.  
  65. public BufferedRandomAccessFile(IPersistentMap meta,
  66. File file,
  67. String mode,
  68. int bufflen,
  69. long position)
  70. throws FileNotFoundException, IOException {
  71. super(file, mode); _meta = meta; init(file, mode, bufflen);
  72. seek(position);
  73. }
  74. public BufferedRandomAccessFile(File file, String mode, int bufflen, long position)
  75. throws FileNotFoundException, IOException {
  76. super(file, mode); _meta = null; init(file, mode, bufflen);
  77. seek(position);
  78. }
  79. public BufferedRandomAccessFile(IPersistentMap meta,
  80. File file,
  81. String mode,
  82. long position)
  83. throws FileNotFoundException, IOException {
  84. super(file, mode); _meta = meta; init(file, mode, _bufferlength);
  85. seek(position);
  86. }
  87. public BufferedRandomAccessFile(File file, String mode, long position)
  88. throws FileNotFoundException, IOException {
  89. super(file, mode); _meta = null; init(file, mode, _bufferlength);
  90. seek(position);
  91. }
  92.  
  93. private void init(File file, String mode, int bufflen) {
  94. bufferlength = bufflen;
  95. bytebuffer = new byte[bufferlength];
  96. maxread = 0;
  97. buffpos = 0;
  98. sb = new StringBuilder("0");
  99. _file = file;
  100. _mode = mode;
  101. }
  102.  
  103. private byte[] bytebuffer;
  104. private int bufferlength;
  105. private int maxread;
  106. private int buffpos;
  107. private StringBuilder sb;
  108.  
  109. // Clojure Meta support
  110. final IPersistentMap _meta;
  111. private File _file;
  112. private String _mode;
  113.  
  114. final public IPersistentMap meta(){
  115. return _meta;
  116. }
  117.  
  118. public BufferedRandomAccessFile withMeta(IPersistentMap meta) {
  119. if(meta != _meta)
  120. try {
  121. return new BufferedRandomAccessFile(meta, _file, _mode, bufferlength, getFilePointer());
  122. } catch (Exception e) { return null; } // FIXME
  123. return this;
  124. }
  125.  
  126. public int getbuffpos() {
  127. return buffpos;
  128. }
  129.  
  130. @Override
  131. public int read() throws IOException {
  132. if (buffpos >= maxread) {
  133. maxread = readchunk();
  134. if (maxread == -1) {
  135. return -1;
  136. }
  137. }
  138. buffpos++;
  139. return bytebuffer[buffpos - 1];
  140. }
  141.  
  142. public String readLine2() throws IOException {
  143. sb.delete(0, sb.length());
  144. int c = -1;
  145. boolean eol = false;
  146. while (!eol) {
  147. switch (c = read()) {
  148. case -1:
  149. case '\n':
  150. eol = true;
  151. break;
  152. case '\r':
  153. eol = true;
  154. long cur = getFilePointer();
  155. if ((read()) != '\n') {
  156. seek(cur);
  157. }
  158. break;
  159. default:
  160. sb.append((char) c);
  161. break;
  162. }
  163. }
  164.  
  165. if ((c == -1) && (sb.length() == 0)) {
  166. return null;
  167. }
  168. return sb.toString();
  169. }
  170.  
  171. @Override
  172. public long getFilePointer() throws IOException {
  173. return super.getFilePointer() + buffpos;
  174. }
  175.  
  176. @Override
  177. public void seek(long pos) throws IOException {
  178. if (maxread != -1 && pos < (super.getFilePointer() + maxread) && pos > super.getFilePointer()) {
  179. Long diff = (pos - super.getFilePointer());
  180. if (diff < Integer.MAX_VALUE) {
  181. buffpos = diff.intValue();
  182. } else {
  183. throw new IOException("something wrong w/ seek");
  184. }
  185. } else {
  186. buffpos = 0;
  187. super.seek(pos);
  188. maxread = readchunk();
  189. }
  190. }
  191.  
  192. private int readchunk() throws IOException {
  193. long pos = super.getFilePointer() + buffpos;
  194. super.seek(pos);
  195. int read = super.read(bytebuffer);
  196. super.seek(pos);
  197. buffpos = 0;
  198. return read;
  199. }
  200. }
Add Comment
Please, Sign In to add comment