Пример #1
0
  static int safeRead(ReadableByteChannel channel, ByteBuffer dst) throws IOException {
    int read = -1;
    try {
      // Read data from the Channel
      read = channel.read(dst);
    } catch (ClosedChannelException e) {

    } catch (IOException e) {
      switch ("" + e.getMessage()) {
        case "null":
        case "Connection reset by peer":
        case "Broken pipe":
          break;
        default:
          cntIoError++;
          System.out.println("error count IoError: " + cntIoError);
      }
      channel.close();
      e.printStackTrace();
    } catch (CancelledKeyException e) {
      cntCancelledKeyException++;
      System.out.println("error count CancelledKey: " + cntCancelledKeyException);
      channel.close();
    }
    return read;
  }
  @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");
  }
Пример #3
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();
       }
     }
   }
 }
Пример #4
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) {
          /**/
        }
    }
  }
Пример #5
0
 /**
  * Helper Method to Copy from one Byte Channel to another.
  *
  * @param from
  * @param to
  * @throws IOException
  */
 protected static void channelCopy(ReadableByteChannel from, WritableByteChannel to)
     throws IOException {
   ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
   try {
     // Read
     while (from.read(buffer) != -1) {
       buffer.flip();
       to.write(buffer);
       buffer.compact();
     }
     // Flip the Buffer
     buffer.flip();
     // Write
     while (buffer.hasRemaining()) {
       to.write(buffer);
     } // End of While Loop
   } finally {
     // Handle In Channel Closure
     if (from != null) {
       try {
         from.close();
       } catch (Exception ex) {
         // No handling required
       }
     }
     // Handle Out Channel Closure
     if (to != null) {
       try {
         to.close();
       } catch (Exception ex) {
         // No handling required
       }
     }
   } // End of Finally
 }
Пример #6
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();
  }
Пример #7
0
 @Override
 public void failed(Throwable x) {
   super.failed(x);
   _channel.getByteBufferPool().release(_buffer);
   try {
     _in.close();
   } catch (IOException e) {
     LOG.ignore(e);
   }
 }
 /** Dispose of this <code>InputStreamImageDataProvider</code> and its resources. */
 public synchronized void dispose() {
   if (m_channel != null) {
     final ReadableByteChannel temp = m_channel;
     m_channel = null;
     try {
       temp.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
Пример #9
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();
   }
 }
Пример #10
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();
   }
 }
 public static void downloadFileFromURL(String urlString, File destination) {
   try {
     URL website = new URL(urlString);
     ReadableByteChannel rbc;
     rbc = Channels.newChannel(website.openStream());
     FileOutputStream fos = new FileOutputStream(destination);
     fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
     fos.close();
     rbc.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Пример #12
0
 /**
  * Downloads from an url and store the image.
  *
  * @param idImage
  * @param url
  */
 public void storeImage(String idImage, String url) {
   try {
     URL website = new URL(url);
     Path pathToFile = Paths.get(getRoot());
     ReadableByteChannel rbc;
     rbc = Channels.newChannel(website.openStream());
     FileOutputStream fos = new FileOutputStream(pathToFile.resolve(idImage).toFile());
     fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
     fos.flush();
     fos.close();
     rbc.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  public byte[] getfromUrl(String urlName) throws MalformedURLException, IOException {
    int totalRead = 0;
    URL url = new URL(urlName);
    ReadableByteChannel rbc = Channels.newChannel(url.openStream());
    ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);

    while (rbc.read(buf) != -1) {}
    buf.flip();
    totalRead = buf.limit();
    LOG.debug("baixado: " + totalRead + " bytes de [" + urlName + "]");
    byte[] b = new byte[totalRead];
    buf.get(b, 0, totalRead);
    rbc.close();
    return b;
  }
Пример #14
0
  private void fetch(String res) throws IOException {
    URL url = new URL(Configuration.composeres() + res);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoInput(true);
    con.setConnectTimeout(5000);
    con.setRequestProperty(
        "User-Agent",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");

    ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
    FileOutputStream fos = new FileOutputStream(Configuration.STORAGE_DIR + File.separator + res);
    fos.getChannel().transferFrom(rbc, 0, 1 << 24);
    fos.close();
    rbc.close();
  }
Пример #15
0
  public static void copy(InputStream in, OutputStream out) throws IOException {
    ReadableByteChannel source = Channels.newChannel(in);
    WritableByteChannel target = Channels.newChannel(out);

    ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
    while (source.read(buffer) != -1) {
      buffer.flip(); // Prepare the buffer to be drained
      while (buffer.hasRemaining()) {
        target.write(buffer);
      }
      buffer.clear(); // Empty buffer to get ready for filling
    }

    source.close();
    target.close();
  }
    public boolean onData(INonBlockingConnection connection)
        throws IOException, BufferUnderflowException, MaxReadSizeExceededException {

      connection.readStringByDelimiter("\r\n");

      File file = QAUtil.createTestfile_400k();
      RandomAccessFile raf = new RandomAccessFile(file, "r");
      ReadableByteChannel in = raf.getChannel();
      connection.transferFrom(in);

      in.close();
      raf.close();
      file.delete();

      return true;
    }
Пример #17
0
 /**
  * Clean up any resources. Closes the channel.
  *
  * @throws IOException If errors occur while closing the channel.
  */
 public void close() throws IOException {
   // don't throw NPE on double close
   if (channel == null) return;
   try {
     if (channel.isOpen()) {
       channel.close();
       streamLogger.close();
     }
     NIOUtilities.clean(buffer, useMemoryMappedBuffer);
   } finally {
     if (shxReader != null) shxReader.close();
   }
   shxReader = null;
   channel = null;
   header = null;
 }
 /** Copy the content of sourceUri to the destination. */
 private Uri copyTo(final Uri sourceUri, String filename) throws IOException {
   Log.i(
       LOG_TAG,
       String.format("Copy a Uri to app local storage (%s -> %s)", sourceUri, filename));
   final Context context = ImportVCardActivity.this;
   final ContentResolver resolver = context.getContentResolver();
   ReadableByteChannel inputChannel = null;
   WritableByteChannel outputChannel = null;
   Uri destUri = null;
   try {
     inputChannel = Channels.newChannel(resolver.openInputStream(sourceUri));
     destUri = Uri.parse(context.getFileStreamPath(filename).toURI().toString());
     outputChannel = context.openFileOutput(filename, Context.MODE_PRIVATE).getChannel();
     final ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
     while (inputChannel.read(buffer) != -1) {
       if (mCanceled) {
         Log.d(LOG_TAG, "Canceled during caching " + sourceUri);
         return null;
       }
       buffer.flip();
       outputChannel.write(buffer);
       buffer.compact();
     }
     buffer.flip();
     while (buffer.hasRemaining()) {
       outputChannel.write(buffer);
     }
   } finally {
     if (inputChannel != null) {
       try {
         inputChannel.close();
       } catch (IOException e) {
         Log.w(LOG_TAG, "Failed to close inputChannel.");
       }
     }
     if (outputChannel != null) {
       try {
         outputChannel.close();
       } catch (IOException e) {
         Log.w(LOG_TAG, "Failed to close outputChannel");
       }
     }
   }
   return destUri;
 }
Пример #19
0
 /** Gets String contents from channel and closes it. */
 public static String getStringContents(ReadableByteChannel channel) throws IOException {
   // TODO Checks if a supplier would be nice
   try {
     ByteBuffer buffer = ByteBuffer.allocate(1024 * 8);
     StringBuilder sb = new StringBuilder();
     int bytesRead = channel.read(buffer);
     while (bytesRead != -1) {
       buffer.flip();
       CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer);
       sb.append(charBuffer.toString());
       buffer.clear();
       bytesRead = channel.read(buffer);
     }
     return sb.toString();
   } finally {
     channel.close();
   }
 }
Пример #20
0
 public static byte[] getBytes(InputStream in) {
   ByteArrayOutputStream os = new ByteArrayOutputStream();
   WritableByteChannel wbc = Channels.newChannel(os);
   ReadableByteChannel rbc = Channels.newChannel(in);
   ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
   try {
     while (rbc.read(byteBuffer) != -1) {
       byteBuffer.flip();
       wbc.write(byteBuffer);
       byteBuffer.clear();
     }
     wbc.close();
     rbc.close();
     return os.toByteArray();
   } catch (IOException e) {
     throw new HerokuAPIException("IOException while reading response", e);
   }
 }
Пример #21
0
  /**
   * Create a POIFSFileSystem from an <tt>InputStream</tt>. Normally the stream is read until EOF.
   * The stream is always closed.
   *
   * <p>Some streams are usable after reaching EOF (typically those that return <code>true</code>
   * for <tt>markSupported()</tt>). In the unlikely case that the caller has such a stream
   * <i>and</i> needs to use it after this constructor completes, a work around is to wrap the
   * stream in order to trap the <tt>close()</tt> call. A convenience method (
   * <tt>createNonClosingInputStream()</tt>) has been provided for this purpose:
   *
   * <pre>
   * InputStream wrappedStream = POIFSFileSystem.createNonClosingInputStream(is);
   * HSSFWorkbook wb = new HSSFWorkbook(wrappedStream);
   * is.reset();
   * doSomethingElse(is);
   * </pre>
   *
   * Note also the special case of <tt>ByteArrayInputStream</tt> for which the <tt>close()</tt>
   * method does nothing.
   *
   * <pre>
   * ByteArrayInputStream bais = ...
   * HSSFWorkbook wb = new HSSFWorkbook(bais); // calls bais.close() !
   * bais.reset(); // no problem
   * doSomethingElse(bais);
   * </pre>
   *
   * @param stream the InputStream from which to read the data
   * @exception IOException on errors reading, or on invalid data
   */
  public NPOIFSFileSystem(InputStream stream) throws IOException {
    this(false);

    ReadableByteChannel channel = null;
    boolean success = false;

    try {
      // Turn our InputStream into something NIO based
      channel = Channels.newChannel(stream);

      // Get the header
      ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE);
      IOUtils.readFully(channel, headerBuffer);

      // Have the header processed
      _header = new HeaderBlock(headerBuffer);

      // Sanity check the block count
      BlockAllocationTableReader.sanityCheckBlockCount(_header.getBATCount());

      // We need to buffer the whole file into memory when
      //  working with an InputStream.
      // The max possible size is when each BAT block entry is used
      int maxSize = BATBlock.calculateMaximumSize(_header);
      ByteBuffer data = ByteBuffer.allocate(maxSize);
      // Copy in the header
      headerBuffer.position(0);
      data.put(headerBuffer);
      data.position(headerBuffer.capacity());
      // Now read the rest of the stream
      IOUtils.readFully(channel, data);
      success = true;

      // Turn it into a DataSource
      _data = new ByteArrayBackedDataSource(data.array(), data.position());
    } finally {
      // As per the constructor contract, always close the stream
      if (channel != null) channel.close();
      closeInputStream(stream, success);
    }

    // Now process the various entries
    readCoreContents();
  }
Пример #22
0
  // Load a TGA file
  public static void loadTGA(Texture texture, String filename) throws IOException {
    ByteBuffer header = BufferUtil.newByteBuffer(12);
    ReadableByteChannel in = Channels.newChannel(ResourceRetriever.getResourceAsStream(filename));
    readBuffer(in, header);

    // See if header matches the predefined header of
    if (uTGAcompare.equals(header)) { // an Uncompressed TGA image
      // If so, jump to Uncompressed TGA loading code
      loadUncompressedTGA(texture, in);
    } else if (cTGAcompare.equals(header)) {
      // See if header matches the predefined header of
      // an RLE compressed TGA image
      // If so, jump to Compressed TGA loading code
      loadCompressedTGA(texture, in);
    } else // If header matches neither type
    {
      in.close();
      // Display an error
      throw new IOException("TGA file be type 2 or type 10 ");
    }
  }
Пример #23
0
    @Override
    protected boolean process() throws Exception {
      // Only return if EOF has previously been read and thus
      // a write done with EOF=true
      if (_eof) {
        _in.close();
        closed();
        _channel.getByteBufferPool().release(_buffer);
        return true;
      }

      // Read from stream until buffer full or EOF
      _buffer.clear();
      while (_buffer.hasRemaining() && !_eof) _eof = (_in.read(_buffer)) < 0;

      // write what we have
      _buffer.flip();
      _channel.write(_buffer, _eof, this);

      return false;
    }
  private File prepareSessionsXmlFile(ZipFile zipFile, ZipEntry entry) throws Exception {
    // Create the Channel from the ZipEntry
    InputStream inputStream = zipFile.getInputStream(entry);
    ReadableByteChannel sourceChannel = Channels.newChannel(inputStream);

    // Create the Channel for the destination
    String fileName = entry.getName().replace('/', '_');
    File file = File.createTempFile(fileName, null);
    FileOutputStream fos = new FileOutputStream(file);
    FileChannel destinationChannel = fos.getChannel();

    // Copy the information
    try {
      destinationChannel.transferFrom(sourceChannel, 0, entry.getSize());
    } finally {
      sourceChannel.close();
      destinationChannel.close();
    }

    return file;
  }
Пример #25
0
    @Override
    protected Action process() throws Exception {
      // Only return if EOF has previously been read and thus
      // a write done with EOF=true
      if (_eof) {
        if (LOG.isDebugEnabled()) LOG.debug("EOF of {}", this);
        _in.close();
        closed();
        _channel.getByteBufferPool().release(_buffer);
        return Action.SUCCEEDED;
      }

      // Read from stream until buffer full or EOF
      _buffer.clear();
      while (_buffer.hasRemaining() && !_eof) _eof = (_in.read(_buffer)) < 0;

      // write what we have
      _buffer.flip();
      write(_buffer, _eof, this);

      return Action.SCHEDULED;
    }
Пример #26
0
  public static void downloadFromUrl(String urlStr, String file) throws Exception {

    System.out.println(urlStr + " " + file);
    try {
      URL url = new URL(urlStr);
      ReadableByteChannel rbc = Channels.newChannel(url.openStream());
      FileOutputStream fos = new FileOutputStream(file);
      fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
      fos.close();
      rbc.close();
    } catch (MalformedURLException e) {
      e.printStackTrace();
      throw e;
    } catch (IOException e) {
      e.printStackTrace();
      throw e;
    } catch (Exception e) {
      throw e;
    }

    System.out.println("download done...");
  }
Пример #27
0
 @Override
 public void run() {
   try {
     if (!picDirectory.exists()) picDirectory.mkdirs();
     File csvPath = csvFile.getParentFile();
     if ((csvPath != null) && !csvPath.exists()) csvPath.mkdirs();
     PrintWriter csvWrite = new PrintWriter(csvFile);
     for (int i = 0; i < id.length; i++) {
       progressBar.setValue(i * 100 / id.length);
       csvWrite.print("'" + id[i] + "'");
       Detail info = new Detail(frame.database, id[i]);
       for (int x = 0; x < 7; x++)
         for (int y = 0; y < 7; y++) {
           String data = info.get(List.COLUMN_NAME[x][y]);
           switch (List.COLUMN_TYPE[x][y]) {
             case 1:
               data = "'" + data + "'";
               break;
             case 2:
               if (data == null) data = "null";
               break;
             case 3:
               if (data == null) data = "0000-00-00";
               data = "'" + data + "'";
               break;
             case 4:
               data = "'" + data + "'";
           }
           csvWrite.print("," + data);
         }
       csvWrite.println();
       String picAddress = info.get("pic");
       info.close();
       if (picAddress.length() == 32) {
         ReadableByteChannel url =
             Channels.newChannel(
                 new URL(
                         "http://"
                             + Configure.webserverAddress
                             + "/"
                             + Configure.picDirectory
                             + picAddress.substring(0, picAddress.length() - 5)
                             + "/"
                             + picAddress.substring(picAddress.length() - 5)
                             + ".jpg")
                     .openStream());
         FileOutputStream outStream =
             new FileOutputStream(picDirectory.getPath() + "/" + id[i] + ".jpg");
         FileChannel out = outStream.getChannel();
         ByteBuffer buffer = ByteBuffer.allocate(10000);
         while (url.read(buffer) != -1) {
           buffer.flip();
           out.write(buffer);
           buffer.clear();
         }
         out.close();
         outStream.close();
         url.close();
       }
     }
     csvWrite.close();
     progressBar.setValue(100);
     finish();
   } catch (Exception e) {
     JOptionPane.showMessageDialog(Port.this, "导出失败!", "错误", JOptionPane.ERROR_MESSAGE);
     dispose();
   }
 }
Пример #28
0
 @Override
 public void close() throws IOException {
   out.close();
   in.close();
 }
 @After
 public void closeChannel() throws IOException {
   bz2Channel.close();
   bz2Channel = null;
 }
Пример #30
0
  private Status sendFile() throws IOException, ConexionException, ClassNotFoundException {
    ControlMsg msg = new ControlMsg(Command.S);
    Status status = new Status();
    File file = new File(this.workDir, this.filename);
    String md5sum = getMD5Checksum(file);
    long filesize = file.length();
    StatusValue stat;

    /*
    this.pantalla.println("Filename: "+filename);
    this.pantalla.println("Tamaño: "+filesize);
    this.pantalla.println("md5sum: "+md5sum);
    */

    msg.setFilename(filename);
    msg.setFileHash(md5sum);
    msg.setFileSize(filesize);
    msg.setStatus(StatusValue.HELLO);

    // Enviamos el paquete de control
    if (sendMsg(server, buffer, msg) == -1) {
      System.out.println("Error " + msg);
    }

    // Recibimos la respuesta
    msg = (ControlMsg) recieveMsg(server, buffer);

    stat = msg.getStatus();
    switch (stat) {
      case START:
        // Comenzamos a enviar el fichero
        System.out.println("Enviando fichero " + this.filename + " ... ");
        final ReadableByteChannel src = Channels.newChannel(new FileInputStream(file));
        buffer.clear();
        int n;
        while ((n = src.read(buffer)) != -1) {
          // while(src.read(buffer) != -1) {
          System.out.println("Leidos " + n + " desde " + file);
          buffer.flip();
          n = dataChan.write(buffer);
          // dataChan.write(buffer);
          System.out.println("Escritos " + n + " en " + getId(dataChan));
          buffer.compact();
          try {
            Thread.sleep(50);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        buffer.flip();
        while (buffer.hasRemaining()) {
          System.out.println("He entrado en el hasRemaining.");
          dataChan.write(buffer);
        }
        src.close();
        break;
      case ERROR:
      default:
        return new Status(stat, msg.getResponse());
    }

    // Vemos la respuesta del MD5
    msg = (ControlMsg) recieveMsg(server, buffer);

    stat = msg.getStatus();
    switch (stat) {
      case MD5OK:
        // this.pantalla.println("DONE");
        status = new Status(OK, "Subida completada");
        break;
      default:
        status = new Status(stat, msg.getResponse());
    }

    return status;
  }