Client-Server Model & Java Streams
40 Questions
0 Views

Client-Server Model & Java Streams

Created by
@DiversifiedCatharsis

Questions and Answers

What does the java.io.Reader class primarily do?

  • Flushes data to the console.
  • Writes bytes to an output stream.
  • Converts characters to bytes.
  • Reads characters from a character stream. (correct)
  • Which method in the Writer class is used to write a single character?

  • writeSignature()
  • write(int c) (correct)
  • writeChar()
  • writeElement()
  • What is the purpose of the flush() method in the Writer class?

  • To read data from a character stream.
  • To skip characters in the stream.
  • To close the stream.
  • To ensure all data is written out. (correct)
  • What advantage does BufferReader provide over simple reading methods?

    <p>It retrieves text from a buffer for efficiency.</p> Signup and view all the answers

    How does the InputStreamReader handle data?

    <p>It reads bytes and converts them into characters.</p> Signup and view all the answers

    Which method allows the Reader class to determine if it can read data without blocking?

    <p>ready()</p> Signup and view all the answers

    Which method in the Reader class skips over a specified number of characters?

    <p>skip(long n)</p> Signup and view all the answers

    What type of streams do the FileReader and FileWriter classes work with?

    <p>Character streams for text files.</p> Signup and view all the answers

    What is the primary role of a server in relation to clients?

    <p>Offers a service to clients that connect to it</p> Signup and view all the answers

    Which method is used to forcibly write any buffered data in an output stream?

    <p>flush()</p> Signup and view all the answers

    What happens if a stream is not closed in a long-running program?

    <p>Resource leaks such as open file handles may occur</p> Signup and view all the answers

    What is indicated by returning -1 from a read() method on an input stream?

    <p>The end of the stream has been reached</p> Signup and view all the answers

    What is the main advantage of using readers and writers in Java I/O?

    <p>They facilitate handling text instead of bytes</p> Signup and view all the answers

    Which method is NOT a key method of the java.io.OutputStream class?

    <p>read()</p> Signup and view all the answers

    What is the purpose of filter streams in Java I/O?

    <p>To convert raw bytes into other formats</p> Signup and view all the answers

    What should you do before closing an output stream to avoid data loss?

    <p>Flush the stream</p> Signup and view all the answers

    What is the primary function of BufferedOutputStream?

    <p>To temporarily store data until it is written to the output stream.</p> Signup and view all the answers

    Which of the following methods does an InputStream use?

    <p>read()</p> Signup and view all the answers

    Which type of stream is responsible for handling text in formats like UTF-8?

    <p>Readers/Writers</p> Signup and view all the answers

    What happens when you call the read() method of a BufferedInputStream with an empty buffer?

    <p>It reads directly from the source.</p> Signup and view all the answers

    Which predefined stream variables are included in the System class?

    <p>in, out, err</p> Signup and view all the answers

    What is the significance of autoFlush in PrintStream?

    <p>It flushes the stream after every print() or println() call.</p> Signup and view all the answers

    Which method is NOT part of OutputStream functionality?

    <p>print()</p> Signup and view all the answers

    What is the primary purpose of filter streams?

    <p>To manipulate raw data in bytes.</p> Signup and view all the answers

    What does the method isFile() return?

    <p>True if the object is a file.</p> Signup and view all the answers

    What is the function of the length() method?

    <p>Returns the file size in bytes.</p> Signup and view all the answers

    Which interface must a class implement to allow serialization?

    <p>Serializable</p> Signup and view all the answers

    What is the primary purpose of the ObjectOutputStream class?

    <p>To save entire objects to a file.</p> Signup and view all the answers

    What is the logical connection identified by a number called?

    <p>Port</p> Signup and view all the answers

    Which range of ports is reserved for 'well-known' services?

    <p>1-1023</p> Signup and view all the answers

    When a client connects to a server, what does it normally create to establish communication?

    <p>A socket</p> Signup and view all the answers

    What happens when the server receives a connection request from a client?

    <p>It creates a new socket for this specific client.</p> Signup and view all the answers

    What should you wrap around a FileReader to improve reading efficiency?

    <p>BufferedReader</p> Signup and view all the answers

    Which method is used to read a line from a BufferedReader?

    <p>readLine()</p> Signup and view all the answers

    What type of exception is thrown if a file cannot be found when using FileInputStream?

    <p>FileNotFoundException</p> Signup and view all the answers

    What will the canWrite() method return if the file is not writable?

    <p>false</p> Signup and view all the answers

    Which statement is true about the File methods?

    <p>exists() returns true if the file is present.</p> Signup and view all the answers

    What must be done after finishing file operations, such as using FileReader or FileWriter?

    <p>Close the file</p> Signup and view all the answers

    Which class handles byte streams for file operations in Java?

    <p>FileOutputStream</p> Signup and view all the answers

    What does the isDirectory() method indicate about the file object?

    <p>True if the object is a directory</p> Signup and view all the answers

    Study Notes

    Client-Server Architecture

    • Servers provide services to clients that connect to them.
    • A server operates on a hosting machine and responds when a client initiates communication.
    • Common server services include web page hosting.
    • Web browsers like Firefox and Chrome function as clients accessing web servers.

    Java I/O Streams

    • Java Input/Output is structured around streams, which are sequences of data.
    • Input streams read data, while output streams write data; both types share fundamental read and write methods.
    • Filter streams can modify data during the reading or writing process.
    • Readers and writers are specialized for text handling, converting it instead of just working with bytes.

    Output Streams

    • Java's output class is java.io.OutputStream, which includes key methods:
      • write(int b): Writes a single byte.
      • write(byte[] data): Writes an array of bytes.
      • write(byte[] data, int off, int len): Writes a portion of an array.
      • flush(): Ensures all buffered data is written out.
      • close(): Releases resources associated with the stream.

    Importance of Closing Streams

    • The close() method frees resources like file handles and network ports.
    • Closing the stream ends any associated network connection.
    • Attempting to write to a closed stream will throw an IOException.
    • Not closing streams in long-running applications can lead to resource leaks.

    Flushing Streams

    • Flushing a stream before closing prevents data loss.
    • It helps mitigate hard-to-diagnose errors that might arise from incomplete data writing.

    Input Streams

    • The core input class for Java is java.io.InputStream, with essential methods:
      • int read(): Reads a single byte.
      • int read(byte[] data): Reads bytes into an array.
      • int read(byte[] data, int off, int len): Reads bytes with an offset.
      • long skip(long n): Skips over a defined number of bytes.
      • int available(): Returns the number of bytes that can be read.
      • close(): Closes the stream and frees resources.
    • The end of a stream is signaled by returning -1.

    Filter Streams

    • Filter streams enhance byte streams by converting raw data formats, such as compression.
    • Two types of filters exist: filter streams (for bytes) and readers/writers (for text).
    • Both filter input and output streams support similar methods as their respective streams.

    Buffered Streams

    • BufferedOutputStream temporarily collects data in a buffer until it's full or flushed for efficiency.
    • Constructors for BufferedOutputStream include:
      • BufferedOutputStream(OutputStream out)
      • BufferedOutputStream(OutputStream out, int bufferSize)
    • BufferedInputStream uses a buffer to read data first and only fetches from the source if needed.
    • PrintStream is widely used, with System.out being a common instance.
    • Additional constructors for PrintStream allow attachment to other output streams, including auto-flushing functionality.
    • Features print() and println() methods for outputting various data types.

    Predefined Streams

    • Three predefined stream variables in the System class:
      • System.out: Standard output to the console.
      • System.in: Standard input from the keyboard.
      • System.err: Outputs error messages to the console.
    • System.in is an InputStream, while System.out and System.err are PrintStream objects.

    Readers and Writers

    • java.io.Reader and java.io.Writer classes are designed for character read/write operations using Unicode.
    • FileReader and FileWriter are specific for file reading and writing.
    • Methods in the Writer class support various forms of writing characters and include flush() and close().

    Input/Output Stream Classes

    • InputStreamReader: Converts bytes from an input stream to characters using encoding.
    • BufferedReader: Improves reading efficiency by utilizing a buffer.
    • File creation for reading and writing involves FileReader and FileWriter, typically wrapped around buffered or print streams for better performance.

    File Handling

    • Use FileInputStream and FileOutputStream for file operations.
    • Constructors throw FileNotFoundException if issues arise with file accessibility or existence.
    • File class methods (e.g., canRead(), delete(), exists(), length()) enable various file-related operations.

    Command-Line Parameters

    • Java allows passing parameters during execution, accessible in the main method via an array of Strings.

    Serialization

    • Serialization saves an object's state in a byte stream for persistent storage, retrievable through deserialization.
    • Classes implementing the Serializable interface can be serialized and deserialized using ObjectOutputStream and ObjectInputStream.

    Ports and Sockets

    • A port is a numbered connection point for services, ranging from 1 to 65,535, with ports 1-1023 being "well-known" for standard services.
    • Sockets facilitate communication between client and server, with each end of the connection represented by a socket.
    • Servers generate a dedicated socket for communication after receiving connection requests from clients.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    CH1(chat).docx

    Description

    This quiz explores the fundamentals of the client-server model and the concept of streams in Java I/O. You will learn about how servers provide services to clients and the basics of input and output streams in Java. Test your knowledge on web communication and stream operations!

    More Quizzes Like This

    Client-Server Architecture and Caching
    10 questions
    Client-Server Architecture
    22 questions

    Client-Server Architecture

    JubilantExuberance avatar
    JubilantExuberance
    Client-Server Architecture
    6 questions
    Operating Systems: Client-server Architecture
    10 questions
    Use Quizgecko on...
    Browser
    Browser