Package java.io

Examples of java.io.DataOutputStream


      control_socket = new Socket( Proxy.NO_PROXY );
     
      control_socket.connect( new InetSocketAddress( socks_host, socks_port ));
   
      DataOutputStream   dos = new DataOutputStream( new BufferedOutputStream( control_socket.getOutputStream(), 256 ));
      DataInputStream   dis = new DataInputStream( control_socket.getInputStream());
           
      dos.writeByte( (byte)5 ); // socks 5
      dos.writeByte( (byte)2 ); // 2 methods
      dos.writeByte( (byte)0 ); // no auth
      dos.writeByte( (byte)2 ); // user/pw
     
      dos.flush();
     
        dis.readByte()// version byte
       
        byte method = dis.readByte();

        if ( method != 0 && method != 2 ){
         
            throw new IOException( "SOCKS 5: no valid method [" + method + "]" );
        }

          // auth
       
        if ( method == 2 ) {
           
          dos.writeByte( (byte)1 ); // user/pw version
          dos.writeByte( (byte)socks_user.length() ); // user length
          dos.write( socks_user.getBytes() );
          dos.writeByte( (byte)socks_password.length() ); // password length
          dos.write( socks_password.getBytes() );

          dos.flush();
         
          dis.readByte()// version byte
         
          byte status = dis.readByte();

            if ( status != 0 ){
             
              throw( new IOException( "SOCKS 5: authentication fails [status=" +status+ "]" ));
            }
        }
       
        String  mapped_ip;
       
        if ( target.isUnresolved() || target.getAddress() == null ){
         
            // deal with long "hostnames" that we get for, e.g., I2P destinations
         
            mapped_ip = AEProxyFactory.getAddressMapper().internalise( target.getHostName() );
           
        }else{
           
            mapped_ip = target.getAddress().getHostName();
        }
       
        dos.writeByte( (byte)5 ); // version
        dos.writeByte( (byte)3 ); // udp associate
        dos.writeByte( (byte)0 ); // reserved
       
        dos.writeByte((byte)1);
        dos.write( new byte[4] );
       
        dos.writeShort( (short)delegate.getPort()); // port
       
        dos.flush();
       
        dis.readByte()// ver
       
        byte reply = dis.readByte();
       
        if ( reply != 0 ){
         
            throw( new IOException( "SOCKS 5: udp association fails [reply=" +reply+ "]" ));
        }
       
        dis.readByte()// reserved
       
        InetAddress  relay_address;
       
        byte atype = dis.readByte();
       
        if ( atype == 1 ){
         
          byte[]  bytes = new byte[4];
         
          dis.readFully( bytes );
         
          relay_address = InetAddress.getByAddress( bytes );
         
        }else if ( atype == 3 ){
         
          byte  len = dis.readByte();
         
          byte[] bytes = new byte[(int)len&0xff ];
         
          dis.readFully( bytes );
         
          relay_address = InetAddress.getByName( new String( bytes ));
         
        }else{
         
          byte[]  bytes = new byte[16];
         
          dis.readFully( bytes );
         
          relay_address = InetAddress.getByAddress( bytes );

        }
       
        int  relay_port = ((dis.readByte()<<8)&0xff00) | (dis.readByte() & 0x00ff );
         
        if ( relay_address.isAnyLocalAddress()){
         
          relay_address = control_socket.getInetAddress();
        }
       
        relay = new InetSocketAddress( relay_address, relay_port );
                 
          // use the maped ip for dns resolution so we don't leak the
          // actual address if this is a secure one (e.g. I2P one)
       
        ByteArrayOutputStream  baos_temp     = new ByteArrayOutputStream();
        DataOutputStream    dos_temp  = new DataOutputStream( baos_temp );
 
        dos_temp.writeByte(0)// resv
        dos_temp.writeByte(0)// resv       
        dos_temp.writeByte(0)// frag (none)
 
        try {
          byte[] ip_bytes = HostNameToIPResolver.syncResolve( mapped_ip ).getAddress();
 
          dos_temp.writeByte( ip_bytes.length==4?(byte)1:(byte)4 );
          dos_temp.write( ip_bytes );
 
         
        }catch( Throwable e ){
                   
          dos_temp.writeByte( (byte)3 )// address type = domain name
          dos_temp.writeByte( (byte)mapped_ip.length() )// address type = domain name
          dos_temp.write( mapped_ip.getBytes() );
 
        }
 
        dos_temp.writeShort( (short)target.getPort() ); // port
 
        dos_temp.flush();
        packet_out_header = baos_temp.toByteArray();

             
      ok = true;
     
View Full Code Here


    boolean startAtHeader = true// otherwise skip to GDS
    boolean processGDS = true;
    Grib2ProductDefinitionSection pds = null;
    //DataOutputStream dos = new DataOutputStream(System.out);
    int count = -1;
    DataOutputStream dos = new DataOutputStream(
        new FileOutputStream( raf.getLocation() +".extract"));
    while (raf.getFilePointer() < raf.length()) {
      if (startAtHeader) {  // begining of record
        count++;
        if (!seekHeader(raf, raf.length())) {
          //System.out.println( "Scan seekHeader failed" );
          return false// not valid Grib file
        }

        // Read Section 0 Indicator Section
        is = new Grib2IndicatorSection(raf)// section 0
        //System.out.println( "Grib record length=" + is.getGribLength());
        // EOR (EndOfRecord) calculated so skipping data sections is faster
        EOR = raf.getFilePointer() + is.getGribLength()
            - is.getLength();
        SOR = raf.getFilePointer() - is.getLength();

        if (is.getGribEdition() == 1) {
          //System.out.println( "Error Grib 1 record in Grib2 file" ) ;
          raf.seek(EOR);
          continue;
        }
        if (discipline == -1 && forecast == -1 ) { // extract only 1st record
          raf.seek(SOR);
          byte[] oneRecord = new byte[(int) is.getGribLength()];
          raf.read(oneRecord);
          dos.write(oneRecord, 0, oneRecord.length);
          dos.flush();
          break;
        }

        // Read other SectionsGrib2
        id = new Grib2IdentificationSection(raf)// Section 1

      // end startAtHeader

      try { // catch all exceptions and seek to EOR

        if (processGDS) {
          // check for Local Use Section 2
          lus = new Grib2LocalUseSection(raf);
          // obtain GDS offset in the file for this record
          gdsOffset = raf.getFilePointer();
          // Section 3
          gds = new Grib2GridDefinitionSection(raf, true);
          //System.out.println( "GDS length=" + gds.getLength() );

        // end processGDS

        long refTime = id == null ? 0 : id.getRefTime();
        // obtain PDS offset in the file for this record
        pdsOffset = raf.getFilePointer();
        if ( gdsOffset == gOffset && pdsOffset == pOffset) {
          raf.seek(SOR);
          byte[] oneRecord = new byte[(int) is.getGribLength()];
          raf.read(oneRecord);
          dos.write(oneRecord, 0, oneRecord.length);
          dos.flush();
          break;
        }
        pds = new Grib2ProductDefinitionSection(raf, refTime)// Section 4
        Grib2Pds pdsv = pds.getPdsVars();
        //if (pdsv._getForecastTime() == forecast || (discipline == is.getDiscipline() &&
        if (pdsv._getForecastTime() == forecast && (discipline == is.getDiscipline() &&
            category == pdsv.getParameterCategory() &&
            number == pdsv.getParameterNumber())) {
          raf.seek(SOR);
          byte[] oneRecord = new byte[(int) is.getGribLength()];
          raf.read(oneRecord);
          dos.write(oneRecord, 0, oneRecord.length);
          dos.flush();
        }

        raf.seek(EOR);
      } catch (Exception e) {
        //System.out.println( "Caught Exception scannning record" );
        e.printStackTrace();
        return true;
      }
    }  // end raf.getFilePointer() < raf.length()
    dos.close();
    return true;

  // end scan
View Full Code Here

          if (pds.getLengthErr()) {
            raf.seek(EOR);
            continue;
          }
          String gridID = Integer.toString(pds.getGrid_ID());
          DataOutputStream dos = getOS(gridID);
          long size = EOR - start;//+1;
          byte[] data = new byte[(int) size];
          raf.seek(start);
          raf.read(data);
          dos.write(data, 0, data.length);
          dos.flush();
          raf.seek(EOR);

        } catch (Exception e) {
          //.println( "Caught Exception scannning record" );
          e.printStackTrace();
          raf.seek(EOR);
        }
      }  // end if seekHeader
    // end while raf.getFilePointer() < raf.length()
    // close all files.
    java.util.Set<String> ids = gridFiles.keySet();
    for (String id : ids) {
      DataOutputStream os = gridFiles.get(id);
      os.close();
    }
    return true;
  // end scan
View Full Code Here

   * @param gid Grid ID
   * @return DataOutputStream
   * @throws IOException _more_
   */
  private DataOutputStream getOS(String gid) throws IOException {
    DataOutputStream os = gridFiles.get(gid);
    if (os != null)
      return os;
    int idx = fileName.indexOf("20");
    String gFile = fileName.substring(0, idx) + "G" + gid + "_" + fileName.substring(idx);
    os = new DataOutputStream(new FileOutputStream(gFile));
    gridFiles.put(gid, os);
    return os;
  }
View Full Code Here

public class ObjectOutputStreamMicro implements I_ObjectStream {

   private DataOutputStream out;
  
   public ObjectOutputStreamMicro(OutputStream outStream) throws IOException {
      this.out = new DataOutputStream(outStream);
   }
View Full Code Here

         baos.write(qos.getBytes());
      baos.write(0);
      if (content != null && content.length > 0) baos.write(content);
      byte[] buf = baos.toByteArray();
      if (buf.length > 3) {
         DataOutputStream dos = new DataOutputStream(out);
         //dos.write(buf, 0, buf.length);
         dos.write(buf);
         dos.flush();
      }
      return buf.length;
   }
View Full Code Here

   * @throws java.io.IOException on write error
   */
  public static long copyToByteChannel(Array data, WritableByteChannel channel) throws java.io.IOException {

    if (data instanceof ArrayStructure) { // use NcStream encoding
      DataOutputStream os = new DataOutputStream( Channels.newOutputStream( channel));
      return NcStream.encodeArrayStructure((ArrayStructure) data, os);
    }

    DataOutputStream outStream = new DataOutputStream( Channels.newOutputStream( channel));
    IndexIterator iterA = data.getIndexIterator();
    Class classType = data.getElementType();

    if (classType == double.class) {
      while (iterA.hasNext())
        outStream.writeDouble(iterA.getDoubleNext());

    } else if (classType == float.class) {
      while (iterA.hasNext())
        outStream.writeFloat(iterA.getFloatNext());

    } else if (classType == long.class) {
      while (iterA.hasNext())
        outStream.writeLong(iterA.getLongNext());

    } else if (classType == int.class) {
      while (iterA.hasNext())
        outStream.writeInt(iterA.getIntNext());

    } else if (classType == short.class) {
      while (iterA.hasNext())
        outStream.writeShort(iterA.getShortNext());

    } else if (classType == char.class) {  // LOOK why are we using chars anyway ?
      byte[] pa = convertCharToByte((char[]) data.get1DJavaArray(char.class));
      outStream.write(pa, 0 , pa.length);

    } else if (classType == byte.class) {
      while (iterA.hasNext())
        outStream.writeByte(iterA.getByteNext());

    } else if (classType == boolean.class) {
      while (iterA.hasNext())
        outStream.writeBoolean(iterA.getBooleanNext());

    } else if (classType == String.class) {
      long size = 0;
      while (iterA.hasNext()) {
        String s = (String) iterA.getObjectNext();
        size += NcStream.writeVInt( outStream, s.length());
        byte[] b = s.getBytes("UTF-8");
        outStream.write(b);
        size += b.length;
      }
      return size;

    } else if (classType == ByteBuffer.class) { // OPAQUE
View Full Code Here

  static public long transferData(Array result, WritableByteChannel channel)
      throws java.io.IOException, ucar.ma2.InvalidRangeException {

    // LOOK should we buffer ??
    DataOutputStream outStream = new DataOutputStream( Channels.newOutputStream( channel));

    IndexIterator iterA = result.getIndexIterator();
    Class classType = result.getElementType();

    if (classType == double.class) {
      while (iterA.hasNext())
        outStream.writeDouble(iterA.getDoubleNext());

    } else if (classType == float.class) {
      while (iterA.hasNext())
        outStream.writeFloat(iterA.getFloatNext());

    } else if (classType == long.class) {
      while (iterA.hasNext())
        outStream.writeLong(iterA.getLongNext());

    } else if (classType == int.class) {
      while (iterA.hasNext())
        outStream.writeInt(iterA.getIntNext());

    } else if (classType == short.class) {
      while (iterA.hasNext())
        outStream.writeShort(iterA.getShortNext());

    } else if (classType == char.class) {
      while (iterA.hasNext())
        outStream.writeChar(iterA.getCharNext());

    } else if (classType == byte.class) {
      while (iterA.hasNext())
        outStream.writeByte(iterA.getByteNext());

    } else if (classType == boolean.class) {
      while (iterA.hasNext())
        outStream.writeBoolean(iterA.getBooleanNext());

    } else
      throw new UnsupportedOperationException("Class type = " + classType.getName());

    return 0;
View Full Code Here

  }

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

  public static void writeFromFile(NetcdfFile fileIn, String fileOutName) throws IOException {
    DataOutputStream stream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileOutName), 10 * 1000));
    N3outputStreamWriter writer = new N3outputStreamWriter(fileIn);
    int numrec = fileIn.getUnlimitedDimension() == null ? 0 : fileIn.getUnlimitedDimension().getLength();
    writer.writeHeader(stream, numrec);
    writer.writeDataAll(stream);
    stream.close();
  }
View Full Code Here

    // stores the number of times a particular GDS is used
    HashMap gdsCounter = new HashMap();
    Grib1ProductDefinitionSection pds = null;
    Grib1GridDefinitionSection gds = null;
    long gdsOffset = 0;
    DataOutputStream dos = new DataOutputStream(System.out);

    //System.out.println("file position =" + raf.getFilePointer());
    long SOR = raf.getFilePointer();
    int count = -1;
    while (raf.getFilePointer() < raf.length()) {
      SOR = raf.getFilePointer();
      if (seekHeader(raf, raf.length())) {
        count++;
        // Read Section 0 Indicator Section
        Grib1IndicatorSection is = new Grib1IndicatorSection(raf);
        //System.out.println( "Grib record length=" + is.getGribLength());
        // EOR (EndOfRecord) calculated so skipping data sections is faster
        long EOR = raf.getFilePointer() + is.getGribLength()
            - is.getLength();
        //long SOR = raf.getFilePointer() - is.getLength();

        // skip Grib 2 records in a Grib 1 file
        if (is.getGribEdition() == 2) {
          //System.out.println( "Error Grib 2 record in Grib1 file" ) ;
          raf.seek(EOR);
          continue;
        }

        if (parm[0] == -1) { // extract only 1st record
          raf.seek(SOR);
          byte[] oneRecord = new byte[(int) is.getGribLength()];
          raf.read(oneRecord);
          dos.write(oneRecord, 0, oneRecord.length);
          dos.flush();
          break;
        } else if( parm[1] == -1 ) {
          if( count == parm[0]) {
            raf.seek(SOR);
            byte[] oneRecord = new byte[(int) is.getGribLength()];
            raf.read(oneRecord);
            dos.write(oneRecord, 0, oneRecord.length);
            dos.flush();
            break;
          } else {
            raf.seek(EOR);
          }
          continue;
        }
        long dataOffset = 0;
        try { // catch all exceptions and seek to EOR

          // Read Section 1 Product Definition Section PDS
          pds = new Grib1ProductDefinitionSection(raf);
          if (pds.getLengthErr()) {
            raf.seek(EOR);
            continue;
          }
          Grib1Pds pdsv = pds.getPdsVars();
          if (getParameter && parm[0] == pdsv.getParameterNumber()) {
            raf.seek(SOR);
            byte[] oneRecord = new byte[(int) is.getGribLength()];
            raf.read(oneRecord);
            dos.write(oneRecord, 0, oneRecord.length);
            dos.flush();
          } else if ( parm[0] <= pdsv.getForecastTime() && parm[1] >= pdsv.getForecastTime()) {
            raf.seek(SOR);
            byte[] oneRecord = new byte[(int) is.getGribLength()];
            raf.read(oneRecord);
            dos.write(oneRecord, 0, oneRecord.length);
            dos.flush();
          }

          raf.seek(EOR);
          continue;

        } catch (Exception e) {
          //.println( "Caught Exception scannning record" );
          e.printStackTrace();
          raf.seek(EOR);
          continue;
        }
      }  // end if seekHeader
      //System.out.println( "raf.getFilePointer()=" + raf.getFilePointer());
      //System.out.println( "raf.length()=" + raf.length() );
    // end while raf.getFilePointer() < raf.length()
    //System.out.println("GribInput: processed in " +
    //   (System.currentTimeMillis()- start) + " milliseconds");
    dos.close();
    return true;
  // end scan
View Full Code Here

TOP

Related Classes of java.io.DataOutputStream

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.