Ejemplo n.º 1
0
  public static long stream(InputStream input, OutputStream output) throws IOException {
    ReadableByteChannel inputChannel = null;
    WritableByteChannel outputChannel = null;

    try {
      inputChannel = Channels.newChannel(input);
      outputChannel = Channels.newChannel(output);
      ByteBuffer buffer = ByteBuffer.allocate(10240);
      long size = 0;

      while (inputChannel.read(buffer) != -1) {
        buffer.flip();
        size += outputChannel.write(buffer);
        buffer.clear();
      }

      return size;
    } finally {
      if (outputChannel != null)
        try {
          outputChannel.close();
        } catch (IOException ignore) {
          /**/
        }
      if (inputChannel != null)
        try {
          inputChannel.close();
        } catch (IOException ignore) {
          /**/
        }
    }
  }
Ejemplo n.º 2
0
 public String copy(String basePath, ImageRef source, String targetDirName) {
   try {
     ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
     Resource res = source.eResource();
     URI uri = source.eResource().getURI();
     URI inPath = URI.createURI(source.getPath()).resolve(uri);
     URI outPath =
         URI.createURI(source.getPath())
             .resolve(URI.createFileURI(new File(targetDirName).getAbsolutePath()));
     ReadableByteChannel inChannel =
         Channels.newChannel(res.getResourceSet().getURIConverter().createInputStream(inPath));
     WritableByteChannel outChannel =
         Channels.newChannel(res.getResourceSet().getURIConverter().createOutputStream(outPath));
     while (inChannel.read(buffer) != -1) {
       buffer.flip();
       outChannel.write(buffer);
       buffer.compact();
     }
     buffer.flip();
     while (buffer.hasRemaining()) {
       outChannel.write(buffer);
     }
     outChannel.close();
     return outPath.toFileString();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     logger.error(e);
     return null;
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * net.jawr.web.resource.bundle.ResourceBundlesHandler#writeBundleTo(java
   * .lang.String, java.io.Writer)
   */
  @Override
  public void writeBundleTo(String bundlePath, Writer writer) throws ResourceNotFoundException {
    // String text = (String) textCache.get(bundlePath);
    String text = (String) cacheMgr.get(TEXT_CACHE_PREFIX + bundlePath);
    try {
      // If it's not cached yet
      if (null == text) {
        String charsetName = rsHandler.getConfig().getResourceCharset().name();
        ByteArrayOutputStream baOs = new ByteArrayOutputStream();
        WritableByteChannel wrChannel = Channels.newChannel(baOs);
        Writer tempWriter = Channels.newWriter(wrChannel, charsetName);
        rsHandler.writeBundleTo(bundlePath, tempWriter);
        text = baOs.toString(charsetName);
        cacheMgr.put(TEXT_CACHE_PREFIX + bundlePath, text);
      }

      // Write the text to the outputstream
      writer.write(text);
      writer.flush();

    } catch (IOException e) {
      throw new BundlingProcessException(
          "Unexpected IOException writing bundle[" + bundlePath + "]", e);
    }
  }
Ejemplo n.º 4
0
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    String fileName = null;
    long startTime, endTime;
    String fileContentToDisplay = req.getParameter("FileToDisplay");
    res.getWriter().println("File conetent to be displayed : " + fileContentToDisplay);
    startTime = System.currentTimeMillis();
    // Find if the file is in memcache. If present there is no need to go to blobstore to get the
    // data
    MemcacheService mService = MemcacheServiceFactory.getMemcacheService();
    AppEngineFile readableFile;
    readableFile = (AppEngineFile) mService.get(fileName);

    if (readableFile != null) {
      FileService fService = FileServiceFactory.getFileService();
      res.getWriter().println("File " + fileName + " is present in cache");
      FileReadChannel readChannel = fService.openReadChannel(readableFile, false);
      BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
      String line = null;
      res.getWriter().println("Contents of the file: ");

      while ((line = reader.readLine()) != null) {
        res.getWriter().println(line + "<br>");
      }
      readChannel.close();
    } else // Search for the file in blob
    {
      List<BlobInfo> blobToRead = new LinkedList<BlobInfo>();
      Iterator<BlobInfo> blobIterator = new BlobInfoFactory().queryBlobInfos();
      while (blobIterator.hasNext()) blobToRead.add(blobIterator.next());
      res.setContentType("text/plain");
      Boolean filePresent = false;
      int i;
      for (i = 0; i < blobToRead.size(); i++) {
        fileName = blobToRead.get(i).getFilename();
        if (fileName.equals(fileContentToDisplay)) {
          FileService fService = FileServiceFactory.getFileService();
          res.getWriter().println("File " + fileName + " is present in the blob store");

          readableFile = fService.getBlobFile(blobToRead.get(i).getBlobKey());

          FileReadChannel readChannel = fService.openReadChannel(readableFile, false);
          BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
          String line = null;
          res.getWriter().println("printing the contents : ");

          while ((line = reader.readLine()) != null) {

            res.getWriter().println(line);
          }

          readChannel.close();
        } else {
          res.getWriter().println("File " + fileName + " is not present in the blob store");
        }
      }
    }
    endTime = System.currentTimeMillis();
    res.getWriter().println("Time taken for the operation is " + (endTime - startTime) + " ms");
  }
  public static void main(String[] args) throws IOException {

    Movie video =
        MovieCreator.build(
            Channels.newChannel(
                Mp4WithAudioDelayExample.class.getResourceAsStream("/count-video.mp4")));
    Movie audio =
        MovieCreator.build(
            Channels.newChannel(
                Mp4WithAudioDelayExample.class.getResourceAsStream("/count-english-audio.mp4")));

    List<Track> videoTracks = video.getTracks();
    video.setTracks(new LinkedList<Track>());

    List<Track> audioTracks = audio.getTracks();

    for (Track videoTrack : videoTracks) {
      video.addTrack(new AppendTrack(videoTrack, videoTrack));
    }
    for (Track audioTrack : audioTracks) {
      audioTrack.getTrackMetaData().setStartTime(10.0);
      video.addTrack(audioTrack);
    }

    IsoFile out = new DefaultMp4Builder().build(video);
    FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4")));
    out.getBox(fos.getChannel());
    fos.close();
  }
Ejemplo n.º 6
0
  public static void patchFile(File originalFile, File deltaFile, File patchedFile)
      throws PortalException {

    FileInputStream originalFileInputStream = null;
    FileChannel originalFileChannel = null;
    FileOutputStream patchedFileOutputStream = null;
    WritableByteChannel patchedWritableByteChannel = null;
    ReadableByteChannel deltaReadableByteChannel = null;

    try {
      originalFileInputStream = new FileInputStream(originalFile);

      originalFileChannel = originalFileInputStream.getChannel();

      patchedFileOutputStream = new FileOutputStream(patchedFile);

      patchedWritableByteChannel = Channels.newChannel(patchedFileOutputStream);

      FileInputStream deltaInputStream = new FileInputStream(deltaFile);

      deltaReadableByteChannel = Channels.newChannel(deltaInputStream);

      ByteChannelReader deltaByteChannelReader = new ByteChannelReader(deltaReadableByteChannel);

      DeltaUtil.patch(originalFileChannel, patchedWritableByteChannel, deltaByteChannelReader);
    } catch (Exception e) {
      throw new PortalException(e);
    } finally {
      StreamUtil.cleanUp(originalFileInputStream);
      StreamUtil.cleanUp(originalFileChannel);
      StreamUtil.cleanUp(patchedFileOutputStream);
      StreamUtil.cleanUp(patchedWritableByteChannel);
      StreamUtil.cleanUp(deltaReadableByteChannel);
    }
  }
Ejemplo n.º 7
0
  public static void main(String[] argv) throws IOException {
    ReadableByteChannel source = Channels.newChannel(System.in);
    WritableByteChannel dest = Channels.newChannel(System.out);

    channelCopy2(source, dest);
    source.close();
    dest.close();
  }
Ejemplo n.º 8
0
  /** Call to initialize reading and writting network streams */
  private void initialize_channels_and_streams() throws IOException {
    // initialze the channels for reading and writting
    read_stream_ = socket_.getInputStream();
    write_stream_ = socket_.getOutputStream();

    read_channel_ = Channels.newChannel(read_stream_);
    write_channel_ = Channels.newChannel(write_stream_);
  }
Ejemplo n.º 9
0
  private int runTestsExternal(
      final CommandRunnerParams params,
      Build build,
      Iterable<String> command,
      Iterable<TestRule> testRules)
      throws InterruptedException, IOException {
    TestRunningOptions options = getTestRunningOptions(params);

    // Walk the test rules, collecting all the specs.
    List<ExternalTestRunnerTestSpec> specs = Lists.newArrayList();
    for (TestRule testRule : testRules) {
      if (!(testRule instanceof ExternalTestRunnerRule)) {
        params
            .getBuckEventBus()
            .post(
                ConsoleEvent.severe(
                    String.format(
                        "Test %s does not support external test running",
                        testRule.getBuildTarget())));
        return 1;
      }
      ExternalTestRunnerRule rule = (ExternalTestRunnerRule) testRule;
      specs.add(rule.getExternalTestRunnerSpec(build.getExecutionContext(), options));
    }

    // Serialize the specs to a file to pass into the test runner.
    Path infoFile =
        params
            .getCell()
            .getFilesystem()
            .resolve(BuckConstant.SCRATCH_PATH.resolve("external_runner_specs.json"));
    Files.createDirectories(infoFile.getParent());
    Files.deleteIfExists(infoFile);
    params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(infoFile.toFile(), specs);

    // Launch and run the external test runner, forwarding it's stdout/stderr to the console.
    // We wait for it to complete then returns its error code.
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams =
        ProcessExecutorParams.builder()
            .addAllCommand(command)
            .addAllCommand(withDashArguments)
            .addCommand("--buck-test-info", infoFile.toString())
            .setDirectory(params.getCell().getFilesystem().getRootPath().toFile())
            .build();
    ForwardingProcessListener processListener =
        new ForwardingProcessListener(
            Channels.newChannel(params.getConsole().getStdOut()),
            Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process =
        processExecutor.launchProcess(processExecutorParams, processListener);
    try {
      return processExecutor.waitForProcess(process, Long.MAX_VALUE, TimeUnit.DAYS);
    } finally {
      processExecutor.destroyProcess(process, /* force */ false);
      processExecutor.waitForProcess(process, Long.MAX_VALUE, TimeUnit.DAYS);
    }
  }
Ejemplo n.º 10
0
 /**
  * Constructor.
  *
  * @param socketChannel the SocketChannel underlying this SocketCommChannel
  * @param location the location for this channel
  * @param protocol the CommProtocol to use to send and receive messages
  * @throws java.io.IOException
  * @see CommProtocol
  * @see SocketChannel
  */
 public SocketCommChannel(SocketChannel socketChannel, URI location, CommProtocol protocol)
     throws IOException {
   super(location, protocol);
   this.socketChannel = socketChannel;
   socketChannel.socket().setSoLinger(true, SO_LINGER);
   this.istream =
       new PreBufferedInputStream(new BufferedInputStream(Channels.newInputStream(socketChannel)));
   this.ostream = new BufferedOutputStream(Channels.newOutputStream(socketChannel));
   setToBeClosed(false); // Socket connections are kept open by default
 }
Ejemplo n.º 11
0
  private static File writeToTempFile(final InputStream resourceStream)
      throws IOException, FileNotFoundException {
    final File tempFile = File.createTempFile("SchemaCrawler", ".dat");
    tempFile.deleteOnExit();

    try (final OutputStream tempFileStream = new FileOutputStream(tempFile); ) {
      fastChannelCopy(Channels.newChannel(resourceStream), Channels.newChannel(tempFileStream));
    }

    return tempFile;
  }
Ejemplo n.º 12
0
 public static void writeFile(File file, byte[] bytes) throws IOException {
   java.io.FileOutputStream f = new java.io.FileOutputStream(file);
   final WritableByteChannel outputChannel = Channels.newChannel(f);
   final ReadableByteChannel inputChannel = Channels.newChannel(new ByteArrayInputStream(bytes));
   try {
     fastChannelCopy(inputChannel, outputChannel);
     // closing the channels
   } finally {
     inputChannel.close();
     outputChannel.close();
   }
 }
Ejemplo n.º 13
0
  public static File convertRpmToZip(File file) throws Exception {
    LOG.info("File: " + file.getAbsolutePath());

    FileInputStream fis = new FileInputStream(file);
    ReadableChannelWrapper in = new ReadableChannelWrapper(Channels.newChannel(fis));

    InputStream uncompressed = new GZIPInputStream(fis);
    in = new ReadableChannelWrapper(Channels.newChannel(uncompressed));

    String rpmZipName = file.getName();
    rpmZipName = StringUtils.replace(rpmZipName, ".", "-");
    rpmZipName = rpmZipName + ".zip";
    String rpmZipPath = FilenameUtils.getFullPath(file.getAbsolutePath());

    File rpmZipOutput = new File(rpmZipPath + File.separator + rpmZipName);
    LOG.info("Converting RPM: " + file.getName() + " to ZIP: " + rpmZipOutput.getName());
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(rpmZipOutput));

    String rpmName = file.getName();
    rpmName = StringUtils.replace(rpmName, ".", "-");

    CpioHeader header;
    int total = 0;
    do {
      header = new CpioHeader();
      total = header.read(in, total);

      if (header.getFileSize() > 0) {
        BoundedInputStream bis = new BoundedInputStream(uncompressed, header.getFileSize());

        String relPath = FilenameUtils.separatorsToSystem(header.getName());
        relPath = StringUtils.removeStart(relPath, ".");
        relPath = StringUtils.removeStart(relPath, "/");
        relPath = rpmName + File.separator + relPath;
        relPath = StringUtils.replace(relPath, "\\", "/");

        ZipEntry zipEntry = new ZipEntry(relPath);
        zos.putNextEntry(zipEntry);
        IOUtils.copy(bis, zos);
      } else {
        final int skip = header.getFileSize();
        if (uncompressed.skip(skip) != skip) throw new RuntimeException("Skip failed.");
      }

      total += header.getFileSize();
    } while (!header.isLast());

    zos.flush();
    zos.close();

    return rpmZipOutput;
  }
Ejemplo n.º 14
0
 public static void copyStreams(final InputStream input, final OutputStream output)
     throws IOException {
   final ReadableByteChannel inputChannel = Channels.newChannel(input);
   final WritableByteChannel outputChannel = Channels.newChannel(output);
   try {
     // copy the channels
     fastChannelCopy(inputChannel, outputChannel);
     // closing the channels
   } finally {
     inputChannel.close();
     outputChannel.close();
   }
 }
Ejemplo n.º 15
0
  public AnalysisStatusUpdater(final String host, final int port) throws IOException {
    EvaluatorLoggingHandler.logger.info("Listening for server status on port " + port);
    this.socketConnection = SocketChannel.open(new InetSocketAddress(host, port));

    this.socketConnection.socket().setSoTimeout(250);

    this.out = new PrintWriter(Channels.newOutputStream(this.socketConnection));
    this.in =
        new BufferedReader(new InputStreamReader(Channels.newInputStream(this.socketConnection)));

    this.completedState = QueryConstants.failedComplete;
    this.finished = false;
  }
Ejemplo n.º 16
0
  public static void main(String[] args) throws IOException {

    FileInputStream fin = new FileInputStream(args[0]);
    GZIPInputStream gzin = new GZIPInputStream(fin);
    ReadableByteChannel in = Channels.newChannel(gzin);

    WritableByteChannel out = Channels.newChannel(System.out);
    ByteBuffer buffer = ByteBuffer.allocate(65536);
    while (in.read(buffer) != -1) {
      buffer.flip();
      out.write(buffer);
      buffer.clear();
    }
  }
  @FixFor("MODE-1358")
  @Test
  public void shouldCopyFilesUsingStreams() throws Exception {
    // Copy a large file into a temporary file ...
    File tempFile = File.createTempFile("copytest", "pdf");
    RandomAccessFile destinationRaf = null;
    RandomAccessFile originalRaf = null;
    try {
      URL sourceUrl = getClass().getResource("/docs/postgresql-8.4.1-US.pdf");
      assertThat(sourceUrl, is(notNullValue()));
      File sourceFile = new File(sourceUrl.toURI());
      assertThat(sourceFile.exists(), is(true));
      assertThat(sourceFile.canRead(), is(true));
      assertThat(sourceFile.isFile(), is(true));

      boolean useBufferedStream = true;
      final int bufferSize = AbstractBinaryStore.bestBufferSize(sourceFile.length());

      destinationRaf = new RandomAccessFile(tempFile, "rw");
      originalRaf = new RandomAccessFile(sourceFile, "r");

      FileChannel destinationChannel = destinationRaf.getChannel();
      OutputStream output = Channels.newOutputStream(destinationChannel);
      if (useBufferedStream) output = new BufferedOutputStream(output, bufferSize);

      // Create an input stream to the original file ...
      FileChannel originalChannel = originalRaf.getChannel();
      InputStream input = Channels.newInputStream(originalChannel);
      if (useBufferedStream) input = new BufferedInputStream(input, bufferSize);

      // Copy the content ...
      Stopwatch sw = new Stopwatch();
      sw.start();
      IoUtil.write(input, output, bufferSize);
      sw.stop();
      System.out.println(
          "Time to copy \""
              + sourceFile.getName()
              + "\" ("
              + sourceFile.length()
              + " bytes): "
              + sw.getTotalDuration());
    } finally {
      tempFile.delete();
      if (destinationRaf != null) destinationRaf.close();
      if (originalRaf != null) originalRaf.close();
    }
  }
  @org.junit.Test
  public void testMapper() throws Exception {

    final ArcFileReader reader = new ArcFileReader();

    Thread thread =
        new Thread(
            new Runnable() {

              public void run() {
                try {

                  while (reader.hasMoreItems()) {
                    ArcFileItem item = new ArcFileItem();

                    reader.getNextItem(item);

                    map(new Text(item.getUri()), item, null, null);
                  }
                  LOG.info("NO MORE ITEMS... BYE");
                } catch (IOException e) {
                  LOG.error(StringUtils.stringifyException(e));
                }
              }
            });

    // run the thread ...
    thread.start();

    File file = new File("/Users/rana/Downloads/1213886083018_0.arc.gz");
    ReadableByteChannel channel = Channels.newChannel(new FileInputStream(file));

    try {

      int totalBytesRead = 0;
      for (; ; ) {

        ByteBuffer buffer = ByteBuffer.allocate(ArcFileReader.DEFAULT_BLOCK_SIZE);

        int bytesRead = channel.read(buffer);
        LOG.info("Read " + bytesRead + " From File");

        if (bytesRead == -1) {
          reader.finished();
          break;
        } else {
          buffer.flip();
          totalBytesRead += buffer.remaining();
          reader.available(buffer);
        }
      }
    } finally {
      channel.close();
    }

    // now wait for thread to die ...
    LOG.info("Done Reading File.... Waiting for ArcFileThread to DIE");
    thread.join();
    LOG.info("Done Reading File.... ArcFileThread to DIED");
  }
Ejemplo n.º 19
0
 /**
  * transfer Stream
  *
  * @param ins
  * @param targetChannel
  */
 private static void transferStream(InputStream ins, FileChannel targetChannel) {
   ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10);
   ReadableByteChannel rbcInst = Channels.newChannel(ins);
   try {
     while (-1 != (rbcInst.read(byteBuffer))) {
       byteBuffer.flip();
       targetChannel.write(byteBuffer);
       byteBuffer.clear();
     }
   } catch (IOException ioe) {
     ioe.printStackTrace();
   } finally {
     if (null != rbcInst) {
       try {
         rbcInst.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     if (null != targetChannel) {
       try {
         targetChannel.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
Ejemplo n.º 20
0
  public void writeTo(Object obj, String mime, OutputStream os) throws IOException {

    if (obj instanceof InputStream) {
      InputStream is = (InputStream) obj;
      byte[] buffer = new byte[BUFFER_SIZE];
      int len;
      while ((len = is.read(buffer)) != -1) os.write(buffer, 0, len);
      os.flush();
    } else if (obj instanceof FileRegionDataSource) {
      FileRegionDataSource frds = (FileRegionDataSource) obj;
      WritableByteChannel wbc = Channels.newChannel(os);
      wbc.write(frds.getByteBuffer());
      os.flush();
    } else if (obj instanceof EmptyDataSource) {
      /*
      EmptyDataSource eds = (EmptyDataSource)obj;
      InputStream is = (InputStream) eds.getInputStream();

            byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = is.read(buffer)) != -1)
                os.write(buffer, 0, len);

            os.flush();
            */
    } else if (obj instanceof String) {
      byte[] bytes = ((String) obj).getBytes();
      os.write(bytes);
      os.flush();
    } else {
      throw new IOException("Unsupported class type - " + obj.getClass().getName());
    }
  }
Ejemplo n.º 21
0
 private static boolean unsafeDownloadToFile(String url, File output) throws IOException {
   output.createNewFile();
   FileOutputStream fos = new FileOutputStream(output);
   fos.getChannel().transferFrom(Channels.newChannel(requestStreamHttp(url)), 0, Long.MAX_VALUE);
   fos.close();
   return true;
 }
Ejemplo n.º 22
0
  @Test
  public void testEncryptDecryptFragmentedMp4() throws Exception {
    SecretKey sk =
        new SecretKeySpec(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "AES");
    Movie m =
        MovieCreator.build(
            CencTracksImplTest.class.getProtectionDomain().getCodeSource().getLocation().getFile()
                + "/com/googlecode/mp4parser/authoring/samples/1365070268951.mp4");

    List<Track> encTracks = new LinkedList<Track>();
    for (Track track : m.getTracks()) {
      encTracks.add(new CencEncryptingTrackImpl(track, UUID.randomUUID(), sk));
    }
    m.setTracks(encTracks);

    Mp4Builder mp4Builder = new FragmentedMp4Builder();
    Container c = mp4Builder.build(m);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    c.writeContainer(Channels.newChannel(baos));

    c.writeContainer(new FileOutputStream("output.mp4").getChannel());

    Movie m2 = MovieCreator.build(new MemoryDataSourceImpl(baos.toByteArray()));
    List<Track> decTracks = new LinkedList<Track>();
    for (Track track : m2.getTracks()) {
      decTracks.add(new CencDecryptingTrackImpl((CencEncyprtedTrack) track, sk));
    }
    m2.setTracks(decTracks);
    c = mp4Builder.build(m2);

    c.writeContainer(new FileOutputStream("output2.mp4").getChannel());
  }
Ejemplo n.º 23
0
 private void writeCompressed(JarOutputStream jarOut, String entryName) throws IOException {
   File file = entryMap.get(entryName);
   if ((file.exists()) && (file.isFile())) {
     JarEntry entry = new JarEntry(entryName);
     jarOut.putNextEntry(entry);
     WritableByteChannel outputChannel = null;
     FileChannel readFromFileChannel = null;
     FileInputStream inputStream = null;
     try {
       outputChannel = Channels.newChannel(jarOut);
       inputStream = new FileInputStream(file);
       readFromFileChannel = inputStream.getChannel();
       readFromFileChannel.transferTo(0, file.length(), outputChannel);
     } finally {
       try {
         if (jarOut != null) {
           jarOut.closeEntry();
         }
         if (readFromFileChannel != null) {
           readFromFileChannel.close();
         }
         if (inputStream != null) {
           inputStream.close();
         }
       } catch (IOException e) {
         AndmoreLogger.error("Could not close stream: ", e.getMessage()); // $NON-NLS-1$
       }
     }
   }
 }
Ejemplo n.º 24
0
  public static boolean downloadOCSSWInstaller() {

    if (isOcsswInstalScriptDownloadSuccessful()) {
      return ocsswInstalScriptDownloadSuccessful;
    }
    try {
      URL website = new URL(OCSSW_INSTALLER_URL);
      ReadableByteChannel rbc = Channels.newChannel(website.openStream());
      FileOutputStream fos = new FileOutputStream(TMP_OCSSW_INSTALLER);
      fos.getChannel().transferFrom(rbc, 0, 1 << 24);
      fos.close();
      (new File(TMP_OCSSW_INSTALLER)).setExecutable(true);
      ocsswInstalScriptDownloadSuccessful = true;
    } catch (MalformedURLException malformedURLException) {
      handleException("URL for downloading install_ocssw.py is not correct!");
    } catch (FileNotFoundException fileNotFoundException) {
      handleException(
          "ocssw installation script failed to download. \n"
              + "Please check network connection or 'seadas.ocssw.root' variable in the 'seadas.config' file. \n"
              + "possible cause of error: "
              + fileNotFoundException.getMessage());
    } catch (IOException ioe) {
      handleException(
          "ocssw installation script failed to download. \n"
              + "Please check network connection or 'seadas.ocssw.root' variable in the \"seadas.config\" file. \n"
              + "possible cause of error: "
              + ioe.getLocalizedMessage());
    } finally {
      return ocsswInstalScriptDownloadSuccessful;
    }
  }
Ejemplo n.º 25
0
 private static Channel prepareStdioChannel(Ruby runtime, STDIO stdio, Object stream) {
   if (runtime.getPosix().isNative() && stdio.isJVMDefault(stream) && !Platform.IS_WINDOWS) {
     // use real native channel for stdio
     return new NativeDeviceChannel(stdio.fileno());
   } else {
     switch (stdio) {
       case IN:
         return Channels.newChannel((InputStream) stream);
       case OUT:
       case ERR:
         return Channels.newChannel((OutputStream) stream);
       default:
         throw new RuntimeException("invalid stdio: " + stdio);
     }
   }
 }
  public static void downloadFile(String url, String outputName) throws IOException {

    URL website = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(outputName);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  }
  public static void write(HttpServletResponse response, File file) throws IOException {

    if (response instanceof ByteBufferServletResponse) {
      ByteBufferServletResponse byteBufferResponse = (ByteBufferServletResponse) response;

      ByteBuffer byteBuffer = ByteBuffer.wrap(FileUtil.getBytes(file));

      byteBufferResponse.setByteBuffer(byteBuffer);
    } else if (response instanceof StringServletResponse) {
      StringServletResponse stringResponse = (StringServletResponse) response;

      String s = FileUtil.read(file);

      stringResponse.setString(s);
    } else {
      FileInputStream fileInputStream = new FileInputStream(file);

      FileChannel fileChannel = fileInputStream.getChannel();

      try {
        int contentLength = (int) fileChannel.size();

        response.setContentLength(contentLength);

        response.flushBuffer();

        fileChannel.transferTo(0, contentLength, Channels.newChannel(response.getOutputStream()));
      } finally {
        fileChannel.close();
      }
    }
  }
Ejemplo n.º 28
0
 /** Caclulates an MD5 hash for each file in the archive. */
 public String[] getMD5s() throws FileNotFoundException, NoSuchAlgorithmException, IOException {
   /**
    * This could be more efficiently handled during the output phase using a filtering channel, but
    * would require placeholder values in the archive and some state. This is left for a later
    * refactoring.
    */
   final ByteBuffer buffer = ByteBuffer.allocate(4096);
   String[] array = new String[headers.size()];
   int x = 0;
   for (CpioHeader header : headers) {
     Object object = sources.get(header);
     String value = "";
     if (object instanceof File) {
       final ReadableChannelWrapper input =
           new ReadableChannelWrapper(new FileInputStream((File) object).getChannel());
       final Key<byte[]> key = input.start("MD5");
       while (input.read(buffer) != -1) buffer.rewind();
       value = new String(Util.hex(input.finish(key)));
       input.close();
     } else if (object instanceof URL) {
       final ReadableChannelWrapper input =
           new ReadableChannelWrapper(
               Channels.newChannel(((URL) object).openConnection().getInputStream()));
       final Key<byte[]> key = input.start("MD5");
       while (input.read(buffer) != -1) buffer.rewind();
       value = new String(Util.hex(input.finish(key)));
       input.close();
     }
     array[x++] = value;
   }
   return array;
 }
  @Before
  public void setUp() throws Exception {

    // Download file for test
    URL website = new URL("http://archive.org/web/images/logo_wayback_210x77.png");

    ReadableByteChannel resFile = Channels.newChannel(website.openStream());

    // Create file
    File file = new File(pathTempFile);
    FileOutputStream fos = new FileOutputStream(pathTempFile);
    fos.getChannel().transferFrom(resFile, 0, Long.MAX_VALUE);

    // Save to MultipartFile
    FileInputStream input = new FileInputStream(file);
    multipartFile =
        new MockMultipartFile("file", file.getName(), "image/png", IOUtils.toByteArray(input));

    fos.close();
    input.close();

    // Set paths
    attachmentService.setStoragePath(storagePath);
    attachmentService.setPathDefaultPreview(pathDefaultPreview);
  }
  @Test
  public void run3() throws IOException {
    final ByteArrayOutputStream expected = new ByteArrayOutputStream();
    final ByteArrayOutputStream result = new ByteArrayOutputStream();

    for (int k = 0; k < this.chunks; k++) {
      final byte[] data = new byte[10000 * (k + 1)];

      for (int j = 0; j < data.length; j++) {
        data[j] = (byte) j;
      }

      final Deflater def1 = new Deflater(9);
      final DeflaterOutputStream dos = new DeflaterOutputStream(expected, def1, 2048);

      dos.write(data);
      dos.finish();

      final Deflater def2 = new Deflater(9);
      final DeflaterChannelOutput dco =
          new DeflaterChannelOutput(Channels.newChannel(result), def2, 2048);

      dco.write(ByteBuffer.wrap(data));
      dco.flush();
    }

    Assert.assertArrayEquals(expected.toByteArray(), result.toByteArray());
  }