Package anbxj
Class Channel_Abstraction
java.lang.Object
anbxj.Channel_Abstraction
- Direct Known Subclasses:
Channel_Client
,Channel_Server
Channel Abstraction: a class abstracting TCP/IP channel
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Channel_Properties
Properties specific to the channel. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for Channel Abstraction. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Checks if the provided port is within the valid range.void
Close()
Close channel streamsGets the role of the channel.void
Open()
Opens the channel, by invoking the checkPort method.protected void
Opens input and output streams for the specified socket.Receive()
Receives an object from the input stream.<T> T
Receives an object of a specified type with additional safety checks.static <T> T
safeReadObject
(Class<?> type, List<Class<?>> safeClasses, long maxObjects, long maxBytes, InputStream in) A method to replace the unsafe ObjectInputStream.readObject() method built into Java.void
Sends an object through the output stream.
-
Field Details
-
cp
Properties specific to the channel.
-
-
Constructor Details
-
Channel_Abstraction
Constructor for Channel Abstraction.- Parameters:
cp
- Channel properties for the abstraction.
-
-
Method Details
-
Close
public void Close()Close channel streams -
getChannelRole
Gets the role of the channel.- Returns:
- Channel role.
-
checkPort
protected void checkPort()Checks if the provided port is within the valid range. -
Open
public void Open()Opens the channel, by invoking the checkPort method. -
OpenStreams
Opens input and output streams for the specified socket.- Parameters:
s
- Socket for which to open streams.
-
Receive
Receives an object from the input stream.- Returns:
- Received object.
-
Receive
Receives an object of a specified type with additional safety checks.- Type Parameters:
T
- Parametric type.- Parameters:
type
- Class representing the object type expected to be returned.safeClasses
- List of Classes allowed in the serialized object being read.maxObjects
- Maximum number of objects allowed inside the serialized object being read.maxBytes
- Maximum number of bytes allowed to be read from the InputStream.- Returns:
- Received object of the specified type.
-
safeReadObject
public static <T> T safeReadObject(Class<?> type, List<Class<?>> safeClasses, long maxObjects, long maxBytes, InputStream in) throws IOException, ClassNotFoundException A method to replace the unsafe ObjectInputStream.readObject() method built into Java. This method checks to be sure the classes referenced are safe, the number of objects is limited to something sane, and the number of bytes is limited to a reasonable number. The returned Object is also cast to the specified type.- Type Parameters:
T
- Parametric type- Parameters:
type
- Class representing the object type expected to be returnedsafeClasses
- List of Classes allowed in serialized object being readmaxObjects
- long representing the maximum number of objects allowed inside the serialized object being readmaxBytes
- long representing the maximum number of bytes allowed to be read from the InputStreamin
- InputStream containing an untrusted serialized object- Returns:
- Object read from the stream (cast to the Class of the type parameter)
- Throws:
IOException
- I/O error exceptionClassNotFoundException
- Class not found exception
-
Send
Sends an object through the output stream.- Parameters:
obj
- Object to be sent.
-