Fix bug: segmented packets, once aggregated, had the interest name and not the returned data name without the segment component.
diff --git a/src/main/java/com/intel/jndn/utils/SegmentedClient.java b/src/main/java/com/intel/jndn/utils/SegmentedClient.java
index 671d6a2..d9deb3e 100644
--- a/src/main/java/com/intel/jndn/utils/SegmentedClient.java
+++ b/src/main/java/com/intel/jndn/utils/SegmentedClient.java
@@ -71,8 +71,7 @@
   @Override
   public Future<Data> getAsync(Face face, Interest interest) {
     List<Future<Data>> segments = getAsyncList(face, interest);
-    Name name = hasSegment(interest.getName()) ? interest.getName().getPrefix(-1) : interest.getName();
-    return new SegmentedFutureData(name, segments);
+    return new SegmentedFutureData(interest.getName(), segments);
   }
 
   /**
@@ -103,10 +102,8 @@
   public List<Future<Data>> getAsyncList(Face face, Interest interest) {
     // get first segment; default 0 or use a specified start segment
     long firstSegment = 0;
-    boolean specifiedSegment = false;
     try {
       firstSegment = interest.getName().get(-1).toSegment();
-      specifiedSegment = true;
     } catch (EncodingException e) {
       // check for interest selector if no initial segment found
       if (interest.getChildSelector() == -1) {
@@ -120,9 +117,11 @@
     segments.add(SimpleClient.getDefault().getAsync(face, interest));
 
     // retrieve first packet and find the FinalBlockId
+    Data firstData;
     long lastSegment;
     try {
-      lastSegment = segments.get(0).get().getMetaInfo().getFinalBlockId().toSegment();
+      firstData = segments.get(0).get();
+      lastSegment = firstData.getMetaInfo().getFinalBlockId().toSegment();
     } catch (ExecutionException | InterruptedException e) {
       logger.log(Level.FINE, "Failed to retrieve first segment: " + interest.toUri(), e);
       ((FutureData) segments.get(0)).reject(e); // TODO implies knowledge of underlying data structure
@@ -132,14 +131,18 @@
       return segments;
     }
 
-    // cut interest segment off
-    if (specifiedSegment) {
-      interest.setName(interest.getName().getPrefix(-1));
+    // set follow-on segment names to match first segment retrieve
+    Interest interestCopy = new Interest(interest);
+    if (hasSegment(firstData.getName())) {
+      interestCopy.setName(firstData.getName().getPrefix(-1)); // cut last segment number if present
+    } else {
+      logger.log(Level.FINER, "First packet retrieved does not have a segment number, continuing on: " + firstData.getName().toUri());
+      interestCopy.setName(firstData.getName());
     }
 
     // send interests in remaining segments
     for (long i = firstSegment + 1; i <= lastSegment; i++) {
-      Interest segmentedInterest = new Interest(interest);
+      Interest segmentedInterest = new Interest(interestCopy);
       segmentedInterest.getName().appendSegment(i);
       Future<Data> futureData = SimpleClient.getDefault().getAsync(face, segmentedInterest);
       segments.add((int) i, futureData);
diff --git a/src/main/java/com/intel/jndn/utils/client/SegmentedFutureData.java b/src/main/java/com/intel/jndn/utils/client/SegmentedFutureData.java
index 403e2da..66d01be 100644
--- a/src/main/java/com/intel/jndn/utils/client/SegmentedFutureData.java
+++ b/src/main/java/com/intel/jndn/utils/client/SegmentedFutureData.java
@@ -13,6 +13,7 @@
  */
 package com.intel.jndn.utils.client;
 
+import com.intel.jndn.utils.SegmentedClient;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.List;
@@ -37,32 +38,21 @@
  */
 public class SegmentedFutureData implements Future<Data> {
 
-  private final Name name;
+  private final Name interestName;
   List<Future<Data>> segments;
   private boolean cancelled = false;
 
   /**
    * Constructor
    *
-   * @param name this will be the name of the returned Data packet, regardless
-   * of suffixes (e.g. segment components) on each segment packet
-   * @param segments
+   * @param interestName the {@link Name} of the original interest, for debugging purposes
+   * @param segments the list of future segments to retrieve
    */
-  public SegmentedFutureData(Name name, List<Future<Data>> segments) {
-    this.name = name;
+  public SegmentedFutureData(Name interestName, List<Future<Data>> segments) {
+    this.interestName = interestName;
     this.segments = segments;
   }
-
-  /**
-   * Get the Interest name; this will also be the name of the Data packet
-   * returned from get().
-   *
-   * @return
-   */
-  public Name getName() {
-    return name;
-  }
-
+  
   /**
    * Cancel the current request.
    *
@@ -122,13 +112,14 @@
       try {
         content.write(futureData.get().getContent().getImmutableArray());
       } catch (ExecutionException | IOException | InterruptedException e) {
-        throw new ExecutionException("Failed while aggregating retrieved packets: " + name.toUri(), e);
+        throw new ExecutionException("Failed while aggregating retrieved packets: " + interestName.toUri(), e);
       }
     }
 
     // build aggregated packet (copy first packet)
-    Data data = new Data(segments.get(0).get());
-    data.setName(getName());
+    Data firstData = segments.get(0).get();
+    Data data = new Data(firstData);
+    data.setName(getNameFromFirstData(firstData));
     data.setContent(new Blob(content.toByteArray()));
     return data;
   }
@@ -154,19 +145,28 @@
       try {
         content.write(futureData.get().getContent().getImmutableArray());
       } catch (ExecutionException | IOException | InterruptedException e) {
-        throw new ExecutionException("Failed while aggregating retrieved packets: " + name.toUri(), e);
+        throw new ExecutionException("Failed while aggregating retrieved packets: " + interestName.toUri(), e);
       }
 
       // check for timeout
       if (System.currentTimeMillis() > endTime) {
-        throw new TimeoutException("Timed out while retrieving packets: " + name.toUri());
+        throw new TimeoutException("Timed out while retrieving packets: " + interestName.toUri());
       }
     }
-
+    
     // build aggregated packet (copy first packet)
-    Data data = new Data(segments.get(0).get());
-    data.setName(getName());
+    Data firstData = segments.get(0).get();
+    Data data = new Data(firstData);
+    data.setName(getNameFromFirstData(firstData));
     data.setContent(new Blob(content.toByteArray()));
     return data;
   }
+  
+  private Name getNameFromFirstData(Data firstPacket) throws InterruptedException, ExecutionException{
+    Name firstPacketName = segments.get(0).get().getName();
+    if(SegmentedClient.hasSegment(firstPacketName)){
+      firstPacketName = firstPacketName.getPrefix(-1);
+    }
+    return firstPacketName;
+  }
 }
diff --git a/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java b/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
index 319ee6a..df6d056 100644
--- a/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
+++ b/src/test/java/com/intel/jndn/utils/SegmentedClientTest.java
@@ -16,6 +16,7 @@
 import com.intel.jndn.mock.MockFace;
 import com.intel.jndn.utils.client.SegmentedFutureData;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
@@ -105,25 +106,46 @@
   }
 
   /**
-   * Ensure Name of the returned Data is the same as was requested; identifies
-   * bug where the last Name.Component was always cut off.
+   * Identifies bug where the last Name.Component was always cut off.
    *
    * @throws InterruptedException
    * @throws ExecutionException
    */
   @Test
   public void testNameShorteningLogic() throws InterruptedException, ExecutionException {
-    Name name = new Name("/test/123").appendSegment(15);
-    Data data = buildSegmentedData(name);
-    data.setContent(new Blob("...."));
-    face.addResponse(name, data);
+    final List<Data> segments = buildSegmentedData("/test/name", 10);
+    for (Data segment : segments) {
+      face.addResponse(segment.getName(), segment);
+    }
+
+    Name name = new Name("/test/name").appendSegment(0);
 
     SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name);
-    assertEquals(name.getPrefix(-1).toUri(), future.getName().toUri());
     assertEquals(name.getPrefix(-1).toUri(), future.get().getName().toUri());
   }
 
   /**
+   * Verify that Data returned with a different Name than the Interest is still
+   * segmented correctly.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testWhenDataNameIsLongerThanInterestName() throws Exception {
+    final List<Data> segments = buildSegmentedData("/a/b/c/d", 10);
+    for (Data segment : segments) {
+      face.addResponse(segment.getName(), segment);
+    }
+
+    Name name = new Name("/a/b");
+    face.addResponse(name, segments.get(0));
+
+    Data data = SegmentedClient.getDefault().getSync(face, name);
+    assertNotNull(data);
+    assertEquals("/a/b/c/d", data.getName().toUri());
+  }
+
+  /**
    * Verify that Data packets with no content do not cause errors; identifies
    * bug.
    *
@@ -152,7 +174,7 @@
     data1.setContent(new Blob("0123456789"));
     data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
     face.addResponse(data1.getName(), data1);
-    
+
     Data data2 = new Data(new Name("/test/content-length").appendSegment(1));
     data2.setContent(new Blob("0123456789"));
     data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
@@ -192,4 +214,18 @@
     return data;
   }
 
+  private List<Data> buildSegmentedData(String name, int numSegments) {
+    Name.Component finalBlockId = Name.Component.fromNumberWithMarker(numSegments - 1, 0x00);
+    List<Data> segments = new ArrayList<>(numSegments);
+
+    for (int i = 0; i < numSegments; i++) {
+      Data data = new Data(new Name(name).appendSegment(i));
+      data.setContent(new Blob("0123456789"));
+      data.getMetaInfo().setFinalBlockId(finalBlockId);
+      segments.add(data);
+    }
+
+    return segments;
+  }
+
 }