/** * Seek for box with the specified id starting from the current location of filepointer, * * <p>Note it won't find the box if it is contained with a level below the current level, nor if * we are at a parent atom that also contains data and we havent yet processed the data. It will * work if we are at the start of a child box even if it not the required box as long as the box * we are looking for is the same level (or the level above in some cases). * * @param data * @param id * @throws java.io.IOException */ public static Mp4BoxHeader seekWithinLevel(ByteBuffer data, String id) throws IOException { logger.finer("Started searching for:" + id + " in bytebuffer at" + data.position()); Mp4BoxHeader boxHeader = new Mp4BoxHeader(); if (data.remaining() >= Mp4BoxHeader.HEADER_LENGTH) { boxHeader.update(data); } else { return null; } while (!boxHeader.getId().equals(id)) { logger.finer( "Found" + boxHeader.getId() + "Still searching for:" + id + " in bytebuffer at" + data.position()); // Something gone wrong probably not at the start of an atom so return null; if (boxHeader.getLength() < Mp4BoxHeader.HEADER_LENGTH) { return null; } data.position(data.position() + (boxHeader.getLength() - HEADER_LENGTH)); if (data.remaining() >= Mp4BoxHeader.HEADER_LENGTH) { boxHeader.update(data); } else { return null; } } return boxHeader; }
/** * Seek for box with the specified id starting from the current location of filepointer, * * <p>Note it wont find the box if it is contained with a level below the current level, nor if we * are at a parent atom that also contains data and we havent yet processed the data. It will work * if we are at the start of a child box even if it not the required box as long as the box we are * looking for is the same level (or the level above in some cases). * * @param raf * @param id * @throws java.io.IOException */ public static Mp4BoxHeader seekWithinLevel(RandomAccessFile raf, String id) throws IOException { logger.finer("Started searching for:" + id + " in file at:" + raf.getChannel().position()); Mp4BoxHeader boxHeader = new Mp4BoxHeader(); ByteBuffer headerBuffer = ByteBuffer.allocate(HEADER_LENGTH); int bytesRead = raf.getChannel().read(headerBuffer); if (bytesRead != HEADER_LENGTH) { return null; } headerBuffer.rewind(); boxHeader.update(headerBuffer); while (!boxHeader.getId().equals(id)) { logger.finer("Still searching for:" + id + " in file at:" + raf.getChannel().position()); // Something gone wrong probably not at the start of an atom so return null; if (boxHeader.getLength() < Mp4BoxHeader.HEADER_LENGTH) { return null; } int noOfBytesSkipped = raf.skipBytes(boxHeader.getDataLength()); logger.finer("Skipped:" + noOfBytesSkipped); if (noOfBytesSkipped < boxHeader.getDataLength()) { return null; } headerBuffer.rewind(); bytesRead = raf.getChannel().read(headerBuffer); logger.finer("Header Bytes Read:" + bytesRead); headerBuffer.rewind(); if (bytesRead == Mp4BoxHeader.HEADER_LENGTH) { boxHeader.update(headerBuffer); } else { return null; } } return boxHeader; }
/** * Construct header * * <p>Create header using headerdata, expected to find header at headerdata current position * * <p>Note after processing adjusts position to immediately after header * * @param headerData */ public Mp4BoxHeader(ByteBuffer headerData) { update(headerData); }