Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2010 eXo Platform SAS.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.wikbook.core;
 
 import java.io.Reader;
 import java.net.URL;

Author(s):
Julien Viet
Version:
$Revision$
 
 public class Utils
 {
 
    public static Iterable<Stringsplit(final String s)
    {
       return new Iterable<String>()
       {
          public Iterator<Stringiterator()
          {
             return new Iterator<String>()
             {

               
The index of previous.
 
                private int from = 0;
 
                public boolean hasNext()
                {
                   return  <= s.length();
                }
 
                public String next()
                {
                   if ( > s.length())
                   {
                      throw new NoSuchElementException();
                   }
                   int to = s.indexOf('\n');
                   if (to == -1)
                   {
                      to = s.length();
                   }
                   String next = s.substring(to);
                    = to + 1;
                   return next;
                }
 
                public void remove()
                {
                   throw new UnsupportedOperationException();
                }
             };
          }
       };
    }
 
    public static Reader read(URL urlthrows IOException
    {
       return new InputStreamReader(url.openStream(), "UTF-8");
    }
 
    public static String read(Reader readerthrows IOException
    {
       StringWriter writer = new StringWriter();
       char[] buffer = new char[256];
       for (int amount = reader.read(buffer); amount != -1; amount = reader.read(buffer))
       {
          writer.write(buffer, 0, amount);
       }
       return writer.toString();
    }
 
    public static byte[] load(URL urlthrows IOException
    {
       return load(url.openStream());
   }
   public static byte[] load(InputStream inthrows IOException
   {
      try
      {
         byte[] bytes = new byte[128];
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         for (int s = in.read(bytes); s != -1; s = in.read(bytes))
         {
            baos.write(bytes, 0, s);
         }
         bytes = baos.toByteArray();
         return bytes;
      }
      finally
      {
         try
         {
            in.close();
         }
         catch (IOException ignore)
         {
         }
      }
   }
New to GrepCode? Check out our FAQ X