Exemple #1
0
 public GlobPattern(ByteList bytelist, int flags) {
   this(
       bytelist.getUnsafeBytes(),
       bytelist.getBegin(),
       bytelist.getBegin() + bytelist.getRealSize(),
       flags);
 }
Exemple #2
0
 public boolean containsAll(ByteList c) {
   for (int i = 0; i != c.size(); ++i) {
     if (!this.contains(c.get(i))) {
       return false;
     }
   }
   return true;
 }
Exemple #3
0
 public void init(ByteList list, boolean isStrict, boolean is19) {
   bytes = list.getUnsafeBytes();
   index = list.begin();
   endIndex = index + list.length();
   this.isStrict = isStrict;
   this.is19 = is19;
   chars = new char[list.length() + 1]; // +1 for implicit '0' for terminating 'E'
   charsIndex = 0;
 }
Exemple #4
0
 public boolean removeAll(ByteList list) {
   boolean removed = false;
   for (int i = 0; i != list.size(); ++i) {
     byte val = list.get(i);
     while (this.remove(val)) {
       removed = true;
     }
   }
   return removed;
 }
Exemple #5
0
 public boolean addAll(int index, ByteList list) {
   checkIndex(index);
   ensureCapacity(list.size());
   int toMove = this.size() - index;
   System.arraycopy(
       this.underlyingArray, index, this.underlyingArray, index + list.size(), toMove);
   System.arraycopy(list.underlyingArray, 0, this.underlyingArray, index, list.size());
   this.pos += list.size();
   return true;
 }
 public void testAddAllIsUnsupportedByDefault() {
   RandomAccessByteList list = new AbstractRandomAccessByteListImpl();
   ByteList list2 = new ArrayByteList();
   list2.add((byte) 3);
   try {
     list.addAll(list2);
     fail("Expected UnsupportedOperationException");
   } catch (UnsupportedOperationException e) {
     // expected
   }
 }
Exemple #7
0
 /**
  * Retrieves and returns the whole text and closes the stream.
  *
  * @return contents
  * @throws IOException I/O exception
  */
 public byte[] content() throws IOException {
   try {
     if (length > 0) {
       // input length is known in advance
       final int sl = (int) Math.min(Integer.MAX_VALUE, length);
       final byte[] bytes = new byte[sl];
       for (int c = 0; c < sl; c++) bytes[c] = (byte) next();
       return bytes;
     }
     // parse until end of stream
     final ByteList bl = new ByteList();
     for (int ch; (ch = next()) != -1; ) bl.add(ch);
     return bl.toArray();
   } finally {
     close();
   }
 }
Exemple #8
0
  public static List<ByteList> push_glob(String cwd, ByteList globByteList, int flags) {
    List<ByteList> result = new ArrayList<ByteList>();
    if (globByteList.length() > 0) {
      push_braces(cwd, result, new GlobPattern(globByteList, flags));
    }

    return result;
  }
 public boolean equals(Object that) {
   if (this == that) {
     return true;
   } else if (that instanceof ByteList) {
     ByteList thatList = (ByteList) that;
     if (size() != thatList.size()) {
       return false;
     }
     for (ByteIterator thatIter = thatList.iterator(), thisIter = iterator();
         thisIter.hasNext(); ) {
       if (thisIter.next() != thatIter.next()) {
         return false;
       }
     }
     return true;
   } else {
     return false;
   }
 }
Exemple #10
0
    @Override
    public HTTPResponse call() throws Exception {
      final HttpURLConnection hc = (HttpURLConnection) uri.toURL().openConnection();
      hc.setReadTimeout(SOCKET_TIMEOUT);
      try {
        while (!stop) {
          try {
            final int code = hc.getResponseCode();

            final InputStream input = hc.getInputStream();
            final ByteList bl = new ByteList();
            for (int i; (i = input.read()) != -1; ) bl.add(i);

            return new HTTPResponse(code, bl.toString());
          } catch (final SocketTimeoutException e) {
          }
        }
        return null;
      } finally {
        hc.disconnect();
      }
    }
Exemple #11
0
  /*
   * Process {}'s (example: Dir.glob("{jruby,jython}/README*")
   */
  private static int push_braces(String cwd, List<ByteList> result, GlobPattern pattern) {
    pattern.reset();
    int lbrace = pattern.indexOf((byte) '{'); // index of left-most brace
    int rbrace = pattern.findClosingIndexOf(lbrace); // index of right-most brace

    // No or mismatched braces..Move along..nothing to see here
    if (lbrace == -1 || rbrace == -1) return push_globs(cwd, result, pattern);

    // Peel onion...make subpatterns out of outer layer of glob and recall with each subpattern
    // Example: foo{a{c},b}bar -> fooa{c}bar, foobbar
    ByteList buf = new ByteList(20);
    int middleRegionIndex;
    int i = lbrace;
    while (pattern.bytes[i] != '}') {
      middleRegionIndex = i + 1;
      for (i = middleRegionIndex;
          i < pattern.end && pattern.bytes[i] != '}' && pattern.bytes[i] != ',';
          i++) {
        if (pattern.bytes[i] == '{') i = pattern.findClosingIndexOf(i); // skip inner braces
      }

      buf.length(0);
      buf.append(pattern.bytes, pattern.begin, lbrace - pattern.begin);
      buf.append(pattern.bytes, middleRegionIndex, i - middleRegionIndex);
      buf.append(pattern.bytes, rbrace + 1, pattern.end - (rbrace + 1));
      int status =
          push_braces(
              cwd,
              result,
              new GlobPattern(
                  buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), pattern.flags));
      if (status != 0) return status;
    }

    return 0; // All braces pushed..
  }
Exemple #12
0
 public void testByteList() {
   ByteList bl = new ByteList();
   bl.checksum = asList(base16().lowerCase().decode("1dda05ed139664f1f89b9dec482b77c0"));
   assertEquals(json.toJson(bl), "{\"checksum\":\"1dda05ed139664f1f89b9dec482b77c0\"}");
   assertEquals(json.fromJson(json.toJson(bl), ByteList.class).checksum, bl.checksum);
 }
Exemple #13
0
 /**
  * Reads a byte array from the input stream, suffixed by a {@code 0} byte.
  *
  * @return token
  * @throws IOException I/O Exception
  */
 public final byte[] readBytes() throws IOException {
   final ByteList bl = new ByteList();
   for (int l; (l = next()) > 0; ) bl.add(l);
   return bl.toArray();
 }
Exemple #14
0
 /**
  * Reads a string from the input stream, suffixed by a {@code 0} byte.
  *
  * @return string
  * @throws IOException I/O Exception
  */
 public final String readString() throws IOException {
   final ByteList bl = new ByteList();
   for (int l; (l = next()) > 0; ) bl.add(l);
   return bl.toString();
 }
/*     */     public boolean addAll(long index, ByteList l) {
/* 588 */       ensureIndex(index);
/* 589 */       this.to += l.size();
/*     */ 
/* 595 */       return this.l.addAll(this.from + index, l);
/*     */     }
Exemple #16
0
  private static int glob_helper(
      String cwd,
      byte[] bytes,
      int begin,
      int end,
      int sub,
      int flags,
      GlobFunc func,
      GlobArgs arg) {
    int p, m;
    int status = 0;
    byte[] newpath = null;
    File st;
    p = sub != -1 ? sub : begin;
    if (!has_magic(bytes, p, end, flags)) {
      if (DOSISH || (flags & FNM_NOESCAPE) == 0) {
        newpath = new byte[end];
        System.arraycopy(bytes, 0, newpath, 0, end);
        if (sub != -1) {
          p = (sub - begin);
          end = remove_backslashes(newpath, p, end);
          sub = p;
        } else {
          end = remove_backslashes(newpath, 0, end);
          bytes = newpath;
        }
      }

      if (bytes[begin] == '/'
          || (DOSISH && begin + 2 < end && bytes[begin + 1] == ':' && isdirsep(bytes[begin + 2]))) {
        if (new JavaSecuredFile(newStringFromUTF8(bytes, begin, end - begin)).exists()) {
          status = func.call(bytes, begin, end - begin, arg);
        }
      } else if (isJarFilePath(bytes, begin, end)) {
        int ix = end;
        for (int i = 0; i < end; i++) {
          if (bytes[begin + i] == '!') {
            ix = i;
            break;
          }
        }

        st = new JavaSecuredFile(newStringFromUTF8(bytes, begin + 5, ix - 5));
        try {
          String jar = newStringFromUTF8(bytes, begin + ix + 1, end - (ix + 1));
          JarFile jf = new JarFile(st);

          if (jar.startsWith("/")) jar = jar.substring(1);
          ZipEntry entry = RubyFile.getDirOrFileEntry(jf, jar);
          if (entry != null) {
            status = func.call(bytes, begin, end, arg);
          }
        } catch (Exception e) {
        }
      } else if ((end - begin)
          > 0) { // Length check is a hack.  We should not be reeiving "" as a filename ever.
        if (new JavaSecuredFile(cwd, newStringFromUTF8(bytes, begin, end - begin)).exists()) {
          status = func.call(bytes, begin, end - begin, arg);
        }
      }

      return status;
    }

    ByteList buf = new ByteList(20);
    List<ByteList> link = new ArrayList<ByteList>();
    mainLoop:
    while (p != -1 && status == 0) {
      if (bytes[p] == '/') p++;

      m = strchr(bytes, p, end, (byte) '/');
      if (has_magic(bytes, p, m == -1 ? end : m, flags)) {
        finalize:
        do {
          byte[] base = extract_path(bytes, begin, p);
          byte[] dir = begin == p ? new byte[] {'.'} : base;
          byte[] magic = extract_elem(bytes, p, end);
          boolean recursive = false;
          String jar = null;
          JarFile jf = null;

          if (dir[0] == '/' || (DOSISH && 2 < dir.length && dir[1] == ':' && isdirsep(dir[2]))) {
            st = new JavaSecuredFile(newStringFromUTF8(dir));
          } else if (isJarFilePath(dir, 0, dir.length)) {
            int ix = dir.length;
            for (int i = 0; i < dir.length; i++) {
              if (dir[i] == '!') {
                ix = i;
                break;
              }
            }

            st = new JavaSecuredFile(newStringFromUTF8(dir, 5, ix - 5));
            jar = newStringFromUTF8(dir, ix + 1, dir.length - (ix + 1));
            try {
              jf = new JarFile(st);

              if (jar.startsWith("/")) jar = jar.substring(1);
              if (jf.getEntry(jar + "/") != null) jar = jar + "/";
            } catch (Exception e) {
              jar = null;
              jf = null;
            }
          } else {
            st = new JavaSecuredFile(cwd, newStringFromUTF8(dir));
          }

          if ((jf != null
                  && ("".equals(jar)
                      || (jf.getJarEntry(jar) != null && jf.getJarEntry(jar).isDirectory())))
              || st.isDirectory()) {
            if (m != -1 && Arrays.equals(magic, DOUBLE_STAR)) {
              int n = base.length;
              recursive = true;
              buf.length(0);
              buf.append(base);
              buf.append(bytes, (base.length > 0 ? m : m + 1), end - (base.length > 0 ? m : m + 1));
              status =
                  glob_helper(
                      cwd,
                      buf.getUnsafeBytes(),
                      buf.getBegin(),
                      buf.getRealSize(),
                      n,
                      flags,
                      func,
                      arg);
              if (status != 0) {
                break finalize;
              }
            }
          } else {
            break mainLoop;
          }

          if (jar == null) {
            String[] dirp = files(st);

            for (int i = 0; i < dirp.length; i++) {
              if (recursive) {
                byte[] bs = getBytesInUTF8(dirp[i]);
                if (fnmatch(STAR, 0, 1, bs, 0, bs.length, flags) != 0) {
                  continue;
                }
                buf.length(0);
                buf.append(base);
                buf.append(BASE(base) ? SLASH : EMPTY);
                buf.append(getBytesInUTF8(dirp[i]));
                if (buf.getUnsafeBytes()[0] == '/'
                    || (DOSISH
                        && 2 < buf.getRealSize()
                        && buf.getUnsafeBytes()[1] == ':'
                        && isdirsep(buf.getUnsafeBytes()[2]))) {
                  st =
                      new JavaSecuredFile(
                          newStringFromUTF8(
                              buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
                } else {
                  st =
                      new JavaSecuredFile(
                          cwd,
                          newStringFromUTF8(
                              buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
                }
                if (st.isDirectory() && !".".equals(dirp[i]) && !"..".equals(dirp[i])) {
                  int t = buf.getRealSize();
                  buf.append(SLASH);
                  buf.append(DOUBLE_STAR);
                  buf.append(bytes, m, end - m);
                  status =
                      glob_helper(
                          cwd,
                          buf.getUnsafeBytes(),
                          buf.getBegin(),
                          buf.getRealSize(),
                          t,
                          flags,
                          func,
                          arg);
                  if (status != 0) {
                    break;
                  }
                }
                continue;
              }
              byte[] bs = getBytesInUTF8(dirp[i]);
              if (fnmatch(magic, 0, magic.length, bs, 0, bs.length, flags) == 0) {
                buf.length(0);
                buf.append(base);
                buf.append(BASE(base) ? SLASH : EMPTY);
                buf.append(getBytesInUTF8(dirp[i]));
                if (m == -1) {
                  status = func.call(buf.getUnsafeBytes(), 0, buf.getRealSize(), arg);
                  if (status != 0) {
                    break;
                  }
                  continue;
                }
                link.add(buf);
                buf = new ByteList(20);
              }
            }
          } else {
            try {
              List<JarEntry> dirp = new ArrayList<JarEntry>();
              for (Enumeration<JarEntry> eje = jf.entries(); eje.hasMoreElements(); ) {
                JarEntry je = eje.nextElement();
                String name = je.getName();
                int ix = name.indexOf('/', jar.length());
                if (ix == -1 || ix == name.length() - 1) {
                  if ("/".equals(jar) || (name.startsWith(jar) && name.length() > jar.length())) {
                    dirp.add(je);
                  }
                }
              }
              for (JarEntry je : dirp) {
                byte[] bs = getBytesInUTF8(je.getName());
                int len = bs.length;

                if (je.isDirectory()) {
                  len--;
                }

                if (recursive) {
                  if (fnmatch(STAR, 0, 1, bs, 0, len, flags) != 0) {
                    continue;
                  }
                  buf.length(0);
                  buf.append(base, 0, base.length - jar.length());
                  buf.append(BASE(base) ? SLASH : EMPTY);
                  buf.append(bs, 0, len);

                  if (je.isDirectory()) {
                    int t = buf.getRealSize();
                    buf.append(SLASH);
                    buf.append(DOUBLE_STAR);
                    buf.append(bytes, m, end - m);
                    status =
                        glob_helper(
                            cwd,
                            buf.getUnsafeBytes(),
                            buf.getBegin(),
                            buf.getRealSize(),
                            t,
                            flags,
                            func,
                            arg);
                    if (status != 0) {
                      break;
                    }
                  }
                  continue;
                }

                if (fnmatch(magic, 0, magic.length, bs, 0, len, flags) == 0) {
                  buf.length(0);
                  buf.append(base, 0, base.length - jar.length());
                  buf.append(BASE(base) ? SLASH : EMPTY);
                  buf.append(bs, 0, len);
                  if (m == -1) {
                    status = func.call(buf.getUnsafeBytes(), 0, buf.getRealSize(), arg);
                    if (status != 0) {
                      break;
                    }
                    continue;
                  }
                  link.add(buf);
                  buf = new ByteList(20);
                }
              }
            } catch (Exception e) {
            }
          }
        } while (false);

        if (link.size() > 0) {
          for (ByteList b : link) {
            if (status == 0) {
              if (b.getUnsafeBytes()[0] == '/'
                  || (DOSISH
                      && 2 < b.getRealSize()
                      && b.getUnsafeBytes()[1] == ':'
                      && isdirsep(b.getUnsafeBytes()[2]))) {
                st = new JavaSecuredFile(newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
              } else {
                st =
                    new JavaSecuredFile(
                        cwd, newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
              }

              if (st.isDirectory()) {
                int len = b.getRealSize();
                buf.length(0);
                buf.append(b);
                buf.append(bytes, m, end - m);
                status =
                    glob_helper(
                        cwd, buf.getUnsafeBytes(), 0, buf.getRealSize(), len, flags, func, arg);
              }
            }
          }
          break mainLoop;
        }
      }
      p = m;
    }
    return status;
  }
Exemple #17
0
 boolean hasNext() {
   return this.pos < list.size();
 }
Exemple #18
0
 byte next() {
   if (pos >= this.list.size() || pos < 0) {
     throw new java.util.NoSuchElementException();
   }
   return list.get(this.pos++);
 }
Exemple #19
0
 public boolean addAll(ByteList list) {
   ensureCapacity(list.size());
   System.arraycopy(list.underlyingArray, 0, this.underlyingArray, this.size(), list.size());
   this.pos += list.size();
   return true;
 }