Guest User

Untitled

a guest
Jan 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package mp4parser;
  2.  
  3. import java.io.*;
  4. import java.util.Enumeration;
  5. import java.util.Vector;
  6.  
  7. public class RandomAccessSequenceFile implements Closeable{
  8. Enumeration<RandomAccessFile> e;
  9. private RandomAccessFile rIn;
  10. private long startOffset = 0;//偏移位置
  11. private long endOffset = 0;
  12. private File file = null;//mdathead文件
  13. public RandomAccessSequenceFile(RandomAccessFile s1, RandomAccessFile s2, long startOffset, long endOffset) throws IOException {
  14. //startOffset为原文件的偏移起始位置
  15. this.startOffset = startOffset;
  16. this.endOffset = endOffset;
  17. RandomAccessFile mdatHeadIn = getMdatHead(endOffset-startOffset+8);
  18. Vector<RandomAccessFile> v = new Vector<RandomAccessFile>(2);
  19. // s2.setLength(endOffset);
  20. s2.skipBytes((int) startOffset);//跳到偏移字节位置
  21.  
  22. v.addElement(s1);
  23. v.addElement(mdatHeadIn);
  24. v.addElement(s2);
  25. e = v.elements();
  26. try {
  27. nextStream();
  28. } catch (IOException ex) {
  29. // This should never happen
  30. throw new Error("panic");
  31. }
  32. }
  33. final void nextStream() throws IOException {
  34.  
  35. if (rIn != null) {
  36. rIn.close();
  37. }
  38.  
  39. if (e.hasMoreElements()) {
  40.  
  41. rIn = e.nextElement();
  42. if (rIn == null)
  43. throw new NullPointerException();
  44. }
  45. else{
  46. file.delete();//删除创建的临时文件
  47. rIn = null;
  48. }
  49. }
  50.  
  51. public int read(byte[] bytes) throws IOException {
  52. // System.out.println("偏移量"+rIn.getFilePointer());
  53. if(rIn == null){
  54. return -1;
  55. }
  56. do {
  57. int n = rIn.read(bytes);
  58. if (n > 0) {
  59. return n;
  60. }
  61. nextStream();
  62.  
  63. } while (rIn != null);
  64. return -1;
  65. }
  66. //实现从任意偏移位置开始
  67. public void seek(long start) throws IOException {
  68. if(start>0){
  69. long n = start;
  70. while (n-rIn.length()>0){
  71. n-=rIn.length();
  72. nextStream();
  73. }
  74. rIn.seek(n);
  75. }
  76.  
  77. }
  78.  
  79. //mdat头部
  80. private RandomAccessFile getMdatHead(long size) throws IOException {
  81. byte[] mdathead = {0x00,0x00,0x00,0x00,0x6d,0x64,0x61,0x74};
  82. for (int i = 0; i < 4; i++) {
  83. mdathead[i] = (byte) ((size>>((3-i)*8))&0xff);
  84. // System.out.println(Long.toBinaryString(mdathead[i]));
  85. }
  86. // System.out.println(Long.toBinaryString(mdathead[4]&0xff));
  87. file = File.createTempFile("mdathead",null);
  88. FileOutputStream out = new FileOutputStream(file);
  89. out.write(mdathead);
  90. out.close();
  91. return new RandomAccessFile(file,"r");
  92. }
  93.  
  94. @Override
  95. public void close() throws IOException {
  96. if(rIn!=null)
  97. rIn.close();
  98. while (e.hasMoreElements()) {
  99. rIn = e.nextElement();
  100. if (rIn == null)
  101. rIn.close();
  102. }
  103. file.delete();//删除创建的临时文件
  104. rIn = null;
  105. }
  106. }
Add Comment
Please, Sign In to add comment