DoubleBuffer asReadOnlyBuffer() method in Java with Examples27 Mar 2025 | 3 min read The java.nio.DoubleBuffer has an asReadOnlyBuffer() function. With the contents of this buffer, a new, read-only double buffer is created using the DoubleBuffer Class. The buffer is a duplicate of the new buffer. Therefore, any modifications made to the contents of this buffer will be included in the new buffer. Any changes made to the content of the new buffer will not be permitted as it is read-only. The position, limit, and mark values of the two buffers will not depend on one another. The capacity, limit, location, and mark values of the new buffer will be the same as those of the current buffer. The technique works identically to the duplicate method if this buffer is read-only. Syntax: Return Value: The new read-only double buffer with the identical contents as this buffer is returned by this method. Example 1:The code basically creates a DoubleBuffer with a five capacity, then loads it with five double values. In order to enable reading from the beginning, the buffer is then rewound to reset its position to zero. With the asReadOnlyBuffer() function, a read-only copy of the buffer is produced. The contents of this read-only buffer are identical, yet it is immutable. Both the original and read-only buffers' contents are printed. The code handles exceptions for ReadOnlyBufferException and IllegalArgumentException. Implementation:FileName: BufferReadOnlyExample1.java Output: The Original DoubleBuffer is given by : [9.63, 8.36, 7.54, 6.53, 5.87] The ReadOnlyBuffer DoubleBuffer is given by : 9.63 8.36 7.54 6.53 5.87 Example 2:The Java ReadOnlyBuffer's functionality is illustrated by the code. The asReadOnlyBuffer() function is used to create a read-only duplicate of the buffer after it has been rewound to its original location. Next, the contents of this read-only buffer are displayed on the screen. Since the read-only buffer is immutable, the attempt to add a new value to it by the code raises a ReadOnlyBufferException. The catch block handles and catches this exception. Implementation:FileName: BufferReadOnlyExample2.java Output: The Original DoubleBuffer is given by : [9.63, 8.36, 7.54, 6.53, 5.87] The ReadOnlyBuffer DoubleBuffer is given by : 9.63 8.36 7.54 6.53 5.87 ReadOnlyBufferException is caught: java.nio.ReadOnlyBufferException |
In the realm of software development, text processing is a common task. Whether you're building a search engine, a chatbot, or any application that deals with text, you might need to determine if certain words are present in a string. In this section, we will...
8 min read
Java is a widely-used programming language known for its versatility and ability to handle complex tasks. One of the fundamental concepts in Java programming is the use of iterative constructs that allows us to repeat a set of instructions multiple times. In this section, we will...
5 min read
Computers can understand only the numeric values. But, it is not always certain that all the inputs are given in numeric form. So, there was a need for an encoding system which could convert the text files into numeric values. For this (pronounced as...
2 min read
Internationalization is the process of developing a software application so that it can be altered to various languages and regions without modifying the application. Developing an application locale specific increases, the cost of the application, also require lots of maintenance. Localization is the process of adapting internationalized...
10 min read
Java URLEncoder is a utility class used to encode the URLs (Uniform Resource Locator). Reliability and security are ensured using Encoding of URL. When the user requests a particular site through the get method, the form parameters and their values are added after the '?' sign...
3 min read
In Java, static reference variables play a significant role in the overall structure and behaviour of a program. These variables hold references to objects or data that are shared across multiple instances of a class. By understanding static reference variables and their usage, developers can effectively...
4 min read
Stackers are linear data structures in principle. A simple Load-In-First-Out (LIFO) set is the last item added to the stack and the first item to be removed. The basic operations in a stack include push, pop, and peek. However, manipulating the middle element of a stack-such...
5 min read
The java.time.format.DecimalStyle class contains withNegativeSign() function. The character that is used to represent the negative sign for this DecimalStyle's locale in Java is set using the DecimalStyle class. Upon obtaining the character as an input, this function produces a DecimalStyle object containing the revised negative sign...
3 min read
Finding a pair with the smallest difference within an array is a common algorithmic problem in Java. It involves comparing differences between pairs of elements to identify the pair with minimal separation, and Java provides various solutions to address this challenge. Example 1: Input: A[] = {4, 7,...
7 min read
An error defines a reasonable issue that is topping the execution of the program. In different programming languages, there occur different types of errors as per the concepts. This section will discuss errors in Java and different types of errors, and when such errors occur. What is an...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India