1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.io.ByteArrayInputStream ; 41 import java.io.ByteArrayOutputStream ; 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.io.OutputStreamWriter ; 45 import java.io.UnsupportedEncodingException ; 46 47 53 public final class TextUtil { 54 55 private TextUtil() {} 56 57 58 64 public static boolean startsWithIgnoreCase( final String stringToCheck, final String prefix ) { 65 Assert.notNull("stringToCheck", stringToCheck); 66 Assert.notNull("prefix", prefix); 67 68 if( prefix.length() == 0 ) { 69 throw new IllegalArgumentException ("Prefix may not be empty"); 70 } 71 72 final int prefixLength = prefix.length(); 73 if( stringToCheck.length() < prefixLength ) { 74 return false; 75 } 76 else { 77 return stringToCheck.substring(0,prefixLength).toLowerCase().equals(prefix.toLowerCase()); 78 } 79 } 80 81 82 87 public static InputStream toInputStream( final String content ) { 88 try { 89 return toInputStream(content, "ISO-8859-1"); 90 } 91 catch( final UnsupportedEncodingException e ) { 92 throw new IllegalStateException ( 93 "ISO-8859-1 is an unsupported encoding! You may have a corrupted installation of java."); 94 } 95 } 96 97 104 public static InputStream toInputStream( 105 final String content, 106 final String encoding ) 107 throws 108 UnsupportedEncodingException { 109 110 try { 111 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (content.length()*2); 112 final OutputStreamWriter writer = new OutputStreamWriter (byteArrayOutputStream, encoding); 113 writer.write(content); 114 writer.flush(); 115 116 final byte[] byteArray = byteArrayOutputStream.toByteArray(); 117 return new ByteArrayInputStream (byteArray); 118 } 119 catch( final UnsupportedEncodingException e ) { 120 throw e; 121 } 122 catch( final IOException e ) { 123 e.printStackTrace(); 126 throw new IllegalStateException ("Exception when converting a string to an input stream: "+e); 127 } 128 } 129 } 130 | Popular Tags |