Package java.nio.channels

Examples of java.nio.channels.WritableByteChannel


  /**
   * @return
   */
  public static WritableByteChannel asWritableByteChannel(
      final ByteBuffer buffer) {
    return new WritableByteChannel() {

      private boolean open = true;

      public int write(ByteBuffer src) throws IOException {
        if (open == false) {
View Full Code Here


  ////////////////////////////////////////

  public static void writeFromFile(NetcdfFile fileIn, String fileOutName) throws IOException, InvalidRangeException {

    FileOutputStream stream = new FileOutputStream(fileOutName);
    WritableByteChannel channel = stream.getChannel();
    DataOutputStream dout = new DataOutputStream(Channels.newOutputStream(channel));

    N3channelWriter writer = new N3channelWriter(fileIn);
    int numrec = fileIn.getUnlimitedDimension() == null ? 0 : fileIn.getUnlimitedDimension().getLength();

    writer.writeHeader(dout, numrec);
    dout.flush();

    writer.writeDataAll(channel);
    channel.close();
  }
View Full Code Here


  // extract the msgno-th message to fileOut
  static void extractNthMessage(String filein, int msgno, String fileout) throws IOException {
    FileOutputStream fos = new FileOutputStream(fileout);
    WritableByteChannel wbc = fos.getChannel();

    RandomAccessFile raf = new RandomAccessFile(filein, "r");
    MessageScanner scan = new MessageScanner(raf);
    int count = 0;
    while (scan.hasNext()) {
      Message m = scan.next();
      if (msgno == count) {
        scan.writeCurrentMessage(wbc);
        wbc.close();
        raf.close();
        return;
      }
      count++;
    }
View Full Code Here

  }

  // extract the message matching listHash
  static void extractMessageByListhash(String filein, int want, String fileout) throws IOException {
    FileOutputStream fos = new FileOutputStream(fileout);
    WritableByteChannel wbc = fos.getChannel();

    int count = 0;
    RandomAccessFile raf = new RandomAccessFile(filein, "r");
    MessageScanner scan = new MessageScanner(raf);
    while (scan.hasNext()) {
      Message m = scan.next();
      int listHash = m.dds.getDataDescriptors().hashCode();

      if (listHash == want) {
        scan.writeCurrentMessage(wbc);
        wbc.close();
        raf.close();
        System.out.printf("output %d from %s %n",count, filein);
        return;
      }
      count++;
View Full Code Here

  }

  // extract n messages to fileOut
  static void extractNMessages(String filein, int n, String fileout) throws IOException {
    FileOutputStream fos = new FileOutputStream(fileout);
    WritableByteChannel wbc = fos.getChannel();

    RandomAccessFile raf = new RandomAccessFile(filein, "r");
    MessageScanner scan = new MessageScanner(raf);
    int count = 0;
    while (scan.hasNext() && (count < n)) {
      Message m = scan.next();
        scan.writeCurrentMessage(wbc);
      count++;
    }
    wbc.close();
    raf.close();
  }
View Full Code Here

  }

  // extract the first message that contains the header string to fileOut
  static void extractFirstMessageWithHeader(String filein, String header, String fileout) throws IOException {
    FileOutputStream fos = new FileOutputStream(fileout);
    WritableByteChannel wbc = fos.getChannel();

    RandomAccessFile raf = new RandomAccessFile(filein, "r");
    MessageScanner scan = new MessageScanner(raf);
    int count = 0;
    while (scan.hasNext()) {
      Message m = scan.next();
      if (m.getHeader().contains(header)) {
        scan.writeCurrentMessage(wbc);
        wbc.close();
        raf.close();
        return;
      }
      count++;
    }
View Full Code Here

      NetcdfFile ncfile = NetcdfFile.open(filename);
      NcStreamWriter writer = new NcStreamWriter(ncfile, null);

      File file = new File("C:/temp/out.ncs");
      FileOutputStream fos = new FileOutputStream(file);
      WritableByteChannel wbc = fos.getChannel();
      writer.streamAll( wbc);
      wbc.close();

      NetcdfFile ncfileBack = NetcdfFile.open(file.getPath());

      Formatter f = new Formatter();
      CompareNetcdf2 cn = new CompareNetcdf2(f, false, false, true);
View Full Code Here

  }

  public void writeToFile(String filename) throws IOException {
    File file = new File(filename);
    FileOutputStream fos = new FileOutputStream(file);
    WritableByteChannel wbc = fos.getChannel();

    long size = 4;
    fos.write(NcStream.MAGIC_START);

    // header
View Full Code Here

    {
        ByteBufferOutputStream out = null;
        ByteBuffer dataBuf = null;
        SerializeAdapter adapter = this._receiverInfo.getAdapterName() != null ? AdapterFactory
                .createAdapter( this._receiverInfo.getAdapterName() ) : null;
        WritableByteChannel channel = this._receiverInfo.getChannel();
        DataChannel dataChannel = DataChannel.getInstance( this._receiverInfo );

        try
        {
            if ( this._receiverInfo.hasAttachment() )
View Full Code Here

        String filename = (item.getDescription());
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);

        FileInputStream fis = new FileInputStream(f);
        FileChannel in = fis.getChannel();
        WritableByteChannel out = Channels.newChannel(response.getOutputStream());
        try {
          in.transferTo(0, in.size(), out);
          service.removeFile(id);
        } catch (Exception e) {
          throw e;
        } finally {
          in.close();
          // make sure servlet doesn't append anything unnecessary:
          out.close();
        }
      }
    } catch (FileNotFoundException e) {
      log.warn("Error retrieving file: " + e.getMessage());
      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
View Full Code Here

TOP

Related Classes of java.nio.channels.WritableByteChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.