www.fatihkabakci.com

Personal Website and Computer Science TUR EN

JAVA NIO KAVRAMI

Last update: 12/16/2014 11:40:00 PM

Yazan:Fatih KABAKCI

JDK 6 ile piyasaya sürülen New IO veya Non-Blocking IO olarak anılan, kısaca NIO API' si, geleneksel Java IO paketine ek bir takım özelliklerin yanı sıra, IO işlemleri için çok önemli iki kavramı da getirmiştir. Bunlar buffer(tampon) ve channel(kanal) kavramları.

Tamponlar IO işlemlerinde verileri tutarken, kanallar ise bu tamponların herhangi bir IO aygıtına yaptığı bağlantıları tutar. Yani birisi veri saklama, diğer bağlantı saklama ve yönetme işlemlerinden sorumludur. Veriler tamponlar içerisinde, dosyalara, printer' lara ve diğer ağ aygıtlarına kanallar yardımıyla taşınır. Bu durum klasik girdi/çıktı sisteminden farklıdır. Ancak NIO, klasik IO sistemini ezen yeni bir yöntem olarak kabul görmez. Yalnızca bazı işlemlerin daha kolay yapılabilmesini sağlamaktadır.

Java NIO sistemi aşağıda açıklanan 4 kavramı barındırır.

Tamponlar(Buffer)

Tamponlar(Buffer) IO işlemlerinde kullanılan kaynak/hedef verileri barındıran dizilerdir. IO işlemleri sırasında kanallar üzerinden taşınır.

Kanallar(Channel)

Kanallar(Channel) IO işlemlerinin yapılmasını sağlar. Bağlantı açma, kapama ve tampon verinin taşınmasından sorumludur.

Karakter Setleri(Charset)

Klasik IO sisteminde olduğu gibi, byte ve karakter stream geçişini sağlar. byte ve Unicode karakter setleri geçişi buna bir örnektir.

Seçiciler(Selector)

Seçiciler(Selector) kanallar üzerinde bekleme yaratan mekanizmalar olarak adlandırılır. Ortamdaki kanal sayısı bir selector ile kaydettirildiği zaman, kanallardan biri kullanıma hazır olana kadar veya programda bir interrupt(kesme) meydana gelene kadar program askıya alınır.
Aşağıda NIO kullanılarak, bir txt dosyasından okuma yapan ve bir txt dosyasına yazma yapan program örnekleri verilmiştir.

Dosyadan Okuma

package IO.NIO;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 
 * @author www.fatihkabakci.com
 * This class shows that how reading is on the nio system
 *
 */
public class ReadingApp {
   public static void main(String[] args) {
      try {
         FileInputStream fis = new FileInputStream("com.fatihkabakci/IO/NIO/data.txt");
         FileChannel channel = fis.getChannel();
         long size = channel.size();
         ByteBuffer bf = channel.map(FileChannel.MapMode.READ_ONLY, 0L, size);
         for(int i = 0; i < size; i++) {
            System.out.print((char)bf.get());
         }
         channel.close();
         fis.close();
      }catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Dosyaya Yazma

package IO.NIO;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author www.fatihkabakci.com
 * This class shows that how writing is on the nio system
 */
public class WritingApp {
   public static void main(String[] args) {
      try {
         byte[] source = "Fatih".getBytes();

         RandomAccessFile raf = new RandomAccessFile("com.fatihkabakci/IO/NIO/data.txt", "rw");
         FileChannel channel = raf.getChannel();
         ByteBuffer bf = channel.map(FileChannel.MapMode.READ_WRITE, 0, source.length);
         bf.put(source);
         channel.write(bf);

         channel.close();
         raf.close();
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Yukarıdaki programlarda dikkat edilirse, öncelikle bir tampon(buffer) oluşturulur. Bu tampon veriyi tutması için önemlidir. Daha sonra txt dosyasını ihtiva eden bir kanal yardımıyla veri ilgili dosyaya tampon üzerinden yazılmış olur. Bu esasen otomatik olarak map() metodu sayesinde olur.

Özetle NIO sistemi, klasik IO sistemine ek bir takım olumlu gelişmeler sağlamaktadır. Klasik IO sistemi güçlü bir şekilde kullanılmaya devam ederken, ileride NIO sisteminin varlığını sürdürmesi ise, yeni problemlere ne kadar adapte olabileceğine bağlıdır.

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.