jaulib v1.3.0
Jau Support Library (C++, Java, ..)
IOState.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021-2023 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.io;
25
26/**
27 * Mimic std::ios_base::iostate for state functionality, see iostate_func.
28 *
29 * This `enum class` type fulfills `C++ named requirements: BitmaskType`.
30 *
31 * @see ByteInStream
32 */
33public class IOState {
34 public enum Bit {
35 /** No error occurred nor has EOS being reached. Value is no bit set! */
36 none ( 0 ),
37
38 /** No error occurred nor has EOS being reached. Value is no bit set! */
39 goodbit ( 0 ),
40
41 /** Irrecoverable stream error, including loss of integrity of the underlying stream or media. */
42 badbit ( 1 << 0 ),
43
44 /** An input operation reached the end of its stream. */
45 eofbit ( 1 << 1 ),
46
47 /** Input or output operation failed (formatting or extraction error). */
48 failbit ( 1 << 2 ),
49
50 /** Input or output operation failed due to timeout. */
51 timeout ( 1 << 3 );
52
53 Bit(final int v) { value = v; }
54 public final int value;
55 }
56 public int mask;
57
58 public IOState(final int v) {
59 mask = v;
60 }
61 public IOState() {
62 mask = 0;
63 }
64
65 public boolean isSet(final Bit bit) { return bit.value == ( mask & bit.value ); }
66
67 /**
68 * Sets the given bit and returns this instance for chaining.
69 * @param bit the given Bit value to set
70 * @return this instance for chaining.
71 */
72 public IOState set(final Bit bit) { mask = (short) ( mask | bit.value ); return this; }
73
74 public IOState mask(final int bits) {
75 final int r = mask & bits;
76 if( r == mask ) { return this; }
77 else { return new IOState(r); }
78 }
79
80 private static native String to_string(final int mask);
81
82 @Override
83 public String toString() {
84 return to_string(mask);
85 }
86
87 @Override
88 public boolean equals(final Object other) {
89 if (this == other) {
90 return true;
91 }
92 return (other instanceof IOState) &&
93 this.mask == ((IOState)other).mask;
94 }
95}
Mimic std::ios_base::iostate for state functionality, see iostate_func.
Definition: IOState.java:33
boolean isSet(final Bit bit)
Definition: IOState.java:65
boolean equals(final Object other)
Definition: IOState.java:88
String toString()
Definition: IOState.java:83
IOState(final int v)
Definition: IOState.java:58
IOState mask(final int bits)
Definition: IOState.java:74
goodbit
No error occurred nor has EOS being reached.
Definition: IOState.java:39
none
No error occurred nor has EOS being reached.
Definition: IOState.java:36
badbit
Irrecoverable stream error, including loss of integrity of the underlying stream or media.
Definition: IOState.java:42
Bit(final int v)
Definition: IOState.java:53
failbit
Input or output operation failed (formatting or extraction error).
Definition: IOState.java:48
eofbit
An input operation reached the end of its stream.
Definition: IOState.java:45
timeout
Input or output operation failed due to timeout.
Definition: IOState.java:51