Package java.io

Examples of java.io.BufferedInputStream


   //--------------------------------------
   private ByteArrayOutputStream loadInputStream(InputStream input) throws Exception
   {
      ByteArrayOutputStream output = new ByteArrayOutputStream();

      BufferedInputStream buffer = new BufferedInputStream(input);
      byte[] bytes = new byte[buffer.available()];
      int available = -1;
      while ((available = buffer.read(bytes)) > -1)
      {
         output.write(bytes, 0, available);
      }
      return output;
   }
View Full Code Here


            System.out.println("CHARSET = " + charset);
         }
      });

      URL url = new URL(argv[0]);
      BufferedInputStream imp = new BufferedInputStream(url.openStream());

      byte[] buf = new byte[1024];
      int len;
      boolean done = false;
      boolean isAscii = true;

      while ((len = imp.read(buf, 0, buf.length)) != -1)
      {
         //Check if the stream is only ascii.
         if (isAscii)
            isAscii = det.isAscii(buf, len);
View Full Code Here

    public static void write(byte[] data, String fileName) throws IOException {
        ByteArrayInputStream bais = null;
        InputStream is = null;
        try {
            bais = new ByteArrayInputStream(data);
            is = new BufferedInputStream(bais);
   
            write(is, fileName)
        } finally {
            if (is != null) {
                is.close();
View Full Code Here

   public static void write(byte[] data, File file) throws IOException {
         ByteArrayInputStream bais = null;
         InputStream is = null;
         try {
         bais = new ByteArrayInputStream(data);
         is = new BufferedInputStream(bais);
   
         write(is, file)
         } finally {
             if (is != null) {
                 is.close();
View Full Code Here

    /**
     * Get a <code>BufferedInputStream</code>.
     */
    public final static BufferedInputStream getBufferedInputStream(
            InputStream in) {
        BufferedInputStream bin = null;
        if (in instanceof java.io.BufferedInputStream) {
            bin = (BufferedInputStream) in;
        } else {
            bin = new BufferedInputStream(in);
        }
        return bin;
    }
View Full Code Here

 
  private void loadList() {   
    File fTrackers = FileUtil.getUserFile("trackers.config");
    if(fTrackers.exists() && fTrackers.isFile()) {
      FileInputStream fin = null;
      BufferedInputStream bin = null;
      try {
        fin = new FileInputStream(fTrackers);
        bin = new BufferedInputStream(fin, 8192);
        Map map = BDecoder.decode(bin);
        List list = (List) map.get("trackers");
        if(list != null) {
          Iterator iter = list.iterator();
          while(iter.hasNext()) {
            String tracker =  new String((byte[])iter.next());
            trackers.add(tracker);
          }
        }
        Map mapMT = (Map) map.get("multi-trackers");
        if(mapMT != null) {
          Iterator iter = mapMT.keySet().iterator();
          while(iter.hasNext()) {
            String configName =  (String) iter.next();           
            List groups = (List) mapMT.get(configName);
            List resGroups = new ArrayList(groups.size());
            Iterator iterGroups = groups.iterator();
            while(iterGroups.hasNext()) {
              List theseTrackers = (List) iterGroups.next();
              List resTrackers = new ArrayList(theseTrackers.size());
              Iterator iterTrackers = theseTrackers.iterator();
              while(iterTrackers.hasNext()) {
                String tracker = new String((byte[]) iterTrackers.next());
                resTrackers.add(tracker);
              }
              resGroups.add(resTrackers);
            }
            this.multiTrackers.put(configName,resGroups);
          }
        }
        webseeds = (Map)map.get( "webseeds" );
       
        if ( webseeds == null ){
          webseeds = new HashMap();
        }else{
          BDecoder.decodeStrings( webseeds );
        }
      } catch(Exception e) {
       
        Debug.printStackTrace( e );
       
    }finally{
     
    if ( bin != null ){
      try{
          bin.close();
      }catch( Throwable e ){
      }
    }
    if ( fin != null ){
      try{
View Full Code Here

        boolean isAscii = session.getDataType() == DataType.ASCII;
        long startTime = System.currentTimeMillis();
        byte[] buff = new byte[4096];

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = IoUtils.getBufferedInputStream(in);

            bos = IoUtils.getBufferedOutputStream(out);

            DefaultFtpSession defaultFtpSession = null;
            if (session instanceof DefaultFtpSession) {
                defaultFtpSession = (DefaultFtpSession) session;
            }

            byte lastByte = 0;
            while (true) {

                // if current rate exceeds the max rate, sleep for 50ms
                // and again check the current transfer rate
                if (maxRate > 0) {

                    // prevent "divide by zero" exception
                    long interval = System.currentTimeMillis() - startTime;
                    if (interval == 0) {
                        interval = 1;
                    }

                    // check current rate
                    long currRate = (transferredSize * 1000L) / interval;
                    if (currRate > maxRate) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException ex) {
                            break;
                        }
                        continue;
                    }
                }

                // read data
                int count = bis.read(buff);

                if (count == -1) {
                    break;
                }

View Full Code Here

            long skipLen) throws IOException {
        InputStream in;
        if (session.getDataType() == DataType.ASCII) {
            int c;
            long offset = 0L;
            in = new BufferedInputStream(file.createInputStream(0L));
            while (offset++ < skipLen) {
                if ((c = in.read()) == -1) {
                    throw new IOException("Cannot skip");
                }
                if (c == '\n') {
View Full Code Here

    // System.out.println( "Processor starts: " + socket.getRemoteSocketAddress());
   
    boolean  keep_alive = getServer().isKeepAliveEnabled();
   
    try{
      InputStream  is = new BufferedInputStream( socket.getInputStream());

      while( true ){
       
        setTaskState( "entry" );
       
        try
          if ( keep_alive ){
           
            socket.setSoTimeout( KEEP_ALIVE_SOCKET_TIMEOUT );
           
            setTimeoutsDisabled( true );
           
          }else{
         
            socket.setSoTimeout( SOCKET_TIMEOUT );
          }         
        }catch ( Throwable e ){
                           
          // e.printStackTrace();
        }
                             
        setTaskState( "reading header" );
 
        try{
                               
          byte[]  buffer       = new byte[16*1024];
          int    header_pos    = 0;
         
          while( header_pos < buffer.length ){
             
            int  len = is.read( buffer, header_pos, 1);
             
            if ( len != 1 ){
           
              throw( new Exception( "Premature end of stream reading header" ));
            }
           
            header_pos++;
           
            if (   header_pos >= 4 &&
                buffer[header_pos-4] == CR &&
                buffer[header_pos-3] == FF &&
                buffer[header_pos-2] == CR &&
                buffer[header_pos-1] == FF ){
                 
              break;
            }
          }

          String  header = new String( buffer, 0, header_pos, Constants.BYTE_ENCODING );
     
          if ( Logger.isEnabled()){
           
            String  log_str = header;
           
            int  pos = log_str.indexOf( NL );
           
            if ( pos != -1 ){
             
              log_str = log_str.substring(0,pos);
            }
           
            Logger.log(new LogEvent(LOGID, "Tracker Server: received header '"
                + log_str + "' from " + socket.getRemoteSocketAddress()));
          }       
           
          // System.out.println( "got header:" + header );
         
          InputStream  post_is   = null;
          File    post_file  = null;
         
          String  lowercase_header;
         
          boolean  head  = false;
         
          int  url_start;
         
 
          if ( header.startsWith( "GET " )){
         
            timeout_ticks    = 1;
           
            lowercase_header  = header.toLowerCase();
            url_start      = 4;
           
          }else if ( header.startsWith( "HEAD " )){
           
            timeout_ticks    = 1;
           
            lowercase_header  = header.toLowerCase();     
            url_start      = 5;
 
            head  = true;
           
          }else if ( header.startsWith( "POST ")){
           
            timeout_ticks  = TRTrackerServerTCP.PROCESSING_POST_MULTIPLIER;
           
            if ( timeout_ticks == 0 ){
             
              setTimeoutsDisabled( true );
            }
           
            setTaskState( "reading content" );
           
            lowercase_header  = header.toLowerCase();
            url_start      = 5;
 
            String cl_str = getHeaderField( header, lowercase_header, "content-length:" );
           
            if ( cl_str == null ){
             
              throw( new Exception( "Content-Length missing" ));
            }
           
            int content_length = Integer.parseInt( cl_str );
           
            ByteArrayOutputStream  baos    = null;
            FileOutputStream    fos      = null;
         
            OutputStream  data_os;
           
            if ( content_length <= 256*1024 ){
             
              baos = new ByteArrayOutputStream();
           
              data_os  = baos;
             
            }else{
             
              post_file  = AETemporaryFileHandler.createTempFile();
             
              post_file.deleteOnExit();
             
              fos  = new FileOutputStream( post_file );
             
              data_os  = fos;
            }
           
            while( content_length > 0 ){
             
              int  len = is.read( buffer, 0, Math.min( content_length, buffer.length ));
             
              if ( len < 0 ){
               
                throw( new TRTrackerServerException( "premature end of input stream" ));
              }
             
              data_os.write( buffer, 0, len );
             
              content_length -= len;
            }
           
            if ( baos != null ){
             
              post_is = new ByteArrayInputStream(baos.toByteArray());
             
            }else{
             
              fos.close();
             
              post_is = new BufferedInputStream( new FileInputStream( post_file ), 256*1024 );
            }
           
            // System.out.println( "TRTrackerServerProcessorTCP: request data = " + baos.size());
           
          }else{
View Full Code Here

   * @return UserManager object
   * @throws FileNotFoundException
   */
  public void load( ) throws FileNotFoundException
  {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
    doLoad( bis );
  }
View Full Code Here

TOP

Related Classes of java.io.BufferedInputStream

Copyright © 2015 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.