Example #1
0
  /**
   * Copies a source file to a destination file, optionally preserving the source's last
   * modification time. We already have an input stream to read the source file, but we know nothing
   * about the destination file yet. Note that this method <em>never</em> closes the given input
   * stream!
   *
   * @throws FileNotFoundException If either the source or the destination cannot get accessed.
   * @throws InputIOException If copying the data fails because of an IOException in the source.
   * @throws IOException If copying the data fails because of an IOException in the destination.
   */
  private static void cp0(
      final boolean preserve, final java.io.File src, final InputStream in, final java.io.File dst)
      throws IOException {
    try {
      if (dst instanceof File) {
        final File dstFile = (File) dst;
        dstFile.ensureNotVirtualRoot("cannot write");
        final String dstEntryName = dstFile.getEnclEntryName();
        if (dstEntryName != null) {
          cp0(preserve, src, in, dstFile.getEnclArchive().getArchiveController(), dstEntryName);
          return;
        }
      }
    } catch (RfsEntryFalsePositiveException dstIsNotArchive) {
    }

    // Treat the destination like a regular file.
    final OutputStream out = new java.io.FileOutputStream(dst);
    try {
      Streams.cat(in, out);
    } finally {
      out.close();
    }
    if (preserve && !dst.setLastModified(src.lastModified()))
      throw new IOException(dst.getPath() + " (cannot preserve last modification time)");
  }
Example #2
0
  @Test
  public void fizzTest() throws Throwable {
    int numOfItems = 1024;
    int batchSize = 8;
    final Timer timer = new Timer();
    AtomicLong globalCounter = new AtomicLong();
    CountDownLatch latch = new CountDownLatch(1);

    Control c =
        Streams.createWith(
                (demand, subscriber) -> {
                  System.out.println("demand is " + demand);
                  if (!subscriber.isCancelled()) {
                    for (int i = 0; i < demand; i++) {
                      long curr = globalCounter.incrementAndGet();
                      if (curr % 5 == 0 && curr % 3 == 0) subscriber.onNext("FizBuz \r\n");
                      else if (curr % 3 == 0) subscriber.onNext("Fiz ");
                      else if (curr % 5 == 0) subscriber.onNext("Buz ");
                      else subscriber.onNext(String.valueOf(curr) + " ");

                      if (globalCounter.get() > numOfItems) {
                        subscriber.onComplete();
                        return;
                      }
                    }
                  }
                })
            .flatMap(
                (s) ->
                    Streams.withOverflowSupport(
                        (sub) ->
                            timer.schedule(
                                new TimerTask() {
                                  @Override
                                  public void run() {
                                    sub.onNext(s);
                                    sub.onComplete();
                                  }
                                },
                                10)))
            .capacity(batchSize)
            .log()
            //                .observe(System.out::print)
            .consume(numOfItems);

    while (c.isPublishing()) ;
  }
Example #3
0
  @Test
  public void indexBugTest() throws InterruptedException {
    int numOfItems = 20;

    Environment.initializeIfEmpty();

    // this line causes an java.lang.ArrayIndexOutOfBoundsException unless there is a break point in
    // ZipAction.createSubscriber()
    RingBufferProcessor<String> ring = RingBufferProcessor.create("test", 1024);

    // this line works
    //        Broadcaster<String> ring = Broadcaster.create(Environment.get());

    Stream<String> stream = Streams.wrap(ring);

    Stream<String> stream2 =
        stream
            .zipWith(
                Streams.createWith(
                    (d, s) -> {
                      for (int i = 0; i < d; i++) {
                        s.onNext(System.currentTimeMillis());
                      }
                    }),
                t -> String.format("%s : %s", t.getT2(), t.getT1()))
            .observeError(
                Throwable.class,
                (o, t) -> {
                  System.err.println(t.toString());
                  t.printStackTrace();
                });

    Promise<List<String>> p = stream2.observe(System.out::println).buffer(numOfItems).next();

    for (int curr = 0; curr < numOfItems; curr++) {
      if (curr % 5 == 0 && curr % 3 == 0) ring.onNext("FizBuz");
      else if (curr % 3 == 0) ring.onNext("Fiz");
      else if (curr % 5 == 0) ring.onNext("Buz");
      else ring.onNext(String.valueOf(curr));
    }

    p.await();
  }
Example #4
0
  public void testStreams_withTenPoints_createsStreamWithTenPoints() throws Exception {
    MatrixCursor fakeCursor = new MatrixCursor(COLUMNS);
    for (int i = 0; i < 10; i++) {
      fakeCursor.addRow(new Object[] {"", 0});
    }
    when(fakeContentProviderClient.query(
            StreamContract.Streams.CONTENT_URI,
            new String[] {
              "distinct " + StreamContract.Streams.STREAM_ID, StreamContract.Streams.STREAM_VERSION
            },
            "",
            new String[] {},
            null))
        .thenReturn(fakeCursor);

    Streams s = new Streams(fakeContentProviderClient);

    assertEquals(10, s.size());
  }
 public final T read(JsonReader paramJsonReader)
 {
   if (b == null) {
     return (T)a().read(paramJsonReader);
   }
   paramJsonReader = Streams.parse(paramJsonReader);
   if (paramJsonReader.isJsonNull()) {
     return null;
   }
   return (T)b.deserialize(paramJsonReader, d.getType(), c.a);
 }
 public final void write(JsonWriter paramJsonWriter, T paramT)
 {
   if (a == null)
   {
     a().write(paramJsonWriter, paramT);
     return;
   }
   if (paramT == null)
   {
     paramJsonWriter.nullValue();
     return;
   }
   Streams.write(a.serialize(paramT, d.getType(), c.b), paramJsonWriter);
 }
  /**
   * Returns the next available {@link JsonElement} on the reader. Null if none available.
   *
   * @return the next available {@link JsonElement} on the reader. Null if none available.
   * @throws JsonParseException if the incoming stream is malformed JSON.
   * @since 1.4
   */
  public JsonElement next() throws JsonParseException {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }

    try {
      return Streams.parse(parser);
    } catch (StackOverflowError e) {
      throw new JsonParseException("Failed parsing JSON source to Json", e);
    } catch (OutOfMemoryError e) {
      throw new JsonParseException("Failed parsing JSON source to Json", e);
    } catch (JsonParseException e) {
      throw e.getCause() instanceof EOFException ? new NoSuchElementException() : e;
    }
  }
Example #8
0
 /** @see File#cp(InputStream, OutputStream) */
 public static void cp(final InputStream in, final OutputStream out) throws IOException {
   try {
     try {
       Streams.cat(in, out);
     } finally {
       out.close();
     }
   } finally {
     try {
       in.close();
     } catch (IOException ex) {
       throw new InputIOException(ex);
     }
   }
 }
 public static void registerStream() {
   Streams.registerStream(STREAM, STREAM_TYPE);
 }
Example #10
0
 /**
  * Reads <code>body-data</code> from the current <code>encapsulation</code> and writes its
  * contents into the output <code>Stream</code>.
  *
  * <p>Arbitrary large amounts of data can be processed by this method using a constant size
  * buffer.
  *
  * @param output The <code>Stream</code> to write data into. May be null, in which case this
  *     method is equivalent to {@link #discardBodyData()}.
  * @return the amount of data written.
  * @throws MalformedStreamException if the stream ends unexpectedly.
  * @throws IOException if an i/o error occurs.
  */
 public int readBodyData(OutputStream output) throws MalformedStreamException, IOException {
   final InputStream istream = newInputStream();
   return (int) Streams.copy(istream, output, false);
 }
  public void writeLandingPage(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    String landingPage = getNewTokenLandingPage();

    /** default to current page * */
    if (landingPage == null) {
      StringBuilder sb = new StringBuilder();

      sb.append(request.getContextPath());
      sb.append(request.getServletPath());

      landingPage = sb.toString();
    }

    /** create auto posting form * */
    StringBuilder sb = new StringBuilder();

    sb.append("<html>\r\n");
    sb.append("<head>\r\n");
    sb.append("<title>OWASP CSRFGuard Project - New Token Landing Page</title>\r\n");
    sb.append("</head>\r\n");
    sb.append("<body>\r\n");
    sb.append("<script type=\"text/javascript\">\r\n");
    sb.append("var form = document.createElement(\"form\");\r\n");
    sb.append("form.setAttribute(\"method\", \"post\");\r\n");
    sb.append("form.setAttribute(\"action\", \"");
    sb.append(landingPage);
    sb.append("\");\r\n");

    /** only include token if needed * */
    if (isProtectedPage(landingPage)) {
      sb.append("var hiddenField = document.createElement(\"input\");\r\n");
      sb.append("hiddenField.setAttribute(\"type\", \"hidden\");\r\n");
      sb.append("hiddenField.setAttribute(\"name\", \"");
      sb.append(getTokenName());
      sb.append("\");\r\n");
      sb.append("hiddenField.setAttribute(\"value\", \"");
      sb.append(getTokenValue(request, landingPage));
      sb.append("\");\r\n");
      sb.append("form.appendChild(hiddenField);\r\n");
    }

    sb.append("document.body.appendChild(form);\r\n");
    sb.append("form.submit();\r\n");
    sb.append("</script>\r\n");
    sb.append("</body>\r\n");
    sb.append("</html>\r\n");

    String code = sb.toString();

    /** setup headers * */
    response.setContentType("text/html");
    response.setContentLength(code.length());

    /** write auto posting form * */
    OutputStream output = null;
    PrintWriter writer = null;

    try {
      output = response.getOutputStream();
      writer = new PrintWriter(output);

      writer.write(code);
      writer.flush();
    } finally {
      Writers.close(writer);
      Streams.close(output);
    }
  }
 /**
  * Read all the data from the resource into memory.
  *
  * @param reader The source reader to load the data from
  * @throws IOException
  */
 public FullyBufferedReader(final Reader reader) throws IOException {
   this(Streams.readString(reader));
 }
Example #13
0
  /**
   * Copies a source file to a destination file, optionally preserving the source's last
   * modification time. We already have an input stream to read the source file and the destination
   * appears to be an entry in an archive file. Note that this method <em>never</em> closes the
   * given input stream!
   *
   * <p>Note that this method synchronizes on the class object in order to prevent dead locks by two
   * threads copying archive entries to the other's source archive concurrently!
   *
   * @throws FalsePositiveException If the destination is a false positive and the exception cannot
   *     get resolved within this method.
   * @throws InputIOException If copying the data fails because of an IOException in the source.
   * @throws IOException If copying the data fails because of an IOException in the destination.
   */
  static final void cp0(
      final boolean preserve,
      final java.io.File src,
      final InputStream in,
      final ArchiveController dstController,
      final String dstEntryName)
      throws IOException {
    // Do not assume anything about the lock status of the controller:
    // This method may be called from a subclass while a lock is acquired!
    // assert !dstController.readLock().isLocked();
    // assert !dstController.writeLock().isLocked();

    try {
      class OStreamCreator implements IORunnable {
        OutputStream out; // = null;

        public void run() throws IOException {
          // Update controller.
          // This may invalidate the file system object, so it must be
          // done first in case srcController and dstController are the
          // same!
          dstController.autoUmount(dstEntryName);

          final boolean lenient = File.isLenient();

          // Get source archive entry.
          final ArchiveEntry srcEntry = new RfsEntry(src);

          // Get destination archive entry.
          final ArchiveFileSystem dstFileSystem = dstController.autoMount(lenient);
          final Delta delta = dstFileSystem.link(dstEntryName, lenient, preserve ? srcEntry : null);
          final ArchiveEntry dstEntry = delta.getEntry();

          // Create output stream.
          out = dstController.createOutputStream(dstEntry, srcEntry);

          // Now link the destination entry into the file system.
          delta.commit();
        }
      }

      // Create the output stream while the destination controller is
      // write locked.
      final OStreamCreator stream = new OStreamCreator();
      dstController.runWriteLocked(stream);
      final OutputStream out = stream.out;

      // Finally copy the entry data.
      try {
        Streams.cat(in, out);
      } finally {
        out.close();
      }
    } catch (ArchiveEntryFalsePositiveException ex) {
      assert dstController == ex.getController();
      // Reroute call to the destination's enclosing ArchiveController.
      cp0(
          preserve,
          src,
          in,
          dstController.getEnclController(),
          dstController.enclEntryName(dstEntryName));
    }
  }