@Override
  public GcsPath subpath(int beginIndex, int endIndex) {
    checkArgument(beginIndex >= 0);
    checkArgument(endIndex > beginIndex);

    Iterator<Path> iterator = iterator();
    for (int i = 0; i < beginIndex; ++i) {
      checkArgument(iterator.hasNext());
      iterator.next();
    }

    GcsPath path = null;
    while (beginIndex < endIndex) {
      checkArgument(iterator.hasNext());
      if (path == null) {
        path = (GcsPath) iterator.next();
      } else {
        path = path.resolve(iterator.next());
      }
      ++beginIndex;
    }

    return path;
  }
@Test
  public void testSubpath() {
    GcsPath a = GcsPath.fromComponents("bucket", "a/b/c/d");
    assertThat(a.subpath(0, 1).toString(), Matchers.equalTo("gs://bucket/"));
    assertThat(a.subpath(0, 2).toString(), Matchers.equalTo("gs://bucket/a"));
    assertThat(a.subpath(0, 3).toString(), Matchers.equalTo("gs://bucket/a/b"));
    assertThat(a.subpath(0, 4).toString(), Matchers.equalTo("gs://bucket/a/b/c"));
    assertThat(a.subpath(1, 2).toString(), Matchers.equalTo("a"));
    assertThat(a.subpath(2, 3).toString(), Matchers.equalTo("b"));
    assertThat(a.subpath(2, 4).toString(), Matchers.equalTo("b/c"));
    assertThat(a.subpath(2, 5).toString(), Matchers.equalTo("b/c/d"));
  }