{{Short description|Sequence of bits or bytes of a fixed size}} {{About|the computer input/output technique|the process scheduling concept|Blocking (computing)}} {{Distinguish|Page (computer memory)|Bank (computer memory)}} {{More citations needed|date=April 2014}} {{Use dmy dates|date=March 2020}}
In computing (specifically data transmission and data storage), a '''block''',<ref name="Buchholz_1962"/> sometimes called a '''physical record''', is a sequence of bytes or bits, usually containing some whole number of records, having a fixed length; a ''block size''.<ref name="CNET14"/> Data thus structured are said to be ''blocked''. The process of putting data into blocks is called ''blocking'', while ''deblocking'' is the process of extracting data from blocks. Blocked data is normally stored in a data buffer, and read or written a whole block at a time. Blocking reduces the overhead and speeds up the handling of the data stream.<ref name="Chang"/> For some devices, such as magnetic tape and CKD disk devices, blocking reduces the amount of external storage required for the data. Blocking is almost universally employed when storing data to 9-track magnetic tape, NAND flash memory, and rotating media such as floppy disks, hard disks, and optical discs.
Most file systems are based on a block device, which is a level of abstraction for the hardware responsible for storing and retrieving specified blocks of data, though the block size in file systems may be a multiple of the physical block size. This leads to space inefficiency due to internal fragmentation, since file lengths are often not integer multiples of block size, and thus the last block of a file may remain partially empty. This will create slack space. Some newer file systems, such as Btrfs and FreeBSD UFS2, attempt to solve this through techniques called block suballocation and tail merging. Other file systems such as ZFS support variable block sizes.<ref name="ZFS1"/><ref name="ZFS2"/>
Block storage is normally abstracted by a file system or database management system (DBMS) for use by applications and end users. The physical or logical volumes accessed via ''block I/O'' may be devices internal to a server, directly attached via SCSI or Fibre Channel, or distant devices accessed via a storage area network (SAN) using a protocol such as iSCSI, or AoE. DBMSes often use their own block I/O for improved performance and recoverability as compared to layering the DBMS on top of a file system.
On Linux the default block size for most file systems is 4096 bytes. The {{Mono|stat}} command part of GNU Core Utilities can be used to check the block size.
==In languages== ===C++=== In C++, a block can be read using <code>std::ifstream</code> (input file stream). <syntaxhighlight lang="cpp"> import std;
using std::array; using std::byte; using std::ifstream; using std::ios;
constexpr size_t BLOCK_SIZE = 4096;
try { ifstream file("example.bin", ios::binary); file.exceptions(ios::failbit | ios::badbit); array<byte, BLOCK_SIZE> buf; file.read(reinterpret_cast<char*>(buf.data()), BLOCK_SIZE); } catch (const ios::failure& e) { std::println(stderr, "I/O error: {}", e.what()); } </syntaxhighlight>
===C#=== In C#, a block can be read with the {{Code|FileStream}} class.<ref>{{cite web |title=FileStream.ReadAsync Method (System.IO) |url=https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.readasync?view=net-9.0 |website=learn.microsoft.com |access-date=2 February 2025 |language=en-us |archive-url=https://web.archive.org/web/20251226171806/https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.readasync?view=net-9.0 |archive-date=2025-12-26}}</ref> <syntaxhighlight lang="csharp"> using System.IO;
static const int BLOCK_SIZE = 4096;
using (FileStream stream = File.Open("example.bin", FileMode.Open)) { byte[] block = new byte[BLOCK_SIZE]; await stream.ReadAsync(block, 0, BLOCK_SIZE); } </syntaxhighlight>
===Java=== In Java, a block can be read using <code>java.io.FileInputStream</code>. <syntaxhighlight lang="java"> import java.io.FileInputStream; import java.io.IOException;
static final int BLOCK_SIZE = 4096;
try (FileInputStream file = new FileInputStream("example.bin")) { byte[] buf = new byte[BLOCK_SIZE]; file.read(buf); } catch (IOException e) { e.printStackTrace(); } </syntaxhighlight>
===Python=== In Python, a block can be read with the {{Code|read}} method of whatever is implementing <code>io.IOBase</code>. <syntaxhighlight lang="python"> BLOCK_SIZE: int = 4096
with open("example.bin", "rb") as file: # file is of type io.BufferedReader block: bytes = file.read(BLOCK_SIZE) </syntaxhighlight>
===Rust=== In Rust, a block can be read with the {{Code|read_exact}} method of <code>std::fs::File</code>.<ref>{{cite web |title=Read in std::io - Rust |url=https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |website=doc.rust-lang.org |access-date=2 February 2025 |archive-url=https://web.archive.org/web/20260128103441/https://doc.rust-lang.org/std/io/trait.Read.html |archive-date=2026-01-28}}</ref> <syntaxhighlight lang="rust"> use std::fs::File;
const BLOCK_SIZE: usize = 4096;
if let Ok(mut file) = File::open("example.bin") { let mut buf = [0u8; BLOCK_SIZE]; file.read_exact(&mut buf); } </syntaxhighlight>
== References == <references> <ref name="CNET14">{{cite news |author=<!-- staff writer, no byline --> |title=Available hard drive space, block sizes, and size terminology |newspaper=CNET |date=2009-05-05 |url=http://www.cnet.com/news/available-hard-drive-space-block-sizes-and-size-terminology/ |access-date=2014-04-29}}</ref> <ref name="Chang">{{cite web |author-last=Chang |author-first=S. K. |title=Physical Structures |work=Captain SK |url=http://people.cs.pitt.edu/~chang/156/08struct.html |access-date=2014-04-29}}</ref> <ref name="ZFS1">{{cite news |author-first=Rachel |author-last=Balik |title=Bruning Questions: ZFS Record Size |newspaper=Joyent |date=2013-03-29 |url=https://www.joyent.com/blog/bruning-questions-zfs-record-size/ |access-date=2013-03-29}}</ref> <ref name="ZFS2">{{cite news |author-first=Roch |author-last=Bourbonnais |title=Tuning ZFS recordsize |date=2006-06-07 |newspaper=Oracle |url=https://blogs.oracle.com/roch/tuning-zfs-recordsize}}</ref> <ref name="Buchholz_1962">{{citation |title=Planning a Computer System – Project Stretch |author-first1=Gerrit Anne |author-last1=Blaauw |author-link1=Gerrit Anne Blaauw |author-first2=Frederick Phillips |author-last2=Brooks, Jr. |author-link2=Frederick Phillips Brooks, Jr. |author-first3=Werner |author-last3=Buchholz |author-link3=Werner Buchholz |editor-first=Werner |editor-last=Buchholz |editor-link=Werner Buchholz |publisher=McGraw-Hill Book Company, Inc. / The Maple Press Company, York, PA. |lccn=61-10466 |date=1962 |chapter=4: Natural Data Units |pages=39–40 |chapter-url=http://archive.computerhistory.org/resources/text/IBM/Stretch/pdfs/Buchholz_102636426.pdf |access-date=2017-04-03 |url-status=live |archive-url=https://web.archive.org/web/20170403014651/http://archive.computerhistory.org/resources/text/IBM/Stretch/pdfs/Buchholz_102636426.pdf |archive-date=2017-04-03 |quote=[…] Terms used here to describe the structure imposed by the machine design, in addition to ''bit'', are listed below.<br/>''Byte'' denotes a group of bits used to encode a character, or the number of bits transmitted in parallel to and from input-output units. A term other than ''character'' is used here because a given character may be represented in different applications by more than one code, and different codes may use different numbers of bits (i.e., different byte sizes). In input-output transmission the grouping of bits may be completely arbitrary and have no relation to actual characters. (The term is coined from ''bite'', but respelled to avoid accidental mutation to ''bit''.)<br/>A ''word'' consists of the number of data bits transmitted in parallel from or to memory in one memory cycle. Word size is thus defined as a structural property of the memory. (The term ''catena'' was coined for this purpose by the designers of the Bull {{ill|Bull Gamma 60{{!}}GAMMA 60|fr|Gamma 60}} computer.)<br/>''Block'' refers to the number of words transmitted to or from an input-output unit in response to a single input-output instruction. Block size is a structural property of an input-output unit; it may have been fixed by the design or left to be varied by the program. […]}}</ref> </references>
Category:Articles with example C Sharp code Category:Articles with example Python (programming language) code Category:Articles with example Rust code Category:Computer data storage Category:Data transmission