Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
With reference to the following thread: http://stackoverflow.com/questions/498636/java-app-unable-to-read-iso-8859-1-encoded-file-correctly What is the best way to programatically determine the correct charset encoding of an inputstream/file ? I have tried using the following: File in = new File(args[0]); InputStreamReader r = new InputStreamReader(new FileInputStream(in)); System.out...
In Java, I have a String and I want to encode it as a byte array (in UTF8, or some other encoding). Alternately, I have a byte array (in some known encoding) and I want to convert it into a Java String. How do I do these conversions?
I'm reading a local file using a BufferedReader wrapped around a FileReader: BufferedReader reader = new BufferedReader(new FileReader(fileName)); // read the file // (error handling snipped) reader.close(); Do I need to close() the FileReader as well, or will the wrapper handle that? I've seen code where people do something like this: FileReader fReader = new FileReader(fileName); Buffered...
This has probably been answered else where but how do you get the character value of an int value? Specifically I'm reading a from a tcp stream and the readers .read() method returns an int. How do I get a char from this?
This is a double question for you amazingly kind Stacked Overflow Wizards out there. How do I set emacs/slime/swank to use UTF-8 when talking with Clojure, or use UTF-8 at the command-line REPL? At the moment I cannot send any non-roman characters to swank-clojure, and using the command-line REPL garbles things. It's really easy to do regular expressions on latin text: (re-seq #"[\w]+" "It's...
I know there used to be a way to get it with apache commons as documented here: http://hc.apache.org/httpclient-legacy/apidocs/org/apache/commons/httpclient/HttpMethod.html and an example here: http://www.kodejava.org/examples/416.html but i believe this is deprecated. Is there any other way to make an http get request in java and get the response body as a string and not a stream?
For example I have following code Source.fromFile(new File( path), "UTF-8").getLines() and it throws exception Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(CoderResult.java:260) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319) I don't care if some lines were not read, but how to s...
I was wondering, whether is there any need for me to close the InputStream, after I close the reader? try { inputStream = new java.io.FileInputStream(file); reader = new InputStreamReader(inputStream, Charset.forName("UTF-8")); } catch (Exception exp) { log.error(null, exp); } finally { if (false == close(reader)) { return null; ...
Hey Everyone , I need your help here please. I'm working on a java application that convert data from a txt file into the database , The problem is that the file have ANSI encoding which i can't change because it comes from outside my application ,and when i write the data to the database i got some "???" inside. My question is , how can i convert the data that i read from the file from ANSI to...
This is a "meta-question" which I came across when trying to find a better specification for another of my questions (Rendering Devanagari ligatures (Unicode) in Java Swing JComponent on Mac OS X). What I don't quite understand as of yet is which "component" (for want of a better word) of a given system is responsible for displaying Unicode text in Java, and more specifically ligatures. As f...
How do you read and display data from .txt files?
I have a Java application that receives data over a socket using an InputStreamReader. It reports "Cp1252" from its getEncoding method: /* java.net. */ Socket Sock = ...; InputStreamReader is = new InputStreamReader(Sock.getInputStream()); System.out.println("Character encoding = " + is.getEncoding()); // Prints "Character encoding = Cp1252" That doesn't necessarily match what the system rep...
We are trying to download source of webpages, however we cannot see some specific characters -like ü,ö,ş,ç- propoerly due to character encoding. We tried the following code in order to convert encoding of the string ("text" variable): byte[] xyz = text.getBytes(); text = new String(xyz,"windows-1254"); We observed that if encoding is utf-8, we still cannot see pages correctly. What should w...
I have a file which is encoded as iso-8859-1, and contains characters such as ô . I am reading this file with java code, something like: File in = new File("myfile.csv"); InputStream fr = new FileInputStream(in); byte[] buffer = new byte[4096]; while (true) { int byteCount = fr.read(buffer, 0, buffer.length); if (byteCount <= 0) { brea...
I currently use the following function to do a simple HTTP GET. public static String download(String url) throws java.io.IOException { java.io.InputStream s = null; java.io.InputStreamReader r = null; //java.io.BufferedReader b = null; StringBuilder content = new StringBuilder(); try { s = (java.io.InputStream)new URL(url).getContent(); r = new java.io.Inpu...
I'm trying to make a bash script that will talk to a java program that waits for my commands via bash. the java program is running as a server with a very limited GUI so I'm working on making a basic UI for it that will add functionality to it, any help on this topic would be nice. The ways I've tried to start it currently are: INPUTFD=258 #exec "$INPUTFD"> >(exec java -Xmx512M -Xms512M...
i need to recreate this within Java for Blackberry device: char cPacketData[1024]; int thisPacketLength=( X_PACKET_SPACE*12 ) + ( 20*X_PACKET_SPACE ); (*(int *) (cPacketData)) =X_PACKET_START; (*(int *) (cPacketData+X_PACKET_SPACE)) =thisPacketLength; (*(int *) (cPacketData+X_PACKET_SPACE*2)) =X_PACKET_POSITION_DATA; (*(int *) (cPacketData+X_PACKET_SPACE*3)) =posit...
I have issue with spanish characters in java string. I have a content in a file and when i try to transform it to java object using InputStreamReader, the output of some string is "cómo" which should be "cómo". This is happening other spanish like á = á é = é í = í ó = ó ú = ú and more.. Could you please help me to convert it appropriate output. Thanks in advance
I am trying to read an HTML file which is encoded in EUC-KR from a URL. When I compile the code inside the IDE I get the desired output, but when I build a jar and try running the jar, the data I read is shown as question marks ("????" instead of the korean characters). I am assuming it is due to loss of encoding. The meta of the site says the following: <meta http-equiv="Content-Type" con...
I have a webpage that is encoded (through its header) as WIN-1255. A Java program creates text string that are automatically embedded in the page. The problem is that the original strings are encoded in UTF-8, thus creating a Gibberish text field in the page. Unfortunately, I can not change the page encoding - it's required by a customer propriety system. Any ideas? UPDATE: The page I'm cr...
  /*
   * Copyright 1996-2005 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code 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 General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.io;
 
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified java.nio.charset.Charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:

 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));
 

Author(s):
Mark Reinhold
Since:
JDK1.1
See also:
BufferedReader
InputStream
java.nio.charset.Charset
 
 
 public class InputStreamReader extends Reader {
 
     private final StreamDecoder sd;

    
Creates an InputStreamReader that uses the default charset.

Parameters:
in An InputStream
 
     public InputStreamReader(InputStream in) {
         super(in);
         try {
              = StreamDecoder.forInputStreamReader(inthis, (String)null); // ## check lock object
         } catch (UnsupportedEncodingException e) {
             // The default encoding should always be available
             throw new Error(e);
         }
     }

    
Creates an InputStreamReader that uses the named charset.

Parameters:
in An InputStream
charsetName The name of a supported charset
Throws:
UnsupportedEncodingException If the named charset is not supported
 
     public InputStreamReader(InputStream inString charsetName)
         throws UnsupportedEncodingException
     {
         super(in);
         if (charsetName == null)
             throw new NullPointerException("charsetName");
         = StreamDecoder.forInputStreamReader(inthischarsetName);
    }

    
Creates an InputStreamReader that uses the given charset.

Parameters:
in An InputStream
cs A charset
Since:
1.4
Spec:
JSR-51
    public InputStreamReader(InputStream inCharset cs) {
        super(in);
        if (cs == null)
            throw new NullPointerException("charset");
         = StreamDecoder.forInputStreamReader(inthiscs);
    }

    
Creates an InputStreamReader that uses the given charset decoder.

Parameters:
in An InputStream
dec A charset decoder
Since:
1.4
Spec:
JSR-51
    public InputStreamReader(InputStream inCharsetDecoder dec) {
        super(in);
        if (dec == null)
            throw new NullPointerException("charset decoder");
         = StreamDecoder.forInputStreamReader(inthisdec);
    }

    
Returns the name of the character encoding being used by this stream.

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the InputStreamReader(java.io.InputStream,java.lang.String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method will return null if the stream has been closed.

Returns:
The historical name of this encoding, or null if the stream has been closed
See also:
java.nio.charset.Charset
Revised:
1.4
Spec:
JSR-51
    public String getEncoding() {
        return .getEncoding();
    }

    
Reads a single character.

Returns:
The character read, or -1 if the end of the stream has been reached
Throws:
IOException If an I/O error occurs
    public int read() throws IOException {
        return .read();
    }

    
Reads characters into a portion of an array.

Parameters:
cbuf Destination buffer
offset Offset at which to start storing characters
length Maximum number of characters to read
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException If an I/O error occurs
    public int read(char cbuf[], int offsetint lengththrows IOException {
        return .read(cbufoffsetlength);
    }

    
Tells whether this stream is ready to be read. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream.

Throws:
IOException If an I/O error occurs
    public boolean ready() throws IOException {
        return .ready();
    }
    public void close() throws IOException {
        .close();
    }
New to GrepCode? Check out our FAQ X