Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: creating byte[] with subfields Date: Fri, 18 Jan 2013 10:29:42 -0700 Organization: a light protective coating of dust Lines: 20 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="75975abe3fe3503ca7350803ab98e478"; logging-data="23530"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19EPSJ4xKiHhc1mKdwzKjsr" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:52Bm+xWHp7CeGvzQzzJS3rrsSio= sha1:SJlv2X4QYm/OKdLjYQ/T2dPo5ME= Xref: csiph.com comp.lang.java.programmer:21529 Jim Janney writes: > Roedy Green writes: > >> I often have to construct byte arrays with binary fields, or read >> them. >> >> I use a ByteArrayOutputStream and a DataOutputStream to compose them. >> I wondered if there is a simpler way, one that does not require >> computing each byte individually with shifts. > > In C, sure. Java by design doesn't allow those kinds of tricks. The hotspot compiler might notice that you're shifting and masking by 8 bits and replace it with direct byte addressing. That seems like a straightforward optimization. OTOH, the speed difference is probably too tiny to notice anyway. -- Jim Janney