Skip to main content

Pipe is not always for smoking

pipes with IO & Networking

Is a way of communication, connecting two ends "program to a file , program to a program ...etc".as if a person calling another between those two person there exist a pipe. For IO one end is a file, where as for networking both ends are programs

basic operation

  • open():inputStream, for opening a connection. A person dailing a number, the other end pick up
  • close(), for closing this connection, one person hang up
  • write(byte[] bufferToWrite,int numOfByteToWrite):numOfActuallyWriten. A person talking
  • read(byte[] bufferToReadIn,int numOfByteToRead):numOfBytesRead. A person listening

read() operation is a blocking one, so can use available() to check if in the input stream there exist any data to read before do a blocking read. can also synchronize between the two ends by adding a Buffer
for file:
FileInputStream --> BufferedInputStream --> DataInputStream
BufferedInputStream is used to read stream of bytes, if want to distinguish "reading int, char ...etc" use DataInput

In networking we have a socket object that is used to construct a Berkly socket between two ends, also for higher level we can use URL + URLConnection both connected togther to construct a client that can communicate with a http server.
ex:
URL url = new URL("Server URL");
URLConnection connection = url.openConnection();
connection.setRequestProperty(....)//for setting header data
PrintWriter out= new PrintWriter(connection.getOutPutStream())
out.print(name+"="+URLEncoder.encode(value)+"&")//setting request parameter

input output
Binary InputStream OutputStream
text Reader Writer

Comments

Popular posts from this blog

The post-office & the postman

If we were to talk about old messaging system where there exist post-office, postman & mailbox. each component had its own functionality that we looked for when trying to visualize how those component where to interact in a computerized version. Simple scenario: Mail is added in mail box Postman arrive pick mails from his area mailboxes and take them to the post-office. Post-office organize mails by areas. Postman takes mails related to his area "distribute it in mailboxes". A person can go to post-office and  pick his own mail "in case of failure or wishes for early delivery". Mapping in a computerized version: Scenario: Observer design pattern which can use push or pull scenario, to inform those whom are registered for an event about its occurrence. Component: Post-Office = Message-Broker Post-Office-Box = Message-Storage-Validity Mailbox = Topic/Queue Postman !!! where's the postman ? Apache kafka act as a message broker which d...

Not all Ps sting

  If someone meant to say Ps and pronounce it Bees. would this confuse you :). Ps is for the P that is the start of Properties and Practice Each application should have some properties and follow certain practices. Properties: Below are 5 properties we should try to have in our application with a small description of how to include them Scalable, Scale => Increase workload (horizontally scaling) Statless, no state should be shared among different application instances,  Concurrency, concurrent processing = Threads. Loosely coupled, decompose the system into modules, each has minimal dependencies on each other "modularization", encapsulating code that changes together "High cohesion".  API first, Interfaces, implementation can be changed without affecting other application. favor distribution of work across different teams.  Backing Services, "DB, SMTP, FTP ..." , treating them as attached resources, meaning they can easily be changed. Manageable, changi...

String literal pool

I'm used to use String .format() when constructing a SQL statement, and a friend I knew likes to concatenate saying :"it's more readable to me". So String Objects are immutable, meaning that once they are created, they can't be altered. Concatenating 2 strings doesn't modify either Strings instead, it creates a new String "old ones are added to the string pool". String literals always have a reference to them in String Literal Pool, therefore not eligible for garbage collection. to concatenate use StringBuffer (Synchronized) or StringBuilder (not Synchronized) As both uses an internal array "so that new String Objects are not created". String literal Pool : String are stored in pool and before creating a new string  literals, compiler checks if such string already defined "used to optimize and save space". String literals : a sequence of characters between quotation marks.