/** * Segment a stream of bytes into a list of Data packets; this must read all the bytes first in order to determine the * end segment for FinalBlockId. TODO this could be smarter and only add segments if necessary * * @param template the {@link Data} packet to use for the segment {@link Name}, {@link net.named_data.jndn.MetaInfo}, * etc. * @param bytes an {@link InputStream} to the bytes to segment * @return a list of segmented {@link Data} packets * @throws IOException if the stream fails */ public static List<Data> segment(Data template, InputStream bytes, int segmentSize) throws IOException { List<Data> segments = new ArrayList<>(); byte[] readBytes = readAll(bytes); int numBytes = readBytes.length; int numPackets = (int) Math.ceil((double) numBytes / segmentSize); ByteBuffer buffer = ByteBuffer.wrap(readBytes, 0, numBytes); Name.Component lastSegment = Name.Component.fromNumberWithMarker((long) numPackets - 1, NDN_SEGMENT_MARKER); for (int i = 0; i < numPackets; i++) { Data segment = new Data(template); segment.getName().appendSegment(i); segment.getMetaInfo().setFinalBlockId(lastSegment); byte[] content = new byte[Math.min(segmentSize, buffer.remaining())]; buffer.get(content); segment.setContent(new Blob(content)); segments.add(segment); } return segments; }
/** * Replace the final component of an interest name with a segmented component; * if the interest name does not have a segmented component, this will add * one. * * @param interest the request * @param segmentNumber a segment number * @param marker a marker to use for segmenting the packet * @return a segmented interest (a copy of the passed interest) */ protected Interest replaceFinalComponent(Interest interest, long segmentNumber, byte marker) { Interest copied = new Interest(interest); Component lastComponent = Component.fromNumberWithMarker(segmentNumber, marker); Name newName = (SegmentationHelper.isSegmented(copied.getName(), marker)) ? copied.getName().getPrefix(-1) : new Name(copied.getName()); copied.setName(newName.append(lastComponent)); return copied; }
static Name.Component toPublisherComponent(long publisherId) { return Name.Component.fromNumberWithMarker(publisherId, PubSubNamespace.PUBLISHER_ID_MARKER); }
static Name toMessageName(Name prefix, long publisherId, long messageId) { Name.Component messageComponent = Name.Component.fromNumberWithMarker(messageId, PubSubNamespace.MESSAGE_ID_MARKER); return toPublisherName(prefix, publisherId).append(messageComponent); }
static Name toAnnouncement(Name prefix, long publisherId, Announcement announcement) { Name.Component announcementComponent = Name.Component.fromNumberWithMarker(announcement.id, PubSubNamespace.ANNOUNCEMENT_ACTION_MARKER); return toPublisherName(prefix, publisherId).append(announcementComponent); }
static Name toMessageName(Name publisherPrefix, long messageId) { Name.Component messageComponent = Name.Component.fromNumberWithMarker(messageId, PubSubNamespace.MESSAGE_ID_MARKER); return new Name(publisherPrefix).append(messageComponent); }